From 596c548522031880c45443134b8b5e84b4cb5aa0 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 30 Aug 2024 15:12:59 +0200 Subject: [PATCH] Added settings import and export --- sbapp/main.py | 70 ++++++++++++++++++++++++++++++++++++++++++ sbapp/sideband/core.py | 5 ++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/sbapp/main.py b/sbapp/main.py index f4958fe..148179e 100644 --- a/sbapp/main.py +++ b/sbapp/main.py @@ -9,6 +9,8 @@ parser = argparse.ArgumentParser(description="Sideband LXMF Client") parser.add_argument("-v", "--verbose", action='store_true', default=False, help="increase logging verbosity") parser.add_argument("-c", "--config", action='store', default=None, help="specify path of config directory") parser.add_argument("-d", "--daemon", action='store_true', default=False, help="run as a daemon, without user interface") +parser.add_argument("--export-settings", action='store', default=None, help="export application settings to file") +parser.add_argument("--import-settings", action='store', default=None, help="import application settings from file") parser.add_argument("--version", action="version", version="sideband {version}".format(version=__version__)) args = parser.parse_args() sys.argv = [sys.argv[0]] @@ -22,6 +24,74 @@ import base64 import threading import RNS.vendor.umsgpack as msgpack +if args.export_settings: + from .sideband.core import SidebandCore + sideband = SidebandCore( + None, + config_path=args.config, + is_client=False, + verbose=(args.verbose or __debug_build__), + is_daemon=True, + load_config_only=True, + ) + + sideband.version_str = "v"+__version__+" "+__variant__ + + import json + export = sideband.config.copy() + for k in export: + if isinstance(export[k], bytes): + export[k] = RNS.hexrep(export[k], delimit=False) + try: + export_path = os.path.expanduser(args.export_settings) + with open(export_path, "wb") as export_file: + export_file.write(json.dumps(export, indent=4).encode("utf-8")) + print(f"Application settings written to {export_path}") + exit(0) + + except Exception as e: + print(f"Could not write application settings to {export_path}. The contained exception was:\n{e}") + exit(1) + +elif args.import_settings: + from .sideband.core import SidebandCore + sideband = SidebandCore( + None, + config_path=args.config, + is_client=False, + verbose=(args.verbose or __debug_build__), + is_daemon=True, + load_config_only=True, + ) + + sideband.version_str = "v"+__version__+" "+__variant__ + + import json + addr_fields = ["lxmf_propagation_node", "last_lxmf_propagation_node", "nn_home_node", "telemetry_collector"] + try: + import_path = os.path.expanduser(args.import_settings) + imported = None + with open(import_path, "rb") as import_file: + json_data = import_file.read().decode("utf-8") + imported = json.loads(json_data) + for k in imported: + if k in addr_fields and imported[k] != None: + imported[k] = bytes.fromhex(imported[k]) + if len(imported[k]) != RNS.Reticulum.TRUNCATED_HASHLENGTH//8: + raise ValueError(f"Invalid hash length for {RNS.prettyhexrep(imported[k])}") + + if imported: + sideband.config = imported + sideband.save_configuration() + while sideband.saving_configuration: + time.sleep(0.1) + print(f"Application settings imported from {import_path}") + exit(0) + + except Exception as e: + print(f"Could not import application settings from {import_path}. The contained exception was:\n{e}") + exit(1) + if not args.daemon: from kivy.logger import Logger, LOG_LEVELS from PIL import Image as PilImage diff --git a/sbapp/sideband/core.py b/sbapp/sideband/core.py index ca514f9..fcf2512 100644 --- a/sbapp/sideband/core.py +++ b/sbapp/sideband/core.py @@ -108,7 +108,7 @@ class SidebandCore(): # stream logger self.log_announce(destination_hash, app_data, dest_type=SidebandCore.aspect_filter) - def __init__(self, owner_app, config_path = None, is_service=False, is_client=False, android_app_dir=None, verbose=False, owner_service=None, service_context=None, is_daemon=False): + def __init__(self, owner_app, config_path = None, is_service=False, is_client=False, android_app_dir=None, verbose=False, owner_service=None, service_context=None, is_daemon=False, load_config_only=False): self.is_service = is_service self.is_client = is_client self.is_daemon = is_daemon @@ -231,6 +231,9 @@ class SidebandCore(): except Exception as e: RNS.log("Error while configuring Sideband: "+str(e), RNS.LOG_ERROR) + if load_config_only: + return + # Initialise Reticulum configuration if RNS.vendor.platformutils.get_platform() == "android": try: