Refactor methods in utils related to dicts

* The previous versions of these methods were too specific for the
  general utils module. Now they are more generic thereby less
  confusing.

Change-Id: Ifa8bbf0cc8a63bf1de1142dc8c52b1f8ad3958c5
This commit is contained in:
Renat Akhmerov 2017-03-13 19:07:47 +07:00
parent db503fea47
commit 97311a6c04
4 changed files with 42 additions and 44 deletions

View File

@ -42,7 +42,7 @@ class ActionSpec(base.BaseSpec):
self._tags = data.get('tags', []) self._tags = data.get('tags', [])
self._base = data['base'] self._base = data['base']
self._base_input = data.get('base-input', {}) self._base_input = data.get('base-input', {})
self._input = utils.get_input_dict(data.get('input', [])) self._input = utils.get_dict_from_entries(data.get('input', []))
self._output = data.get('output') self._output = data.get('output')
self._base, _input = self._parse_cmd_and_input(self._base) self._base, _input = self._parse_cmd_and_input(self._base)

View File

@ -54,7 +54,7 @@ class WorkflowSpec(base.BaseSpec):
self._description = data.get('description') self._description = data.get('description')
self._tags = data.get('tags', []) self._tags = data.get('tags', [])
self._type = data['type'] if 'type' in data else 'direct' self._type = data['type'] if 'type' in data else 'direct'
self._input = utils.get_input_dict(data.get('input', [])) self._input = utils.get_dict_from_entries(data.get('input', []))
self._output = data.get('output', {}) self._output = data.get('output', {})
self._output_on_error = data.get('output-on-error', {}) self._output_on_error = data.get('output-on-error', {})
self._vars = data.get('vars', {}) self._vars = data.get('vars', {})

View File

@ -100,25 +100,26 @@ class UtilsTest(base.BaseTest):
self.assertEqual([B, C, D], list(utils.iter_subclasses(A))) self.assertEqual([B, C, D], list(utils.iter_subclasses(A)))
def test_get_input_dict(self): def test_get_dict_from_entries(self):
input = ['param1', {'param2': 2}] input = ['param1', {'param2': 2}]
input_dict = utils.get_input_dict(input) input_dict = utils.get_dict_from_entries(input)
self.assertIn('param1', input_dict) self.assertIn('param1', input_dict)
self.assertIn('param2', input_dict) self.assertIn('param2', input_dict)
self.assertEqual(2, input_dict.get('param2')) self.assertEqual(2, input_dict.get('param2'))
self.assertIs(input_dict.get('param1'), utils.NotDefined) self.assertIs(input_dict.get('param1'), utils.NotDefined)
def test_get_input_dict_from_input_string(self): def test_get_input_dict_from_string(self):
input_string = 'param1, param2=2, param3="var3"' self.assertDictEqual(
input_dict = utils.get_dict_from_string(input_string) {
'param1': utils.NotDefined,
'param2': 2,
'param3': 'var3'
},
utils.get_dict_from_string('param1, param2=2, param3="var3"')
)
self.assertIn('param1', input_dict) self.assertDictEqual({}, utils.get_dict_from_string(''))
self.assertIn('param2', input_dict)
self.assertIn('param3', input_dict)
self.assertEqual(2, input_dict.get('param2'))
self.assertEqual('var3', input_dict.get('param3'))
self.assertIs(input_dict.get('param1'), utils.NotDefined)
def test_paramiko_to_private_key(self): def test_paramiko_to_private_key(self):
self.assertRaises( self.assertRaises(

View File

@ -343,53 +343,50 @@ class NotDefined(object):
pass pass
def get_dict_from_string(input_string, delimiter=','): def get_dict_from_string(string, delimiter=','):
# TODO(rakhmerov): Why is it here? This module is too generic. if not string:
if not input_string:
return {} return {}
raw_inputs = input_string.split(delimiter) kv_dicts = []
inputs = [] for kv_pair_str in string.split(delimiter):
kv_str = kv_pair_str.strip()
kv_list = kv_str.split('=')
for raw in raw_inputs: if len(kv_list) > 1:
input = raw.strip()
name_value = input.split('=')
if len(name_value) > 1:
try: try:
value = json.loads(name_value[1]) value = json.loads(kv_list[1])
except ValueError: except ValueError:
value = name_value[1] value = kv_list[1]
inputs += [{name_value[0]: value}] kv_dicts += [{kv_list[0]: value}]
else: else:
inputs += [name_value[0]] kv_dicts += [kv_list[0]]
return get_input_dict(inputs) return get_dict_from_entries(kv_dicts)
def get_input_dict(inputs): def get_dict_from_entries(entries):
# TODO(rakhmerov): Why is it here? This module is too generic. """Transforms a list of entries into dictionary.
# TODO(rakhmerov): Move it to the spec.
"""Transform input list to dictionary. :param entries: A list of entries.
If an entry is a dictionary the method simply updates the result
Ensure every input param has a default value(it will be a NotDefined dictionary with its content.
object if it's not provided). If an entry is not a dict adds {entry, NotDefined} into the result.
""" """
input_dict = {}
for x in inputs: result = {}
if isinstance(x, dict):
input_dict.update(x) for e in entries:
if isinstance(e, dict):
result.update(e)
else: else:
# NOTE(xylan): we put a NotDefined class here as the value of # NOTE(xylan): we put NotDefined here as the value of
# param without value specified, to distinguish from the valid # param without value specified, to distinguish from
# values such as None, ''(empty string), etc. # the valid values such as None, ''(empty string), etc.
input_dict[x] = NotDefined result[e] = NotDefined
return input_dict return result
def get_process_identifier(): def get_process_identifier():