Common: Rename and improve freeze_lists_in_dict function

This commit is contained in:
vakarisz 2022-07-26 17:29:43 +03:00
parent 0ab90aeaf5
commit eaeb78a821
3 changed files with 8 additions and 7 deletions

View File

@ -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))

View File

@ -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

View File

@ -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