Skip to content

Commit 20ca0b8

Browse files
committed
Moving template_dict in dku_utils
1 parent 506afe6 commit 20ca0b8

2 files changed

Lines changed: 28 additions & 28 deletions

File tree

python-lib/dku_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,30 @@ def get_value_from_path(dictionary, path, default=None, can_raise=True):
5656
else:
5757
return None
5858
return ret
59+
60+
61+
def template_dict(dictionnary, **kwargs):
62+
""" Recurses into dictionnary and replace template {{keys}} with the matching values present in the kwargs dictionnary"""
63+
ret = dict.copy(dictionnary)
64+
for key in ret:
65+
if isinstance(ret[key], dict):
66+
ret[key] = template_dict(ret[key], **kwargs)
67+
if is_string(ret[key]):
68+
ret[key] = format_template(ret[key], **kwargs)
69+
return ret
70+
71+
72+
def format_template(template, **kwargs):
73+
""" Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
74+
if template is None:
75+
return None
76+
formated = template
77+
for key in kwargs:
78+
replacement = kwargs.get(key, "")
79+
formated = formated.replace("{{{{{}}}}}".format(key), str(replacement))
80+
return formated
81+
82+
83+
def is_string(data):
84+
data_type = type(data).__name__
85+
return data_type in ["str", "unicode"]

python-lib/rest_api_client.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,13 @@
33
from pagination import Pagination
44
from safe_logger import SafeLogger
55
from loop_detector import LoopDetector
6-
from dku_utils import get_dku_key_values
6+
from dku_utils import get_dku_key_values, template_dict, format_template
77
from dku_constants import DKUConstants
88

99

1010
logger = SafeLogger("api-connect plugin", forbiden_keys=["token", "password"])
1111

1212

13-
def template_dict(dictionnary, **kwargs):
14-
""" Recurses into dictionnary and replace template {{keys}} with the matching values present in the kwargs dictionnary"""
15-
ret = dict.copy(dictionnary)
16-
for key in ret:
17-
if isinstance(ret[key], dict):
18-
ret[key] = template_dict(ret[key], **kwargs)
19-
if is_string(ret[key]):
20-
ret[key] = format_template(ret[key], **kwargs)
21-
return ret
22-
23-
24-
def format_template(template, **kwargs):
25-
""" Replace {{keys}} elements in template with the matching value in the kwargs dictionnary"""
26-
if template is None:
27-
return None
28-
formated = template
29-
for key in kwargs:
30-
replacement = kwargs.get(key, "")
31-
formated = formated.replace("{{{{{}}}}}".format(key), str(replacement))
32-
return formated
33-
34-
35-
def is_string(data):
36-
data_type = type(data).__name__
37-
return data_type in ["str", "unicode"]
38-
39-
4013
class RestAPIClientError(ValueError):
4114
pass
4215

0 commit comments

Comments
 (0)