64 lines
2.3 KiB
Python
64 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, Self, Sequence
|
|
from Interfaces.Application.AnPInterface import AnPInterface
|
|
from Abstracts.ControllerAbstract import ControllerAbstract
|
|
from Models.RequestModel import RequestModel
|
|
from Utils.Common import Common
|
|
from Utils.Checks import Check
|
|
|
|
class ControllersManager:
|
|
|
|
def __init__(self:Self, anp:AnPInterface) -> None:
|
|
|
|
self.anp:AnPInterface = anp
|
|
self.__controllers:dict[str, ControllerAbstract] = {}
|
|
|
|
self.update()
|
|
|
|
def update(self:Self) -> None:
|
|
|
|
key:str
|
|
|
|
for key in ("default_controllers_files", "controllers_files", "default_controllers", "controllers"):
|
|
self.add(self.anp.settings.get(key), True)
|
|
|
|
def reset(self:Self) -> None:
|
|
|
|
self.__controllers = {}
|
|
|
|
self.update()
|
|
|
|
def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None:
|
|
|
|
subinputs:dict[str, Any|None]
|
|
|
|
for subinputs in Common.load_json(inputs, True):
|
|
for key, controller in subinputs.items():
|
|
if Common.is_mark_key(key) and controller is None:
|
|
continue
|
|
|
|
ControllerClass:type[ControllerAbstract]|None
|
|
|
|
if Check.is_string(controller):
|
|
ControllerClass = self.anp.models.get(ControllerAbstract, controller)
|
|
elif issubclass(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"])
|
|
else:
|
|
continue
|
|
|
|
if ControllerClass and (
|
|
overwrite or key not in self.__controllers
|
|
):
|
|
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):
|
|
getattr(self.__controllers[key], method)(request)
|
|
return True
|
|
return False |