CXCV/Python/Managers/ModelsManager.py

42 lines
1.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Self, Any, TypeVar
from inspect import isclass
from Interfaces.Application.CXCVInterface import CXCVInterface
T = TypeVar('T')
class ModelsManager:
def __init__(self:Self, cxcv:CXCVInterface) -> None:
self.cxcv:CXCVInterface = cxcv
self.__models:dict[str, Any] = {}
def get(self:Self, Type:type[T], model:str|type[T]) -> type[T]|None:
return (
model if isclass(model) and issubclass(model, Type) else
self.__models[model] if (
model in self.__models and
issubclass(self.__models[model], Type)
) else
None)
def add(self:Self,
Type:type[T],
inputs:dict[str, Any]|list[Any|None]|tuple[Any|None, ...]|str,
overwrite:bool = False
) -> None:
subinputs:dict[str, Any]
for subinputs in self.cxcv.files.load_json(inputs):
key:str
Model:type[T]
for key, Model in subinputs.items():
if issubclass(Model, Type) and (
overwrite or key not in self.__models
):
self.__models[key] = Model