From eaeb78a8218d4f44d3327075315af4cca904dc94 Mon Sep 17 00:00:00 2001 From: vakarisz Date: Tue, 26 Jul 2022 17:29:43 +0300 Subject: [PATCH] Common: Rename and improve freeze_lists_in_dict function --- monkey/common/agent_configuration/agent_configuration.py | 6 +++--- monkey/common/agent_configuration/utils.py | 4 ++-- monkey/common/utils/code_utils.py | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/monkey/common/agent_configuration/agent_configuration.py b/monkey/common/agent_configuration/agent_configuration.py index ee8ac48b6..e3e6caccf 100644 --- a/monkey/common/agent_configuration/agent_configuration.py +++ b/monkey/common/agent_configuration/agent_configuration.py @@ -6,7 +6,7 @@ from typing import Any, Mapping, Tuple from marshmallow import Schema, fields from marshmallow.exceptions import MarshmallowError -from ..utils.code_utils import freeze_lists_in_dict +from ..utils.code_utils import freeze_lists_in_mapping from .agent_sub_configuration_schemas import ( CustomPBAConfigurationSchema, PluginConfigurationSchema, @@ -60,7 +60,7 @@ class AgentConfiguration: try: config_dict = AgentConfigurationSchema().load(config_mapping) - config_dict = freeze_lists_in_dict(config_dict) + config_dict = freeze_lists_in_mapping(config_dict) return AgentConfiguration(**config_dict) except MarshmallowError as err: raise InvalidConfigurationError(str(err)) @@ -77,7 +77,7 @@ class AgentConfiguration: """ try: config_dict = AgentConfigurationSchema().loads(config_json) - config_dict = freeze_lists_in_dict(config_dict) + config_dict = freeze_lists_in_mapping(config_dict) return AgentConfiguration(**config_dict) except MarshmallowError as err: raise InvalidConfigurationError(str(err)) diff --git a/monkey/common/agent_configuration/utils.py b/monkey/common/agent_configuration/utils.py index 21816076a..3470e57a1 100644 --- a/monkey/common/agent_configuration/utils.py +++ b/monkey/common/agent_configuration/utils.py @@ -1,13 +1,13 @@ from functools import wraps from typing import Callable -from common.utils.code_utils import freeze_lists_in_dict +from common.utils.code_utils import freeze_lists_in_mapping def freeze_lists(function: Callable): @wraps(function) def wrapper(self, data, **kwargs): - data = freeze_lists_in_dict(data) + data = freeze_lists_in_mapping(data) return function(self, data, **kwargs) return wrapper diff --git a/monkey/common/utils/code_utils.py b/monkey/common/utils/code_utils.py index 7a5c34975..52fdcbaac 100644 --- a/monkey/common/utils/code_utils.py +++ b/monkey/common/utils/code_utils.py @@ -1,4 +1,5 @@ import queue +from collections.abc import MutableSequence from typing import Any, List, MutableMapping, TypeVar T = TypeVar("T") @@ -50,8 +51,8 @@ def del_key(mapping: MutableMapping[T, Any], key: T): pass -def freeze_lists_in_dict(mapping: MutableMapping[str, Any]) -> MutableMapping[str, Any]: +def freeze_lists_in_mapping(mapping: MutableMapping[str, Any]) -> MutableMapping[str, Any]: for key, value in mapping.items(): - if type(value) == list: + if isinstance(value, MutableSequence): mapping[key] = tuple(value) return mapping