#wip: Fixing Dispatches and Controllers.

This commit is contained in:
KyMAN 2026-07-15 07:18:46 +02:00
parent a76e63aa39
commit 8d88c077b2
7 changed files with 38 additions and 18 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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"])

View File

@ -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

View File

@ -26,4 +26,9 @@ class DatabaseResponseModel:
if len(table) == 1:
if variable in table[0]:
self.variables[variable] = table[0][variable]
break
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

View File

@ -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)

View File

@ -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))
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))