From fbc151060eb272a2f98922c23f5598ab187ad034 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 8 Apr 2021 20:57:31 +0200 Subject: [PATCH] User interface skeleton --- nomadnet/NomadNetworkApp.py | 34 +++++++++++++++++++++++++------- nomadnet/__init__.py | 9 ++++++++- nomadnet/nomadnet.py | 11 +++++------ nomadnet/ui/GraphicalUI.py | 8 ++++++++ nomadnet/ui/MenuUI.py | 8 ++++++++ nomadnet/ui/TextUI.py | 39 +++++++++++++++++++++++++++++++++++++ nomadnet/ui/WebUI.py | 8 ++++++++ nomadnet/ui/__init__.py | 34 +++++++++++++++++++++++++++++++- 8 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 nomadnet/ui/GraphicalUI.py create mode 100644 nomadnet/ui/MenuUI.py create mode 100644 nomadnet/ui/TextUI.py create mode 100644 nomadnet/ui/WebUI.py diff --git a/nomadnet/NomadNetworkApp.py b/nomadnet/NomadNetworkApp.py index 991696c..e8c9fc5 100644 --- a/nomadnet/NomadNetworkApp.py +++ b/nomadnet/NomadNetworkApp.py @@ -1,7 +1,10 @@ import os -import RNS import atexit +import RNS +import nomadnet + +from ._version import __version__ from .vendor.configobj import ConfigObj class NomadNetworkApp: @@ -9,14 +12,17 @@ class NomadNetworkApp: configdir = os.path.expanduser("~")+"/.nomadnetwork" - def exit_handler(): + def exit_handler(self): RNS.log("Nomad Network Client exit handler executing...") def __init__(self, configdir = None, rnsconfigdir = None): + self.version = __version__ self.enable_client = False self.enable_node = False self.identity = None + self.uimode = None + if configdir == None: self.configdir = NomadNetworkApp.configdir else: @@ -47,7 +53,7 @@ class NomadNetworkApp: except Exception as e: RNS.log("Could not parse the configuration at "+self.configpath, RNS.LOG_ERROR) RNS.log("Check your configuration file for errors!", RNS.LOG_ERROR) - RNS.panic() + nomadnet.panic() else: RNS.log("Could not load config file, creating default configuration file...") self.createDefaultConfig() @@ -62,11 +68,11 @@ class NomadNetworkApp: RNS.log("Loaded Primary Identity %s from %s" % (str(self.identity), self.identitypath)) else: RNS.log("Could not load the Primary Identity from "+self.identitypath, RNS.LOG_ERROR) - RNS.panic() + nomadnet.panic() except Exception as e: RNS.log("Could not load the Primary Identity from "+self.identitypath, RNS.LOG_ERROR) RNS.log("The contained exception was: %s" % (str(e)), RNS.LOG_ERROR) - RNS.panic() + nomadnet.panic() else: try: RNS.log("No Primary Identity file found, creating new...") @@ -79,11 +85,11 @@ class NomadNetworkApp: RNS.panic() self.applyConfig() - self.rns = RNS.Reticulum(configdir = rnsconfigdir) - atexit.register(self.exit_handler) + self.ui = nomadnet.ui.spawn(self.uimode) + def createDefaultConfig(self): self.config = ConfigObj(__default_nomadnet_config__) @@ -114,6 +120,19 @@ class NomadNetworkApp: value = self.config["client"].as_bool(option) self.enable_client = value + if option == "user_interface": + value = value.lower() + if value == "none": + self.uimode = nomadnet.ui.UI_NONE + if value == "menu": + self.uimode = nomadnet.ui.UI_MENU + if value == "text": + self.uimode = nomadnet.ui.UI_TEXT + if value == "graphical": + self.uimode = nomadnet.ui.UI_GRAPHICAL + if value == "web": + self.uimode = nomadnet.ui.UI_WEB + if "node" in self.config: for option in self.config["node"]: value = self.config["node"][option] @@ -155,6 +174,7 @@ loglevel = 4 [client] enable_client = Yes +user_interface = text [node] diff --git a/nomadnet/__init__.py b/nomadnet/__init__.py index f49e360..8297e92 100644 --- a/nomadnet/__init__.py +++ b/nomadnet/__init__.py @@ -1,5 +1,12 @@ import os import glob +from .NomadNetworkApp import NomadNetworkApp +from .ui import * + + modules = glob.glob(os.path.dirname(__file__)+"/*.py") -__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')] \ No newline at end of file +__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')] + +def panic(): + os._exit(255) \ No newline at end of file diff --git a/nomadnet/nomadnet.py b/nomadnet/nomadnet.py index 9cc254c..2ee25de 100644 --- a/nomadnet/nomadnet.py +++ b/nomadnet/nomadnet.py @@ -1,14 +1,13 @@ #!/usr/bin/env python3 -import argparse - from ._version import __version__ -from .NomadNetworkApp import NomadNetworkApp + +import argparse +import nomadnet + def program_setup(configdir, rnsconfigdir): - app = NomadNetworkApp(configdir = configdir, rnsconfigdir = rnsconfigdir) - input() - app.quit() + app = nomadnet.NomadNetworkApp(configdir = configdir, rnsconfigdir = rnsconfigdir) def main(): try: diff --git a/nomadnet/ui/GraphicalUI.py b/nomadnet/ui/GraphicalUI.py new file mode 100644 index 0000000..ee51ae7 --- /dev/null +++ b/nomadnet/ui/GraphicalUI.py @@ -0,0 +1,8 @@ +import RNS +import nomadnet + +class GraphicalUI: + + def __init__(self): + RNS.log("Graphical UI not implemented", RNS.LOG_ERROR) + nomadnet.panic() \ No newline at end of file diff --git a/nomadnet/ui/MenuUI.py b/nomadnet/ui/MenuUI.py new file mode 100644 index 0000000..b5bc34b --- /dev/null +++ b/nomadnet/ui/MenuUI.py @@ -0,0 +1,8 @@ +import RNS +import nomadnet + +class MenuUI: + + def __init__(self): + RNS.log("Menu UI not implemented", RNS.LOG_ERROR) + nomadnet.panic() \ No newline at end of file diff --git a/nomadnet/ui/TextUI.py b/nomadnet/ui/TextUI.py new file mode 100644 index 0000000..00c40e0 --- /dev/null +++ b/nomadnet/ui/TextUI.py @@ -0,0 +1,39 @@ +import RNS +from nomadnet import NomadNetworkApp +import importlib + +class TextUI: + + def __init__(self): + self.app = NomadNetworkApp.get_shared_instance() + + self.loop = None + self.main_widget = None + + if importlib.util.find_spec("urwid") != None: + import urwid + else: + RNS.log("The text-mode user interface requires Urwid to be installed on your system.", RNS.LOG_ERROR) + RNS.log("You can install it with the command: pip3 install urwid", RNS.LOG_ERROR) + nomadnet.panic() + + loop = urwid.MainLoop(self.build_intro()) + loop.run() + + def build_intro(self): + import urwid + + font = urwid.font.HalfBlock5x4Font() + + big_text = "Nomad Network" + big_text = urwid.BigText(("intro_bigtext", big_text), font) + big_text = urwid.Padding(big_text, align="center", width="clip") + + intro = urwid.Pile([ + big_text, + urwid.Text(["Version %s" % (str(self.app.version))], align="center"), + urwid.Divider(), + urwid.Text(("intro_smalltext", "-= Starting =-"), align="center"), + ]) + + return urwid.Filler(intro) \ No newline at end of file diff --git a/nomadnet/ui/WebUI.py b/nomadnet/ui/WebUI.py new file mode 100644 index 0000000..1e00807 --- /dev/null +++ b/nomadnet/ui/WebUI.py @@ -0,0 +1,8 @@ +import RNS +import nomadnet + +class WebUI: + + def __init__(self): + RNS.log("Web UI not implemented", RNS.LOG_ERROR) + nomadnet.panic() \ No newline at end of file diff --git a/nomadnet/ui/__init__.py b/nomadnet/ui/__init__.py index f49e360..e6e561d 100644 --- a/nomadnet/ui/__init__.py +++ b/nomadnet/ui/__init__.py @@ -1,5 +1,37 @@ import os import glob +import RNS +import nomadnet + +from .MenuUI import MenuUI +from .TextUI import TextUI +from .GraphicalUI import GraphicalUI +from .WebUI import WebUI + +UI_NONE = 0x00 +UI_MENU = 0x01 +UI_TEXT = 0x02 +UI_GRAPHICAL = 0x03 +UI_WEB = 0x04 + +UI_MODES = [UI_MENU, UI_TEXT, UI_GRAPHICAL, UI_WEB] modules = glob.glob(os.path.dirname(__file__)+"/*.py") -__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')] \ No newline at end of file +__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')] + +def spawn(uimode): + if uimode in UI_MODES: + RNS.log("Starting user interface...", RNS.LOG_INFO) + if uimode == UI_MENU: + return MenuUI() + elif uimode == UI_TEXT: + return TextUI() + elif uimode == UI_GRAPHICAL: + return GraphicalUI() + elif uimode == UI_WEB: + return WebUI() + else: + return None + else: + RNS.log("Invalid UI mode", RNS.LOG_ERROR) + nomadnet.panic() \ No newline at end of file