CXCV/Python/Managers/ControllersManager.py

56 lines
1.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Self, Any, TypeVar
from Interfaces.Application.CXCVInterface import CXCVInterface
from Abstracts.ControllerAbstract import ControllerAbstract
from Utils.Utils import Utils
T = TypeVar("T")
class ControllersManager:
def __init__(self:Self, cxcv:CXCVInterface) -> None:
key:str
self.cxcv:CXCVInterface = cxcv
self.__controllers:dict[str, ControllerAbstract] = {}
for key in ("default_controllers_models", "controllers_models"):
self.cxcv.models.add(ControllerAbstract, self.cxcv.settings.get(key), True)
for key in (
"default_controllers_files", "default_controllers",
"controllers_files", "controllers"
):
self.add(self.cxcv.settings.get(key), True)
def get(self:Self, Type:type[T], controller:str|T) -> T|None:
return (
controller if isinstance(controller, Type) else
self.__controllers[controller] if (
controller in self.__controllers and
isinstance(self.__controllers[controller], Type)
) else
None)
def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None:
subinputs:dict[str, ControllerAbstract]
for subinputs in self.cxcv.files.load_json(inputs):
key:str
subinputs:dict[str, Any|None]
for key, subinputs in subinputs.items():
if overwrite or key not in self.__controllers:
ControllerModel:type[ControllerAbstract]|None = self.cxcv.models.get(
ControllerAbstract,
Utils.get_value("model", subinputs, key)
)
if ControllerModel is not None:
self.__controllers[key] = ControllerModel(self.cxcv, key, subinputs)