mirror of
https://github.com/aclist/dztui.git
synced 2026-10-25 12:12:31 +01:00
feat: add read_json(), write_json()
This commit is contained in:
1 parent
01c8a5276b
commit
38203a9530
1 file changed
+43
-49
+43
-49
@@ -658,32 +658,42 @@ def block_signals(state: bool = True) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def read_json(path: str) -> str:
|
||||||
|
try:
|
||||||
|
with open(path, "r") as infile:
|
||||||
|
try:
|
||||||
|
data = json.load(infile)
|
||||||
|
except json.decoder.JSONDecodeError as e:
|
||||||
|
raise e
|
||||||
|
except OSError as e:
|
||||||
|
raise e
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def write_json(data: str, path: str) -> None:
|
||||||
|
try:
|
||||||
|
j = json.dumps(data, indent=2)
|
||||||
|
except Exception as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(path, "w") as outfile:
|
||||||
|
outfile.write(j)
|
||||||
|
except OSError as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
def save_res_and_quit(*args) -> None:
|
def save_res_and_quit(*args) -> None:
|
||||||
if App.window.props.is_maximized:
|
if App.window.props.is_maximized:
|
||||||
Gtk.main_quit()
|
Gtk.main_quit()
|
||||||
return
|
return
|
||||||
rect = App.window.get_size()
|
rect = App.window.get_size()
|
||||||
|
|
||||||
def write_json(rect):
|
|
||||||
data = {"res": {"width": rect.width, "height": rect.height}}
|
data = {"res": {"width": rect.width, "height": rect.height}}
|
||||||
j = json.dumps(data, indent=2)
|
|
||||||
with open(res_path, "w") as outfile:
|
|
||||||
outfile.write(j)
|
|
||||||
logger.info(f"Wrote window size to '{res_path}'")
|
|
||||||
|
|
||||||
if os.path.isfile(res_path):
|
|
||||||
with open(res_path, "r") as infile:
|
|
||||||
try:
|
try:
|
||||||
data = json.load(infile)
|
write_json(data, res_path)
|
||||||
data["res"]["width"] = rect.width
|
except Exception as e:
|
||||||
data["res"]["height"] = rect.height
|
logger.critical(e)
|
||||||
with open(res_path, "w") as outfile:
|
|
||||||
outfile.write(json.dumps(data, indent=2))
|
|
||||||
except json.decoder.JSONDecodeError:
|
|
||||||
logger.critical(f"JSON decode error in '{res_path}'")
|
|
||||||
write_json(rect)
|
|
||||||
else:
|
|
||||||
write_json(rect)
|
|
||||||
|
|
||||||
Gtk.main_quit()
|
Gtk.main_quit()
|
||||||
|
|
||||||
@@ -1249,15 +1259,11 @@ class OuterWindow(Gtk.Window):
|
|||||||
self.fullscreen()
|
self.fullscreen()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(res_path, "r") as infile:
|
data = read_json(res_path)
|
||||||
try:
|
|
||||||
data = json.load(infile)
|
|
||||||
valid_json = True
|
valid_json = True
|
||||||
except json.decoder.JSONDecodeError:
|
except Exception as e:
|
||||||
logger.critical(f"JSON decode error in '{res_path}'")
|
|
||||||
valid_json = False
|
|
||||||
except OSError:
|
|
||||||
valid_json = False
|
valid_json = False
|
||||||
|
logger.critical(e)
|
||||||
|
|
||||||
if valid_json:
|
if valid_json:
|
||||||
res = data["res"]
|
res = data["res"]
|
||||||
@@ -2596,39 +2602,27 @@ class TreeView(Gtk.TreeView):
|
|||||||
def _on_col_width_changed(
|
def _on_col_width_changed(
|
||||||
self, col: Gtk.TreeViewColumn, width: GObject.ParamSpecInt
|
self, col: Gtk.TreeViewColumn, width: GObject.ParamSpecInt
|
||||||
) -> None:
|
) -> None:
|
||||||
def write_json(title, size):
|
|
||||||
data = {"cols": {title: size}}
|
|
||||||
j = json.dumps(data, indent=2)
|
|
||||||
with open(geometry_path, "w") as outfile:
|
|
||||||
outfile.write(j)
|
|
||||||
logger.info(f"Wrote initial column widths to '{geometry_path}'")
|
|
||||||
|
|
||||||
title = col.get_title()
|
title = col.get_title()
|
||||||
size = col.get_width()
|
size = col.get_width()
|
||||||
|
|
||||||
if os.path.isfile(geometry_path):
|
|
||||||
with open(geometry_path, "r") as infile:
|
|
||||||
try:
|
try:
|
||||||
data = json.load(infile)
|
data = read_json(geometry_path)
|
||||||
data["cols"][title] = size
|
data["cols"][title] = size
|
||||||
with open(geometry_path, "w") as outfile:
|
except Exception as e:
|
||||||
outfile.write(json.dumps(data, indent=2))
|
logger.critical(e)
|
||||||
except json.decoder.JSONDecodeError:
|
data = {"cols": {title: size}}
|
||||||
logger.critical(f"JSON decode error in '{geometry_path}'")
|
|
||||||
write_json(title, size)
|
try:
|
||||||
else:
|
write_json(data, geometry_path)
|
||||||
write_json(title, size)
|
except Exception as e:
|
||||||
|
logger.critical(e)
|
||||||
|
|
||||||
def initialize_columns(self) -> None:
|
def initialize_columns(self) -> None:
|
||||||
if os.path.isfile(geometry_path):
|
|
||||||
with open(geometry_path, "r") as infile:
|
|
||||||
try:
|
try:
|
||||||
data = json.load(infile)
|
data = read_json(geometry_path)
|
||||||
valid_json = True
|
valid_json = True
|
||||||
except json.decoder.JSONDecodeError:
|
except Exception as e:
|
||||||
logger.critical(f"JSON decode error in '{geometry_path}'")
|
logger.critical(e)
|
||||||
valid_json = False
|
|
||||||
else:
|
|
||||||
valid_json = False
|
valid_json = False
|
||||||
|
|
||||||
browser_cols = [
|
browser_cols = [
|
||||||
|
|||||||
Reference in new issue
Block a user