diff --git a/core/message/validators/base_validator.py b/core/message/validators/base_validator.py index 1e68ad81..05cbc5d0 100644 --- a/core/message/validators/base_validator.py +++ b/core/message/validators/base_validator.py @@ -18,7 +18,7 @@ class ValidationException(Exception): class BaseMessageValidator(ABC): VALIDATOR_EXCEPTION: T = ValidationException - def __init__(self, on_exception: OnException = "raise"): + def __init__(self, on_exception: OnException = "raise", *args, **kwargs): self.on_exception = on_exception def validate(self, message: SmartAppMessage): diff --git a/core/message/validators/required_fields_validator.py b/core/message/validators/required_fields_validator.py index 3916d852..0d9c968f 100644 --- a/core/message/validators/required_fields_validator.py +++ b/core/message/validators/required_fields_validator.py @@ -23,7 +23,8 @@ class RequiredFieldsValidator(BaseMessageValidator): def __init__(self, required_fields: Optional[Dict[Optional[str], Iterable[str]]] = None, field_types: Optional[Dict[str, type]] = None, - on_exception: OnException = "raise"): + on_exception: OnException = "raise", + *args, **kwargs): super().__init__(on_exception) if required_fields is None: required_fields = {} diff --git a/smart_kit/message/validators/base_validator_with_resources.py b/smart_kit/message/validators/base_validator_with_resources.py index 81ad6da1..626d15f4 100644 --- a/smart_kit/message/validators/base_validator_with_resources.py +++ b/smart_kit/message/validators/base_validator_with_resources.py @@ -8,7 +8,8 @@ class BaseMessageValidatorWithResources(BaseMessageValidator, ABC): - def __init__(self, resources: Optional[SmartAppResources] = None, on_exception: OnException = "raise"): + def __init__(self, resources: Optional[SmartAppResources] = None, on_exception: OnException = "raise", + *args, **kwargs): super().__init__(on_exception) self._resources: Optional[SmartAppResources] = None self.resources = resources diff --git a/smart_kit/start_points/app.py b/smart_kit/start_points/app.py index 2cb7f8ed..4408e855 100644 --- a/smart_kit/start_points/app.py +++ b/smart_kit/start_points/app.py @@ -21,10 +21,8 @@ def run(app_config): log("FINISHED MODEL CREATE", level="WARNING") log("START MAIN_LOOP CREATE", level="WARNING") - loop = app_config.MAIN_LOOP( - model, app_config.USER, app_config.PARAMETRIZER, app_config.POSTPROCESSOR_MAIN_LOOP, - settings, app_config.TO_MSG_VALIDATORS, app_config.FROM_MSG_VALIDATORS, - ) + loop = app_config.MAIN_LOOP(model, app_config.USER, app_config.PARAMETRIZER, + app_config.POSTPROCESSOR_MAIN_LOOP, settings) log("FINISHED MAIN_LOOP CREATE", level="WARNING") loop.run() logging.shutdown() diff --git a/smart_kit/start_points/base_main_loop.py b/smart_kit/start_points/base_main_loop.py index 6ac68809..f72b46d0 100644 --- a/smart_kit/start_points/base_main_loop.py +++ b/smart_kit/start_points/base_main_loop.py @@ -1,6 +1,6 @@ # coding=utf-8 -from typing import Type, Iterable +from typing import Type, Tuple, List import asyncio import signal @@ -15,9 +15,10 @@ from core.model.base_user import BaseUser from core.basic_models.parametrizers.parametrizer import BasicParametrizer from core.message.validators.base_validator import BaseMessageValidator -from smart_kit.message.validators.base_validator_with_resources import BaseMessageValidatorWithResources +from smart_kit.configs.settings import Settings from smart_kit.start_points.postprocess import PostprocessMainLoop from smart_kit.models.smartapp_model import SmartAppModel +from smart_kit.utils.dynamic_import import dynamic_import_object class BaseMainLoop: @@ -27,9 +28,7 @@ def __init__( user_cls: Type[BaseUser], parametrizer_cls: Type[BasicParametrizer], postprocessor_cls: Type[PostprocessMainLoop], - settings, - to_msg_validators: Iterable[BaseMessageValidator] = (), - from_msg_validators: Iterable[BaseMessageValidator] = (), + settings: Settings, *args, **kwargs ): log("%(class_name)s.__init__ started.", params={ @@ -48,16 +47,9 @@ def __init__( self.postprocessor = postprocessor_cls() self.db_adapter = self.get_db() self.is_work = True - self.to_msg_validators: Iterable[BaseMessageValidator] = to_msg_validators - self.from_msg_validators: Iterable[BaseMessageValidator] = from_msg_validators - - for validators in (self.from_msg_validators, self.to_msg_validators): - for v in validators: - if isinstance(v, BaseMessageValidatorWithResources): - v.resources = self.model.resources + self.from_msg_validators, self.to_msg_validators = self.get_message_validators() template_settings = self.settings["template_settings"] - save_tries = template_settings.get("user_save_collisions_tries", 0) self.user_save_check_for_collisions = True if save_tries > 0 else False @@ -74,6 +66,16 @@ def __init__( level="ERROR", exc_info=True) raise + def get_message_validators(self) -> Tuple[List[BaseMessageValidator], List[BaseMessageValidator]]: + validators = self.settings["template_settings"].get("validators", {}) + + from_validators = [dynamic_import_object(e["class"])(resources=self.model.resources, **e["params"]) + for e in validators.get("from", [])] + to_validators = [dynamic_import_object(e["class"])(resources=self.model.resources, **e["params"]) + for e in validators.get("to", [])] + + return from_validators, to_validators + def get_db(self): db_adapter = db_adapter_factory(self.settings["template_settings"].get("db_adapter", {})) if not db_adapter.IS_ASYNC: diff --git a/smart_kit/utils/dynamic_import.py b/smart_kit/utils/dynamic_import.py new file mode 100644 index 00000000..f4161285 --- /dev/null +++ b/smart_kit/utils/dynamic_import.py @@ -0,0 +1,8 @@ +from typing import Any +from importlib import import_module + + +def dynamic_import_object(x: str) -> Any: + groups = x.split(".") + module = import_module(".".join(groups[:-1])) + return getattr(module, groups[-1])