63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, Self
|
|
from Interfaces.Application.AnPInterface import AnPInterface
|
|
from Abstracts.DispatchAbstract import DispatchAbstract
|
|
from Utils.Common import Common
|
|
from Utils.Checks import Check
|
|
|
|
class DispatchesManager:
|
|
|
|
def __init__(self:Self, anp:AnPInterface) -> None:
|
|
|
|
self.anp:AnPInterface = anp
|
|
self.__dispatches:dict[str, DispatchAbstract] = {}
|
|
|
|
self.update()
|
|
|
|
def update(self:Self) -> None:
|
|
|
|
key:str
|
|
|
|
for key in ("default_dispatches_files", "dispatches_files", "default_dispatches", "dispatches"):
|
|
self.add(self.anp.settings.get(key), True)
|
|
|
|
def reset(self:Self) -> None:
|
|
|
|
self.__dispatches = {}
|
|
|
|
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, dispatch in subinputs.items():
|
|
if Common.is_mark_key(key) and dispatch is None:
|
|
continue
|
|
|
|
DispatchClass:type[DispatchAbstract]|None
|
|
|
|
if Check.is_string(dispatch):
|
|
DispatchClass = self.anp.models.get(DispatchAbstract, dispatch)
|
|
elif issubclass(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"])
|
|
else:
|
|
continue
|
|
|
|
if DispatchClass and (
|
|
overwrite or key not in self.__dispatches
|
|
):
|
|
self.__dispatches[key] = DispatchClass(
|
|
self.anp, dispatch if Check.is_dictionary(dispatch) else
|
|
None)
|
|
|
|
def execute(self:Self, key:str, method:str, *arguments:list[Any|None]) -> bool:
|
|
if key in self.__dispatches and hasattr(self.__dispatches[key], method):
|
|
getattr(self.__dispatches[key], method)(*arguments)
|
|
return True
|
|
return False |