From adb5435af430243504bdacf7f0bfc17e1cc60fe7 Mon Sep 17 00:00:00 2001 From: JezerM Date: Tue, 7 Sep 2021 22:22:48 -0600 Subject: [PATCH] Added arguments parser --- web-greeter/__main__.py | 70 +++++++++++++++++++-- web-greeter/browser/browser.py | 3 +- web-greeter/{bridge => browser}/devtools.py | 3 + web-greeter/config.py | 8 ++- 4 files changed, 76 insertions(+), 8 deletions(-) rename web-greeter/{bridge => browser}/devtools.py (95%) diff --git a/web-greeter/__main__.py b/web-greeter/__main__.py index e6a0a8b..fa6d7af 100644 --- a/web-greeter/__main__.py +++ b/web-greeter/__main__.py @@ -26,17 +26,77 @@ # along with Web Greeter; If not, see . # Standard lib -import sys -import ruamel.yaml as yaml -import os +import sys, argparse, os +from typing import List # 3rd-Party Libs -from browser.browser import Browser -from logger import logger import globals import config +def list_themes() -> List[str]: + themes_dir = config.web_greeter_config["app"]["theme_dir"] + themes_dir = themes_dir if os.path.exists(themes_dir) else "/usr/share/web-greeter/themes" + filenames = os.listdir(themes_dir) + + dirlist = [] + for file in filenames: + if os.path.isdir(os.path.join(themes_dir, file)): + dirlist.append(file) + + return dirlist + +def print_themes(): + themes_dir = config.web_greeter_config["app"]["theme_dir"] + themes_dir = themes_dir if os.path.exists(themes_dir) else "/usr/share/web-greeter/themes" + themes = list_themes() + print("Themes are located in {themes_dir}\n".format(themes_dir = themes_dir)) + for theme in themes: + print("-", theme) + + +def set_theme(theme: str): + config.web_greeter_config["config"]["greeter"]["theme"] = theme + +def set_debug(value: bool): + conf = config.web_greeter_config["config"] + app = config.web_greeter_config["app"] + conf["greeter"]["debug_mode"] = value + app["decorated"] = value + app["fullscreen"] = not value + +def parse(argv): + version = config.web_greeter_config["app"]["version"]["full"] + parser = argparse.ArgumentParser(prog="web-greeter", add_help=False) + parser.add_argument("-h", "--help", action="help", help="Show this help message and exit") + parser.add_argument("-v", "--version", action="version", version=version, help="Show version number") + + parser.add_argument("--debug", action="store_true", help="Run the greeter in debug mode", dest="debug", default=None) + parser.add_argument("--normal", action="store_false", help="Run in non-debug mode", dest="debug") + parser.add_argument("--list", action="store_true", help="List available themes") + parser.add_argument("--theme", help="Set the theme to use", metavar="[name]") + + args: argparse.Namespace + + try: + args = parser.parse_args(argv) + except argparse.ArgumentError: + sys.exit() + + # print(args) + + if (args.list): + print_themes() + sys.exit() + if (args.theme): + set_theme(args.theme) + if (args.debug != None): + set_debug(args.debug) + if __name__ == '__main__': + parse(sys.argv[1:]) + + from browser.browser import Browser + globals.greeter = Browser() greeter = globals.greeter greeter.run() diff --git a/web-greeter/browser/browser.py b/web-greeter/browser/browser.py index 37d50c9..c0cffe0 100644 --- a/web-greeter/browser/browser.py +++ b/web-greeter/browser/browser.py @@ -29,7 +29,7 @@ # Standard lib from browser.window import MainWindow -from bridge.devtools import DevTools +from browser.devtools import DevTools import os from typing import ( Dict, @@ -138,7 +138,6 @@ class Browser(Application): def init(self): logger.debug("Initializing Browser Window") - web_greeter_config["config"]["greeter"]["debug_mode"] = True if web_greeter_config["config"]["greeter"]["debug_mode"]: os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = '12345' diff --git a/web-greeter/bridge/devtools.py b/web-greeter/browser/devtools.py similarity index 95% rename from web-greeter/bridge/devtools.py rename to web-greeter/browser/devtools.py index 2b562a1..540f14f 100644 --- a/web-greeter/bridge/devtools.py +++ b/web-greeter/browser/devtools.py @@ -32,6 +32,8 @@ from PyQt5.QtWebEngineWidgets import ( QWebEnginePage, ) +from logger import logger + class DevTools: def __init__(self): @@ -42,4 +44,5 @@ class DevTools: self.view.load(QUrl('http://127.0.0.1:12345')) self.view.show() + logger.debug("DevTools initialized") diff --git a/web-greeter/config.py b/web-greeter/config.py index a40cfb1..82acb9f 100644 --- a/web-greeter/config.py +++ b/web-greeter/config.py @@ -66,7 +66,13 @@ web_greeter_config = { "fullscreen": True, "frame": False, "debug_mode": False, - "theme_dir": "/usr/share/web-greeter/themes/" + "theme_dir": "/usr/share/web-greeter/themes/", + "version": { + "full": "3.0.0", + "major": 3, + "minor": 3, + "micro": 0, + }, } }