From 59a26e071cc57ada4d9a2cf303b83b614fd280d6 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Thu, 4 Jun 2020 21:12:05 +0200 Subject: [PATCH] clean up --- MultiClient.py | 9 ++++---- Mystery.py | 2 +- WebUiClient.py => WebUI.py | 38 +++++++++++++++++++++++++++++++++ WebUiServer.py | 43 -------------------------------------- 4 files changed, 43 insertions(+), 49 deletions(-) rename WebUiClient.py => WebUI.py (73%) delete mode 100644 WebUiServer.py diff --git a/MultiClient.py b/MultiClient.py index 805211c5..057e9e01 100644 --- a/MultiClient.py +++ b/MultiClient.py @@ -25,8 +25,7 @@ import prompt_toolkit import typing from prompt_toolkit.patch_stdout import patch_stdout from NetUtils import Endpoint -import WebUiServer -import WebUiClient +import WebUI import Regions import Utils @@ -44,7 +43,7 @@ class Context(): self.snes_address = snes_address self.server_address = server_address - self.ui_node = WebUiClient.WebUiClient() + self.ui_node = WebUI.WebUiClient() self.custom_address = None self.webui_socket_port = port @@ -687,7 +686,7 @@ async def server_loop(ctx: Context, address=None): cmd, args = (msg[0], msg[1]) if len(msg) > 1 else (msg, None) await process_server_cmd(ctx, cmd, args) ctx.ui_node.log_warning('Disconnected from multiworld server, type /connect to reconnect') - except WebUiClient.WaitingForUiException: + except WebUI.WaitingForUiException: pass except ConnectionRefusedError: if cached_address: @@ -1257,7 +1256,7 @@ async def main(): if not sock.connect_ex(('localhost', port)) == 0: break import threading - WebUiServer.start_server( + WebUI.start_server( port, on_start=threading.Timer(1, webbrowser.open, (f'http://localhost:5050?port={port}',)).start) if args.diff_file: diff --git a/Mystery.py b/Mystery.py index 55e91f85..b5495ff2 100644 --- a/Mystery.py +++ b/Mystery.py @@ -40,7 +40,7 @@ def main(): parser.add_argument('--meta', default=None) parser.add_argument('--log_output_path', help='Path to store output log') parser.add_argument('--loglevel', default='info', help='Sets log level') - parser.add_argument('--yaml_output', default=256, type=lambda value: min(max(int(value), 0), 255), + parser.add_argument('--yaml_output', default=0, type=lambda value: min(max(int(value), 0), 255), help='Output rolled mystery results to yaml up to specified number (made for async multiworld)') for player in range(1, multiargs.multi + 1): diff --git a/WebUiClient.py b/WebUI.py similarity index 73% rename from WebUiClient.py rename to WebUI.py index 176184cf..b298a5eb 100644 --- a/WebUiClient.py +++ b/WebUI.py @@ -1,4 +1,11 @@ +import http.server import logging +import os +import socket +import socketserver +import threading +import webbrowser +from functools import partial from NetUtils import Node from MultiClient import Context @@ -6,6 +13,7 @@ import Utils logger = logging.getLogger("WebUIRelay") + class WebUiClient(Node): def __init__(self): super().__init__() @@ -104,3 +112,33 @@ class WebUiClient(Node): class WaitingForUiException(Exception): pass + + +webthread = None +PORT = 5050 +Handler = partial(http.server.SimpleHTTPRequestHandler, + directory=Utils.local_path(os.path.join("data", "web", "public"))) + + +def start_server(socket_port: int, on_start=lambda: None): + global webthread + try: + server = socketserver.TCPServer(("", PORT), Handler) + except OSError: + # In most cases "Only one usage of each socket address (protocol/network address/port) is normally permitted" + import logging + + # If the exception is caused by our desired port being unavailable, assume the web server is already running + # from another client instance + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + if sock.connect_ex(('localhost', PORT)) == 0: + logging.info("Web server is already running in another client window.") + webbrowser.open(f'http://localhost:{PORT}?port={socket_port}') + return + + # If the exception is caused by something else, report on it + logging.exception("Unable to bind port for local web server. The CLI client should work in all cases.") + else: + print("serving at port", PORT) + on_start() + webthread = threading.Thread(target=server.serve_forever).start() diff --git a/WebUiServer.py b/WebUiServer.py deleted file mode 100644 index ada7b057..00000000 --- a/WebUiServer.py +++ /dev/null @@ -1,43 +0,0 @@ -import http.server -import socketserver -import os -import socket -import threading -from functools import partial -import webbrowser - -import Utils - -webthread = None - -PORT = 5050 - -Handler = partial(http.server.SimpleHTTPRequestHandler, directory=Utils.local_path(os.path.join("data", "web", "public"))) - - -def start_server(socket_port: int, on_start=lambda: None): - global webthread - try: - server = socketserver.TCPServer(("", PORT), Handler) - except OSError: - # In most cases "Only one usage of each socket address (protocol/network address/port) is normally permitted" - import logging - - # If the exception is caused by our desired port being unavailable, assume the web server is already running - # from another client instance - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: - if sock.connect_ex(('localhost', PORT)) == 0: - logging.info("Web server is already running in another client window.") - webbrowser.open(f'http://localhost:{PORT}?port={socket_port}') - return - - # If the exception is caused by something else, report on it - logging.exception("Unable to bind port for local web server. The CLI client should work in all cases.") - else: - print("serving at port", PORT) - on_start() - webthread = threading.Thread(target=server.serve_forever).start() - - -if __name__ == "__main__": - start_server(5090)