From f7b009099a1a54a3160e637f5acd543adf2705b7 Mon Sep 17 00:00:00 2001 From: caryoscelus Date: Tue, 6 Sep 2022 16:04:58 +0000 Subject: [PATCH] consolidate & fix open browser behaviour --- src/Ui/UiServer.py | 18 ++---------------- src/main.py | 16 ++-------------- src/util/helper.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/src/Ui/UiServer.py b/src/Ui/UiServer.py index 63738c99e..088208323 100644 --- a/src/Ui/UiServer.py +++ b/src/Ui/UiServer.py @@ -13,6 +13,7 @@ from Debug import Debug import importlib +from util import helper # Skip websocket handler if not necessary class UiWSGIHandler(WebSocketHandler): @@ -144,22 +145,7 @@ def start(self): self.log.info("Web interface: http://%s:%s/" % (config.ui_ip, config.ui_port)) self.log.info("--------------------------------------") - if config.open_browser and config.open_browser != "False": - logging.info("Opening browser: %s...", config.open_browser) - import webbrowser - ui_ip = config.ui_ip if config.ui_ip != "*" else "127.0.0.1" - url = f'http://{ui_ip}:{config.ui_port}/{config.homepage}' - try: - if config.open_browser == "default_browser": - browser = webbrowser.get() - else: - browser = webbrowser.get(config.open_browser) - gevent.spawn_later(0.3, browser.open, url, new=2) - except webbrowser.Error as err: - import subprocess - subprocess.Popen([config.open_browser, url]) - except Exception as err: - print("Error starting browser: %s" % err) + helper.openBrowser(config.open_browser) self.server = WSGIServer((self.ip, self.port), handler, handler_class=UiWSGIHandler, log=self.log) self.server.sockets = {} diff --git a/src/main.py b/src/main.py index d348f37a7..47701b993 100644 --- a/src/main.py +++ b/src/main.py @@ -69,20 +69,8 @@ def init_dirs(): lock = helper.openLocked("%s/lock.pid" % config.data_dir, "w") lock.write("%s" % os.getpid()) except BlockingIOError as err: - startupError("Can't open lock file, your ZeroNet client is probably already running, exiting... (%s)" % err) - if config.open_browser and config.open_browser != "False": - print("Opening browser: %s...", config.open_browser) - import webbrowser - try: - if config.open_browser == "default_browser": - browser = webbrowser.get() - else: - browser = webbrowser.get(config.open_browser) - browser.open("http://%s:%s/%s" % ( - config.ui_ip if config.ui_ip != "*" else "127.0.0.1", config.ui_port, config.homepage - ), new=2) - except Exception as err: - startupError("Error starting browser: %s" % err) + startupError(f"Can't open lock file, your 0net client is probably already running, exiting... ({err})") + helper.openBrowser(config.open_browser) sys.exit() config.initLogging() diff --git a/src/util/helper.py b/src/util/helper.py index 61455b085..fa9798645 100644 --- a/src/util/helper.py +++ b/src/util/helper.py @@ -354,3 +354,14 @@ def wrapper(*args, **kwargs): yield back.encode() return wrapper + +def openBrowser(agent): + if agent and agent != "False": + print(f"Opening browser: {agent}...") + ui_ip = config.ui_ip if config.ui_ip != "*" else "127.0.0.1" + url = f'http://{ui_ip}:{config.ui_port}/{config.homepage}' + try: + import subprocess + subprocess.Popen([config.open_browser, url]) + except Exception as err: + print(f"Error starting browser: {err}")