Skip to content

Commit

Permalink
Move config to yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
bart0003 committed Dec 20, 2023
1 parent d99fbe9 commit 3f18312
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion core/message/validators/base_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion core/message/validators/required_fields_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions smart_kit/start_points/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
28 changes: 15 additions & 13 deletions smart_kit/start_points/base_main_loop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8

from typing import Type, Iterable
from typing import Type, Tuple, List
import asyncio
import signal

Expand All @@ -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:
Expand All @@ -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={
Expand All @@ -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
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions smart_kit/utils/dynamic_import.py
Original file line number Diff line number Diff line change
@@ -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]))

Check warning

Code scanning / Semgrep

Semgrep Finding: python.lang.security.audit.non-literal-import.non-literal-import Warning

Untrusted user input in importlib.import\_module() function allows an attacker to load arbitrary code. Avoid dynamic values in importlib.import\_module() or use a whitelist to prevent running untrusted code.
return getattr(module, groups[-1])

0 comments on commit 3f18312

Please sign in to comment.