User interface skeleton

This commit is contained in:
Mark Qvist 2021-04-08 20:57:31 +02:00
parent 9fdcbf11b8
commit fbc151060e
8 changed files with 136 additions and 15 deletions

View File

@ -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]

View File

@ -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')]
def panic():
os._exit(255)

View File

@ -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:

View File

@ -0,0 +1,8 @@
import RNS
import nomadnet
class GraphicalUI:
def __init__(self):
RNS.log("Graphical UI not implemented", RNS.LOG_ERROR)
nomadnet.panic()

8
nomadnet/ui/MenuUI.py Normal file
View File

@ -0,0 +1,8 @@
import RNS
import nomadnet
class MenuUI:
def __init__(self):
RNS.log("Menu UI not implemented", RNS.LOG_ERROR)
nomadnet.panic()

39
nomadnet/ui/TextUI.py Normal file
View File

@ -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)

8
nomadnet/ui/WebUI.py Normal file
View File

@ -0,0 +1,8 @@
import RNS
import nomadnet
class WebUI:
def __init__(self):
RNS.log("Web UI not implemented", RNS.LOG_ERROR)
nomadnet.panic()

View File

@ -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')]
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()