mirror of
https://github.com/Xevion/tcp-chat.git
synced 2025-12-06 15:16:43 -06:00
update dialog to accept faker default nickname launch sys.argv, prepare_json fix typehint/arg name, commit sizeof helper func, update launch.py for new client/main.py
This commit is contained in:
@@ -44,10 +44,12 @@ class NicknameDialog(QDialog, Ui_NicknameDialog):
|
||||
|
||||
|
||||
class ConnectionDialog(QDialog, Ui_ConnectionDialog):
|
||||
def __init__(self, *args, **kwargs):
|
||||
def __init__(self, nickname: str = None, *args, **kwargs):
|
||||
super(ConnectionDialog, self).__init__(*args, **kwargs)
|
||||
self.setupUi(self)
|
||||
|
||||
if nickname: self.nickname_input.setText(nickname)
|
||||
|
||||
self.connect_button.setDisabled(True)
|
||||
self.server_address_input.textEdited.connect(self.validation)
|
||||
self.port_input.textEdited.connect(self.validation)
|
||||
|
||||
@@ -9,10 +9,10 @@ from client.gui import MainWindow
|
||||
logger = logging.getLogger(__file__)
|
||||
|
||||
|
||||
def main():
|
||||
def main(nickname: str = None):
|
||||
app = QApplication([])
|
||||
app.setApplicationName("TCPChat Client")
|
||||
connect_dialog = ConnectionDialog()
|
||||
connect_dialog = ConnectionDialog(nickname=nickname)
|
||||
app.exec_()
|
||||
|
||||
if connect_dialog.connect_pressed:
|
||||
|
||||
16
helpers.py
16
helpers.py
@@ -1,7 +1,7 @@
|
||||
import html
|
||||
import json
|
||||
import time
|
||||
from typing import List, Tuple
|
||||
from typing import List, Tuple, Any
|
||||
|
||||
import constants
|
||||
|
||||
@@ -14,14 +14,14 @@ def prepare(message: str, encoding='utf-8') -> bytes:
|
||||
return (header + message).encode(encoding)
|
||||
|
||||
|
||||
def prepare_json(object) -> bytes:
|
||||
def prepare_json(obj: Any) -> bytes:
|
||||
"""
|
||||
Prepares a object for sending as encoded JSON with a header.
|
||||
|
||||
:param object: A JSON-encodable object
|
||||
:param obj: A JSON-encodable object
|
||||
:return: Encoded JSON
|
||||
"""
|
||||
return prepare(json.dumps(object))
|
||||
return prepare(json.dumps(obj))
|
||||
|
||||
|
||||
def prepare_message(nickname: str, message: str, color: str, message_id: int, timestamp: int = None) -> bytes:
|
||||
@@ -65,3 +65,11 @@ def formatted_message(message: dict) -> str:
|
||||
nick_esc = html.escape(message["nickname"])
|
||||
msg_esc = html.escape(message["message"])
|
||||
return f'<<span style="color: {message["color"]}">{nick_esc}</span>> {msg_esc}'
|
||||
|
||||
|
||||
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)
|
||||
num /= 1024.0
|
||||
return "%.1f%s%s" % (num, 'Yi', suffix)
|
||||
|
||||
20
launch.py
20
launch.py
@@ -1,18 +1,24 @@
|
||||
import sys
|
||||
|
||||
from faker import Faker
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
arg_count = len(sys.argv) - 1
|
||||
if arg_count < 1:
|
||||
print('Please provide one argument besides the file describing whether to launch the client or server.')
|
||||
print('Client/C/1 to launch the client. Server/S/2 to launch the server.')
|
||||
else:
|
||||
if str(sys.argv[1]).lower() in ['client', 'c', '1']:
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
from client.gui import MainWindow
|
||||
nick = None
|
||||
if arg_count >= 2:
|
||||
if sys.argv[2] == 'random':
|
||||
fake = Faker()
|
||||
nick = fake.user_name()
|
||||
else:
|
||||
nick = sys.argv[2]
|
||||
|
||||
app = QApplication([])
|
||||
app.setApplicationName("TCPChat Client")
|
||||
m = MainWindow()
|
||||
app.exec_()
|
||||
from client.main import main
|
||||
main(nick)
|
||||
elif str(sys.argv[1]).lower() in ['server', 's', '2']:
|
||||
from server import main
|
||||
main.receive()
|
||||
|
||||
Reference in New Issue
Block a user