From 5dcc9e225be08be1f52e62b49f7f11e6b747798c Mon Sep 17 00:00:00 2001 From: The many faced demon <154847721+themanyfaceddemon@users.noreply.github.com> Date: Wed, 2 Oct 2024 04:00:51 +0300 Subject: [PATCH] 0.3.1 (#22) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Исправлен баг при котором ответ от сервера про отключение считался за ничто и не обрабатывался - Исправлен баг когда в ряде случаев отключения колбек отключения клиента не срабатывал - Добавлена возможность через клиент взять его доступы --- DMBotNetwork/__init__.py | 2 +- DMBotNetwork/main/client.py | 43 +++++++++++++++--------- DMBotNetwork/main/server.py | 25 ++++++-------- DMBotNetwork/main/utils/response_code.py | 2 +- setup.py | 4 ++- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/DMBotNetwork/__init__.py b/DMBotNetwork/__init__.py index b848945..8ea3f3d 100644 --- a/DMBotNetwork/__init__.py +++ b/DMBotNetwork/__init__.py @@ -5,4 +5,4 @@ from .main.utils.server_db import ServerDB __all__ = ["Client", "Server", "ClUnit", "require_access", "ServerDB"] -__version__ = "0.3.0" +__version__ = "0.3.1" diff --git a/DMBotNetwork/main/client.py b/DMBotNetwork/main/client.py index 16ce2ea..4477944 100644 --- a/DMBotNetwork/main/client.py +++ b/DMBotNetwork/main/client.py @@ -35,6 +35,7 @@ class Client: _password: str = "owner_password" _use_registration: bool = False _content_path: Path = Path("") + _access: Dict[str, bool] = {} _callback_on_disconect: Optional[ Callable[[Optional[str]], Awaitable[None]] | Callable[[Optional[str]], None] @@ -151,6 +152,10 @@ def get_server_name(cls) -> str: def get_login(cls) -> str: return cls._login + @classmethod + def get_access(cls) -> Dict[str, bool]: + return cls._access.copy() + @classmethod def set_callback_on_disconect( cls, @@ -204,13 +209,26 @@ async def connect(cls, host, port) -> None: except Exception as err: logger.error(f"Error while connect to sever: {err}") - await cls.disconnect() + await cls.disconnect(str(err)) + + @classmethod + async def _on_disconect(cls, reason: Optional[str] = None) -> None: + if cls._callback_on_disconect is None: + return + + if inspect.iscoroutinefunction(cls._callback_on_disconect): + await cls._callback_on_disconect(reason) + + else: + cls._callback_on_disconect(reason) @classmethod - async def disconnect(cls) -> None: + async def disconnect(cls, reason: Optional[str] = None) -> None: async with cls._disconnect_lock: cls._state = ClientState.DISCONNECTED + await cls._on_disconect(reason) + if cls._writer: try: cls._writer.close() @@ -228,6 +246,7 @@ async def disconnect(cls) -> None: cls._writer = None cls._reader = None + cls._access = {} download_files = cls._content_path.glob("**/*.download") for file in download_files: @@ -237,6 +256,8 @@ async def disconnect(cls) -> None: @classmethod async def _server_handler(cls) -> None: + reason = None + try: while not cls._state & ClientState.DISCONNECTED: receive_package = await cls._receive_package() @@ -248,7 +269,6 @@ async def _server_handler(cls) -> None: if code == ResponseCode.DISCONNECT: reason = receive_package.pop("reason", None) - await cls._on_disconect(reason) break if code == ResponseCode.NET_REQ: @@ -287,18 +307,7 @@ async def _server_handler(cls) -> None: logger.error(str(err)) finally: - await cls.disconnect() - - @classmethod - async def _on_disconect(cls, reason: Optional[str] = None) -> None: - if cls._callback_on_disconect is None: - return - - if inspect.iscoroutinefunction(cls._callback_on_disconect): - await cls._callback_on_disconect(reason) - - else: - cls._callback_on_disconect(reason) + await cls.disconnect(reason) @classmethod def _log_handler(cls, code: int, receive_package: dict) -> None: @@ -339,6 +348,10 @@ async def _auth_handler(cls, code: int, receive_package: dict) -> None: cls._state = ClientState.AUTHORIZED cls._server_name = server_name + cls._access = await cls.req_get_data( + "get_access", None, login=cls._login + ) # По хорошему я get_access должен вынести в сервер, но мне похуй. # WARNING! + @classmethod async def _file_handler(cls, code: int, receive_package: dict) -> None: if code == ResponseCode.FIL_REQ: diff --git a/DMBotNetwork/main/server.py b/DMBotNetwork/main/server.py index 8f5b038..34b31b7 100644 --- a/DMBotNetwork/main/server.py +++ b/DMBotNetwork/main/server.py @@ -3,17 +3,8 @@ import logging from collections.abc import Callable from pathlib import Path -from typing import ( - Any, - Dict, - List, - Optional, - Type, - Union, - get_args, - get_origin, - get_type_hints, -) +from typing import (Any, Dict, List, Optional, Type, Union, get_args, + get_origin, get_type_hints) from .utils import ClUnit, ResponseCode, ServerDB @@ -221,12 +212,18 @@ async def stop(cls) -> None: logger.info("Server stop.") @classmethod - async def broadcast(cls, func_name: str, cl_units_dict: Optional[Dict[str, ClUnit]] = None, *args, **kwargs) -> None: + async def broadcast( + cls, + func_name: str, + cl_units_dict: Optional[Dict[str, ClUnit]] = None, + *args, + **kwargs, + ) -> None: tasks = [] - + if cl_units_dict is None: cl_units_dict = cls._cl_units - + for cl_unit in cl_units_dict.values(): func = getattr(cl_unit, func_name, None) if callable(func): diff --git a/DMBotNetwork/main/utils/response_code.py b/DMBotNetwork/main/utils/response_code.py index 69f44a9..7baa170 100644 --- a/DMBotNetwork/main/utils/response_code.py +++ b/DMBotNetwork/main/utils/response_code.py @@ -3,7 +3,7 @@ class ResponseCode(IntEnum): # Системные члены - DISCONNECT = 0 + DISCONNECT = 1 # Авторизация AUTH_REQ = 10 # Запрос авторизации от сервера diff --git a/setup.py b/setup.py index 71785ee..2408b41 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,10 @@ from setuptools import find_packages, setup +from DMBotNetwork import __version__ + setup( name="DMBotNetwork", - version="0.3.0", + version=__version__, packages=find_packages(), install_requires=["aiosqlite", "aiofiles", "bcrypt", "msgpack"], author="Angels And Demons dev team",