From 8d88c077b2b17e7d2e3beb31dcc6b621c0fdc953 Mon Sep 17 00:00:00 2001 From: KyMAN <0kyman0@gmail.com> Date: Wed, 15 Jul 2026 07:18:46 +0200 Subject: [PATCH] #wip: Fixing Dispatches and Controllers. --- Python/AnP/Drivers/FilesDriver.py | 2 +- Python/AnP/Managers/ControllersManager.py | 8 ++++---- Python/AnP/Managers/DatabasesManager.py | 2 +- Python/AnP/Managers/DispatchesManager.py | 10 +++++----- Python/AnP/Models/DatabaseResponseModel.py | 7 ++++++- Python/AnP/Models/HashModel.py | 18 ++++++++++++++---- Python/AnP/Utils/Checks.py | 9 +++++++-- 7 files changed, 38 insertions(+), 18 deletions(-) diff --git a/Python/AnP/Drivers/FilesDriver.py b/Python/AnP/Drivers/FilesDriver.py index 935ad1a..03465b2 100644 --- a/Python/AnP/Drivers/FilesDriver.py +++ b/Python/AnP/Drivers/FilesDriver.py @@ -11,7 +11,7 @@ from os import ( listdir as list_directory_items ) from shutil import rmtree as remove_directory -from AnP.Abstracts.FilesAbstract import FilesAbstract, abstractmethod +from AnP.Abstracts.FilesAbstract import FilesAbstract from AnP.Utils.Checks import Check class FilesDriver(FilesAbstract): diff --git a/Python/AnP/Managers/ControllersManager.py b/Python/AnP/Managers/ControllersManager.py index 11314bd..929ea73 100644 --- a/Python/AnP/Managers/ControllersManager.py +++ b/Python/AnP/Managers/ControllersManager.py @@ -48,7 +48,7 @@ class ControllersManager: if Check.is_string(controller): ControllerClass = self.anp.models.get(ControllerAbstract, controller) - elif issubclass(controller, ControllerAbstract): + elif Check.is_class(controller, ControllerAbstract): ControllerClass = controller elif Check.is_dictionary(controller) and "type" in controller and Check.is_string(controller["type"]): ControllerClass = self.anp.models.get(ControllerAbstract, controller["type"]) @@ -58,9 +58,9 @@ class ControllersManager: if ControllerClass and ( overwrite or key not in self.__controllers ): - self.__controllers[key] = ControllerClass( - self.anp, controller if Check.is_dictionary(controller) else - None) + self.__controllers[key] = ControllerClass(self.anp, ( + controller if Check.is_dictionary(controller) else + None)) def execute(self:Self, key:str, method:str, request:RequestModel) -> bool: if key in self.__controllers and hasattr(self.__controllers[key], method): diff --git a/Python/AnP/Managers/DatabasesManager.py b/Python/AnP/Managers/DatabasesManager.py index b22aba6..32a1bd5 100644 --- a/Python/AnP/Managers/DatabasesManager.py +++ b/Python/AnP/Managers/DatabasesManager.py @@ -54,7 +54,7 @@ class DatabasesManager: if Check.is_string(database): DatabaseClass = self.anp.models.get(DatabaseAbstract, database) - elif isclass(database) and issubclass(database, DatabaseAbstract): + elif Check.is_class(database, DatabaseAbstract): DatabaseClass = database elif Check.is_dictionary(database) and "type" in database and Check.is_string(database["type"]): DatabaseClass = self.anp.models.get(DatabaseAbstract, database["type"]) diff --git a/Python/AnP/Managers/DispatchesManager.py b/Python/AnP/Managers/DispatchesManager.py index 7023b00..c413755 100644 --- a/Python/AnP/Managers/DispatchesManager.py +++ b/Python/AnP/Managers/DispatchesManager.py @@ -54,7 +54,7 @@ class DispatchesManager: if Check.is_string(dispatch): DispatchClass = self.anp.models.get(DispatchAbstract, dispatch) - elif issubclass(dispatch, DispatchAbstract): + elif Check.is_class(dispatch, DispatchAbstract): DispatchClass = dispatch elif Check.is_dictionary(dispatch) and "type" in dispatch and Check.is_string(dispatch["type"]): DispatchClass = self.anp.models.get(DispatchAbstract, dispatch["type"]) @@ -64,11 +64,11 @@ class DispatchesManager: if DispatchClass and ( overwrite or key not in self.__dispatches[via] ): - self.__dispatches[via][key] = DispatchClass( - self.anp, dispatch if Check.is_dictionary(dispatch) else - None) + self.__dispatches[via][key] = DispatchClass(self.anp, ( + dispatch if Check.is_dictionary(dispatch) else + None)) - def execute(self:Self, via:str, key:str, method:str, *arguments:list[Any|None]) -> DatabaseResponseModel|None: + def execute(self:Self, via:str, key:str, method:str, *arguments:list[Any|None]) -> Any|None: if via in self.__dispatches and key in self.__dispatches[via] and hasattr(self.__dispatches[via][key], method): return getattr(self.__dispatches[via][key], method)(*arguments) return None \ No newline at end of file diff --git a/Python/AnP/Models/DatabaseResponseModel.py b/Python/AnP/Models/DatabaseResponseModel.py index bcb1c76..6ba5a1f 100644 --- a/Python/AnP/Models/DatabaseResponseModel.py +++ b/Python/AnP/Models/DatabaseResponseModel.py @@ -26,4 +26,9 @@ class DatabaseResponseModel: if len(table) == 1: if variable in table[0]: self.variables[variable] = table[0][variable] - break \ No newline at end of file + break + + def get(self:Self, key:str, table:int = 0, row:int = 0) -> Any|None: + if table < len(self.tables) and row < len(self.tables[table]) and key in self.tables[table][row]: + return self.tables[table][row][key] + return None \ No newline at end of file diff --git a/Python/AnP/Models/HashModel.py b/Python/AnP/Models/HashModel.py index bfe25b5..9a53df3 100644 --- a/Python/AnP/Models/HashModel.py +++ b/Python/AnP/Models/HashModel.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from hashlib import md5, sha1, sha256, blake2b +from hashlib import md5, sha1, sha256, blake2b, _Hash as Hash from zlib import crc32 from typing import Self, Protocol from AnP.Utils.Checks import Check @@ -23,6 +23,16 @@ class CRC32HashAlgorithm(HashAlgorithm): class HashModel(): + @staticmethod + def algorithms() -> dict[str, type[HashAlgorithm|Hash]]: + return { + "md5" : md5, + "sha1" : sha1, + "sha256" : sha256, + "blake2b" : blake2b, + "crc32" : CRC32HashAlgorithm + } + def __init__(self:Self, data:bytes = b"", chunk_size:int = 4096) -> None: self.__chunk_size:int = chunk_size @@ -31,9 +41,9 @@ class HashModel(): self.sha256:str = "" self.blake2b:str = "" self.crc32:str = "" - self.__hashes:list[HashAlgorithm] = ["md5", "sha1", "sha256", "blake2b", "crc32"] - self.__algorithms:list[HashAlgorithm] = [ - method() for method in (md5, sha1, sha256, blake2b, CRC32HashAlgorithm) + self.__hashes:list[str] = list(self.algorithms().keys()) + self.__algorithms:list[HashAlgorithm|Hash] = [ + method() for method in list(self.algorithms().values()) ] len(data) and self.update(data) diff --git a/Python/AnP/Utils/Checks.py b/Python/AnP/Utils/Checks.py index a14f4a9..761b7bb 100644 --- a/Python/AnP/Utils/Checks.py +++ b/Python/AnP/Utils/Checks.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from typing import Any, Self, Sequence +from typing import Any, Self, Sequence, Optional from re import Pattern as REPattern from os.path import isfile as is_file from json import loads as json_decode import datetime +from inspect import isclass from AnP.Utils.Patterns import RE class Check: @@ -89,4 +90,8 @@ class Check: @staticmethod def is_date(item:Any|None) -> bool: - return isinstance(item, (datetime.date, datetime.datetime)) \ No newline at end of file + return isinstance(item, (datetime.date, datetime.datetime)) + + @staticmethod + def is_class(item:Any, Type:Optional[type[Self]] = None) -> bool: + return isclass(item) and (Type is None or issubclass(item, Type)) \ No newline at end of file