From 65b9e3d8b0b6d7f93bb943522a27ec362db4df8a Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 14 Nov 2021 13:45:10 -0600 Subject: [PATCH] Small code beauty/clarity edits & improvements New code documentation, insignificant code refactors etc. --- client/main.py | 1 - client/worker.py | 3 ++- helpers.py | 3 ++- server/handler.py | 18 ++++++++++++------ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/client/main.py b/client/main.py index 3d78bcf..cadd5d7 100644 --- a/client/main.py +++ b/client/main.py @@ -1,5 +1,4 @@ import logging -from typing import Tuple from PyQt5.QtWidgets import QApplication diff --git a/client/worker.py b/client/worker.py index fa206be..e95923d 100644 --- a/client/worker.py +++ b/client/worker.py @@ -25,7 +25,8 @@ class ReceiveWorker(QThread): def stop(self) -> None: self.__isRunning = False - def __extract_message(self, data) -> dict: + @staticmethod + def __extract_message(data) -> dict: return { 'nickname': data['nickname'], 'message': data['content'], diff --git a/helpers.py b/helpers.py index 05d194e..eaa2ed3 100644 --- a/helpers.py +++ b/helpers.py @@ -38,6 +38,7 @@ def prepare_message(nickname: str, message: str, color: str, message_id: int, ti def prepare_message_history(messages: List[Tuple[int, str, str, str, int]]) -> bytes: + """Returns a encoded JSON message history object with the messages provided""" return prepare_json( { 'type': constants.Types.MESSAGE_HISTORY, @@ -70,6 +71,6 @@ def formatted_message(message: dict) -> str: def sizeof_fmt(num, suffix='B'): for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']: if abs(num) < 1024.0: - return "%3.1f%s%s" % (num, unit, suffix) + return "%3.0f%s%s" % (num, unit, suffix) num /= 1024.0 return "%.1f%s%s" % (num, 'Yi', suffix) diff --git a/server/handler.py b/server/handler.py index 5e79032..847efca 100644 --- a/server/handler.py +++ b/server/handler.py @@ -50,7 +50,6 @@ class BaseClient(object): timestamp=timestamp ) for client in self.all_clients: - print(f'Sending a message to {client.nickname}') client.send(prepared) def broadcast(self, message: bytes) -> None: @@ -73,6 +72,7 @@ class Client(BaseClient): super().__init__(conn, all_clients, address) self.id = str(uuid.uuid4()) + self.short_id = self.id[:8] self.nickname = self.id[:8] self.color: constants.Color = random.choice(constants.Colors.has_contrast(float(constants.MINIMUM_CONTRAST))) @@ -83,12 +83,13 @@ class Client(BaseClient): def __repr__(self) -> str: if self.last_nickname_change is None: - return f'Client({self.id[:8]})' - return f'Client({self.nickname}, {self.id[:8]})' + return f'Client({self.short_id})' + return f'Client({self.nickname}, {self.short_id})' def connect_database(self) -> None: + """Instantiate""" if self.db is None: - logger.debug(f'Connecting Client({self.id[:8]}) to the database.') + logger.debug(f'Connecting Client({self.short_id}) to the database.') self.db = db.ServerDatabase() def request_nickname(self) -> None: @@ -143,12 +144,13 @@ class Client(BaseClient): def handle_nickname(self, nickname: str) -> None: if self.last_nickname_change is None: - logger.info("Nickname is {}".format(nickname)) + logger.info(f'Nickname is {nickname}') self.broadcast_message(f'{nickname} joined!') self.last_nickname_change = time.time() else: logger.info(f'{self.nickname} changed their name to {nickname}') self.nickname = nickname + # New nickname has to be sent to all clients for client in self.all_clients: client.send_connections_list() @@ -164,9 +166,13 @@ class Client(BaseClient): self.db.close() # Close database connection def handle(self) -> None: - self.connect_database() + """Server mainloop function for a given socket connection""" + + self.connect_database() # Initialize a database connection + while True: try: + logger.info('Waiting to received data') data = self.receive() if data['type'] == constants.Types.REQUEST: