Skip to content

Commit

Permalink
replace deepmerge by custom code (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov-aws authored Nov 13, 2023
1 parent 956eaef commit 2a338de
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
12 changes: 5 additions & 7 deletions cid/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import yaml
import click
import requests
from deepmerge import always_merger
from botocore.exceptions import ClientError, NoCredentialsError, CredentialRetrievalError


from cid import utils
from cid.base import CidBase
from cid.plugin import Plugin
from cid.utils import get_parameter, get_parameters, set_parameters, unset_parameter, get_yesno_parameter, cid_print, isatty
from cid.utils import get_parameter, get_parameters, set_parameters, unset_parameter, get_yesno_parameter, cid_print, isatty, merge_objects
from cid.helpers.account_map import AccountMap
from cid.helpers import Athena, CUR, Glue, QuickSight, Dashboard, Dataset, Datasource, csv2view, Organizations
from cid.helpers.quicksight.template import Template as CidQsTemplate
Expand Down Expand Up @@ -195,10 +194,9 @@ def __loadPlugins(self) -> dict:
print(f"\t{ep.name} loaded")
plugins.update({ep.value: plugin})
try:
self.resources = always_merger.merge(
self.resources, plugin.provides())
self.resources = merge_objects(self.resources, plugin.provides(), depth=1)
except AttributeError:
pass
logger.warning(f'Failed to load {ep.name}')
print('\n')
logger.info('Finished loading plugins')
return plugins
Expand Down Expand Up @@ -317,7 +315,7 @@ def load_resource_file(self, source):
except Exception as exc:
logger.warning(f'Failed to load resources from {source}: {exc}')
return
self.resources = always_merger.merge(self.resources, resources)
self.resources = merge_objects(self.resources, resources, depth=1)

def load_catalog(self, catalog_url):
''' load additional resources from catalog
Expand Down Expand Up @@ -372,7 +370,7 @@ def get_template_parameters(self, parameters: dict, param_prefix: str='', others
)
else:
raise CidCritical(f'Unknown parameter type for "{key}". Must be a string or a dict with value or with default key')
return always_merger.merge(params, others or {})
return merge_objects(params, others or {}, depth=1)


@command
Expand Down
25 changes: 25 additions & 0 deletions cid/test/python/test_merge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from cid.utils import merge_objects


def test_merge_objects():
""" make sure the merge works with depth
"""

obj1 = {'a': {'b': {'c1': 1}, 'c': 3}}
obj2 = {'a': {'b': {'c2': 1}, 'd': {'e': 2}}}

assert merge_objects(obj1, obj2, depth=0) == obj2
assert merge_objects(obj1, obj2, depth=1) == {
'a': {
'b': { 'c2': 1},
'c': 3,
'd': {'e': 2}
}
}
assert merge_objects(obj1, obj2, depth=2) == {
'a': {
'b': { 'c1': 1, 'c2': 1},
'c': 3,
'd': {'e': 2}
}
}
21 changes: 20 additions & 1 deletion cid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,23 @@ def ago(time):
if dur > 0:
unit = unit[:-dur] if dur == 1 else unit # De-pluralize if duration is 1 ('1 day' vs '2 days')
return '%s %s ago' % (dur, unit)
return 'just now'
return 'just now'


def merge_objects(obj1, obj2, depth=2):
""" merging objects with a depth
unit tests: cid/test/python/test_merge.py
"""
if isinstance(obj1, dict) and isinstance(obj2, dict):
result = obj1.copy()
for key, value in obj2.items():
if depth > 0 and key in result and isinstance(result[key], (dict, list)) and isinstance(value, (dict, list)):
result[key] = merge_objects(result[key], value, depth - 1)
else:
result[key] = value
return result
elif isinstance(obj1, list) and isinstance(obj2, list):
return obj1 + obj2
else:
return obj2 # If types don't match or if one of them is not a dict or list, prefer the second object.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
boto3>=1.26
Click>=8.0
deepmerge
PyYAML
requests
six>=1.15
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ install_requires =
setuptools
boto3>=1.26
Click>=8.0
deepmerge
PyYAML
requests
tzlocal>=4.0
Expand Down

0 comments on commit 2a338de

Please sign in to comment.