107 lines
4.0 KiB
Python
107 lines
4.0 KiB
Python
from typing import Self, Any, Optional, Type
|
|
from AetherCommon.Utils.Common import Common
|
|
from AetherModelsServer.Abstracts.ControllerAbstract import ControllerAbstract
|
|
from AetherModelsServer.Controllers.ModelController import ModelController
|
|
from AetherModelsServer.Interfaces.Application.AetherModelsServerInterface import AetherModelsServerInterface
|
|
|
|
class ControllersManager:
|
|
|
|
ams:AetherModelsServerInterface
|
|
|
|
def __init__(self:Self, ams:AetherModelsServerInterface, inputs:Optional[Any] = None) -> None:
|
|
|
|
object.__setattr__(self, "ams", ams)
|
|
self.__types:dict[str, type[ControllerAbstract]] = {}
|
|
self.__controllers:dict[str, ControllerAbstract] = {}
|
|
|
|
self.load_types(Common.get_value("controller_types", inputs))
|
|
self.load(Common.get_value("controllers", inputs))
|
|
|
|
def register_type(self:Self, key:str, Class:Type[ControllerAbstract]) -> None:
|
|
"""Permite registrar nuevos tipos de servidor externamente."""
|
|
if not issubclass(Class, ControllerAbstract):
|
|
raise TypeError(f"La clase debe heredar de ControllerAbstract.")
|
|
self.__types[key.lower()] = Class
|
|
|
|
def load_types(self:Self, inputs:Any) -> None:
|
|
if isinstance(inputs, type) and issubclass(inputs, ControllerAbstract):
|
|
self.__types[inputs.key] = inputs
|
|
elif isinstance(inputs, (list, set, tuple)):
|
|
|
|
subinputs:Any
|
|
|
|
for subinputs in inputs:
|
|
self.load_types(subinputs)
|
|
elif isinstance(inputs, dict):
|
|
|
|
key:str
|
|
Class:Type[ControllerAbstract]
|
|
|
|
for key, Class in inputs.items():
|
|
self.register_type(key, Class)
|
|
|
|
def register(self:Self, key:str, controller:ControllerAbstract) -> ControllerAbstract:
|
|
if not isinstance(controller, ControllerAbstract):
|
|
raise TypeError(f"El objeto debe heredar de ControllerAbstract, recibido: {type(controller).__name__}")
|
|
|
|
self.__controllers[key.lower()] = controller
|
|
|
|
return controller
|
|
|
|
def create(self:Self, key:str, inputs:Optional[Any] = None) -> ControllerAbstract:
|
|
|
|
_type:str = str(Common.get_value(["type", "_type"], inputs)).lower()
|
|
|
|
if _type not in self.__types:
|
|
raise ValueError(f"Tipo de controlador no soportado: '{_type}'. Disponibles: {list(self.__types.keys())}")
|
|
return self.register(self.__types[_type](self.ams, key, inputs))
|
|
|
|
def get(self:Self, key:str) -> ControllerAbstract:
|
|
clean_key:str = key.lower()
|
|
if clean_key not in self.__controllers:
|
|
raise KeyError(f"Controlador '{key}' no encontrado en el gestor.")
|
|
return self.__controllers[clean_key]
|
|
|
|
def has(self:Self, key:str) -> bool:
|
|
return key.lower() in self.__controllers
|
|
|
|
def get_all(self:Self) -> dict[str, ControllerAbstract]:
|
|
return dict(self.__controllers)
|
|
|
|
@property
|
|
def model(self:Self) -> ModelController:
|
|
instance:ControllerAbstract = self.get("model")
|
|
return instance # type: ignore
|
|
|
|
def load(self:Self, inputs:Optional[Any]) -> None:
|
|
if inputs is None:
|
|
return
|
|
|
|
container:Any = Common.get_value("servers", inputs, inputs)
|
|
|
|
if isinstance(container, ControllerAbstract):
|
|
self.__servers[container.id] = container
|
|
|
|
elif isinstance(container, dict):
|
|
if "host" in container and ("id" in container or "identifier" in container):
|
|
self.create(Common.get_value(["id", "identifier"], container, 0), container)
|
|
else:
|
|
|
|
key:str
|
|
configuration:Any
|
|
|
|
for key, configuration in container.items():
|
|
if isinstance(configuration, dict):
|
|
self.create(key, configuration)
|
|
|
|
elif isinstance(container, (list, tuple, set)):
|
|
|
|
item:Any
|
|
|
|
for item in container:
|
|
if isinstance(item, dict):
|
|
|
|
key:int|str|None = Common.get_value("key", item, None)
|
|
|
|
if key is not None:
|
|
self.create(key, item) |