cleanup
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from logzero import logger as log
|
||||
|
||||
from bokeh.models import AutocompleteInput, Button, Div
|
||||
|
||||
from ..buki import Column, Row
|
||||
@ -27,22 +29,17 @@ class Files(Column):
|
||||
super().__init__(lbl, inner, sizing_mode=sizing_mode)
|
||||
|
||||
|
||||
def get_fname(self):
|
||||
return self.ai_fname.value.strip()
|
||||
|
||||
|
||||
def on_click_save(self, handler):
|
||||
def wrapper(_event):
|
||||
fn = self.get_fname()
|
||||
if not fn:
|
||||
print("empty filename")
|
||||
log.debug("Clicked save with empty filename")
|
||||
return
|
||||
|
||||
compl = self.ai_fname.completions
|
||||
if fn not in compl:
|
||||
compl.append(fn)
|
||||
self.add_completion(fn)
|
||||
|
||||
fn = self.folder / fn
|
||||
log.debug(f"save to {fn}")
|
||||
|
||||
data = handler()
|
||||
json_save(data, fn)
|
||||
@ -53,30 +50,35 @@ class Files(Column):
|
||||
def wrapper(_event):
|
||||
fn = self.get_fname()
|
||||
if not fn:
|
||||
print("empty filename")
|
||||
log.debug("Clicked load with empty filename")
|
||||
return
|
||||
|
||||
fn = self.folder / fn
|
||||
log.debug(f"load from {fn}")
|
||||
|
||||
data = json_load(fn)
|
||||
handler(data)
|
||||
self.btn_load.on_click(wrapper)
|
||||
|
||||
|
||||
def get_fname(self):
|
||||
return self.ai_fname.value.strip()
|
||||
|
||||
|
||||
def add_completion(self, fn):
|
||||
compl = self.ai_fname.completions
|
||||
if fn not in compl:
|
||||
compl.append(fn)
|
||||
|
||||
|
||||
|
||||
def json_save(what, filename, *args, indent=4, sort_keys=True, **kwargs):
|
||||
print("save to", filename)
|
||||
with open(filename, "w") as f:
|
||||
json.dump(what, f, *args, indent=indent, sort_keys=sort_keys, **kwargs)
|
||||
|
||||
def json_load(filename, *args, **kwargs):
|
||||
print("load from", filename)
|
||||
with open(filename, "r") as f:
|
||||
return json.load(f, *args, **kwargs)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user