From 9094ba0cb3d467296fa3c4cc967c5ee62e21e083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Wed, 9 Feb 2022 18:26:18 +0100 Subject: [PATCH] feat: better typing fix: emulator split admin message --- docker-compose.yml | 1 + src/emulator.py | 19 ++++++++++++------- src/server.py | 6 +++--- src/utils.py | 10 +++++----- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8a02daa..b46772f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,6 +26,7 @@ services: - REDIS_PORT=$REDIS_PORT - RTMP_HOST=$RTMP_HOST - RTMP_PORT=$RTMP_PORT + - EMULATOR_RAND_RATE=0.2 depends_on: - rtmp - redis diff --git a/src/emulator.py b/src/emulator.py index 4e6557d..157bf4e 100644 --- a/src/emulator.py +++ b/src/emulator.py @@ -39,7 +39,11 @@ screen: mgba.image.Image = mgba.image.Image(EMULATOR_WIDTH, EMULATOR_HEIGHT) core.set_video_buffer(screen) core.reset() -logging.basicConfig(level=logging.DEBUG) +logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(name)s %(levelname)-8s %(message)s", datefmt="(%F %T)") + +# disable all loggers from different files +logging.getLogger("asyncio").setLevel(logging.ERROR) +logging.getLogger("asyncio.coroutines").setLevel(logging.ERROR) mgba.log.silence() r: redis.Redis = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=0) @@ -82,7 +86,7 @@ stream = Popen( ) -def next_action(core: mgba.core.Core): +def next_action(core: mgba.core.Core) -> None: """Select the next key from the redis database. Returns: @@ -98,7 +102,7 @@ def next_action(core: mgba.core.Core): core.clear_keys(*KEYS_MGBA) -def state_manager(loop: asyncio.AbstractEventLoop): +def state_manager(loop: asyncio.AbstractEventLoop) -> None: """Subscribe and respond to messages received from redis. Args: @@ -110,16 +114,16 @@ def state_manager(loop: asyncio.AbstractEventLoop): while True: for message in ps.listen(): if message["type"] == "message": - match message["data"].decode("utf-8").split(":"): + match message["data"].decode("utf-8").split(":", 1): case ["save"]: asyncio.ensure_future(utils.save(core), loop=loop) case ["load", filename]: asyncio.ensure_future(utils.load(core, filename), loop=loop) case _: - print(f"Command not understood: {message}") + logging.debug(f"Command not understood: {message}") -async def emulator(): +async def emulator() -> None: """Start the main loop responsible for handling inputs and sending images to ffmpeg.""" while True: last_frame_t = time.time() @@ -141,8 +145,9 @@ async def emulator(): await asyncio.sleep(sleep_t) -async def main(): +async def main() -> None: """Start the emulator.""" + logging.debug("Emulator started !") loop = asyncio.get_event_loop() # setup states in redis diff --git a/src/server.py b/src/server.py index c2483ad..64a9b79 100644 --- a/src/server.py +++ b/src/server.py @@ -30,7 +30,7 @@ logging.getLogger("websockets.server").setLevel(logging.ERROR) logging.getLogger("websockets.protocol").setLevel(logging.ERROR) r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=0) -r.mset(KEYS_RESET) +r.mset(KEYS_RESET) # type: ignore USERS: Users = Users() @@ -53,7 +53,7 @@ async def parse_message(user: User, message: websockets.typing.Data) -> None: logging.error(f"unsupported action: {message!r} from {user}") -async def handler(websocket: websockets.server.WebSocketServerProtocol, path: str): +async def handler(websocket: websockets.server.WebSocketServerProtocol, path: str) -> None: """Handle the messages sent by a user. Args: @@ -75,7 +75,7 @@ async def handler(websocket: websockets.server.WebSocketServerProtocol, path: st USERS.unregister(user) -async def main(): +async def main() -> None: """Start the websocket server.""" logging.debug("Server started !") async with websockets.serve(handler, WEBSOCKET_SERVE, WEBSOCKET_PORT): # nosec diff --git a/src/utils.py b/src/utils.py index b4c88b3..7a88622 100644 --- a/src/utils.py +++ b/src/utils.py @@ -24,7 +24,7 @@ class User: self.websocket = websocket self.last_message = time.time() - async def send(self, data: str): + async def send(self, data: str) -> None: """Send data through the user's websocket. Args: @@ -45,7 +45,7 @@ class User: class Users(set): """Store `User`s connected to the server.""" - def register(self, user: User): + def register(self, user: User) -> None: """Register a user in the set. Args: @@ -54,7 +54,7 @@ class Users(set): self.add(user) logging.debug(f"user registered: {user}") - def unregister(self, user: User): + def unregister(self, user: User) -> None: """Unregister a user in the set. Args: @@ -64,7 +64,7 @@ class Users(set): logging.debug(f"user unregistered: {user}") -async def save(core: mgba.core.Core): +async def save(core: mgba.core.Core) -> None: state = core.save_raw_state() current_time = time.strftime("%Y-%m-%dT%H:%M:%S") with open(f"states/{current_time}.state", "wb") as state_file: @@ -73,7 +73,7 @@ async def save(core: mgba.core.Core): logging.debug(f"state saved : {current_time}.state") -async def load(core: mgba.core.Core, filename: str): +async def load(core: mgba.core.Core, filename: str) -> None: state = ffi.new("unsigned char[397312]") # pulled 397312 straight from my ass, TODO: check mGBA sources ? with open(f"states/{filename}.state", "rb") as state_file: for i in range(len(state)):