Add valid_dict parameter to convert xml
valid_dict provides a way to set options through their key and value. This allows users to set options using more conventional naming schemes/values but the resulting XML will use the corresponding value from the dict for that key. Change-Id: I6574a5f33eecddb9b7927841f08600f3977f0ca6 Signed-off-by: Kien Ha <kienha9922@gmail.com>
This commit is contained in:
parent
49be71864a
commit
9c42d559d6
@ -496,14 +496,23 @@ def convert_mapping_to_xml(parent, data, mapping, fail_required=False):
|
|||||||
valid_options provides a way to check if the value the user input is from a
|
valid_options provides a way to check if the value the user input is from a
|
||||||
list of available options. When the user pass a value that is not supported
|
list of available options. When the user pass a value that is not supported
|
||||||
from the list, it raise an InvalidAttributeError.
|
from the list, it raise an InvalidAttributeError.
|
||||||
|
|
||||||
|
valid_dict provides a way to set options through their key and value. If
|
||||||
|
the user input corresponds to a key, the XML tag will use the key's value
|
||||||
|
for its element. When the user pass a value that there are no keys for,
|
||||||
|
it raise an InvalidAttributeError.
|
||||||
"""
|
"""
|
||||||
for elem in mapping:
|
for elem in mapping:
|
||||||
(optname, xmlname, val) = elem[:3]
|
(optname, xmlname, val) = elem[:3]
|
||||||
val = data.get(optname, val)
|
val = data.get(optname, val)
|
||||||
|
|
||||||
valid_options = []
|
valid_options = []
|
||||||
|
valid_dict = {}
|
||||||
if len(elem) == 4:
|
if len(elem) == 4:
|
||||||
valid_options = elem[3]
|
if type(elem[3]) is list:
|
||||||
|
valid_options = elem[3]
|
||||||
|
if type(elem[3]) is dict:
|
||||||
|
valid_dict = elem[3]
|
||||||
|
|
||||||
# Use fail_required setting to allow support for optional parameters
|
# Use fail_required setting to allow support for optional parameters
|
||||||
# we will phase this out in the future as we rework plugins so that
|
# we will phase this out in the future as we rework plugins so that
|
||||||
@ -517,10 +526,18 @@ def convert_mapping_to_xml(parent, data, mapping, fail_required=False):
|
|||||||
if val is None and fail_required is False:
|
if val is None and fail_required is False:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if valid_dict:
|
||||||
|
if val not in valid_dict:
|
||||||
|
raise InvalidAttributeError(optname, val, valid_dict.keys())
|
||||||
|
|
||||||
if valid_options:
|
if valid_options:
|
||||||
if val not in valid_options:
|
if val not in valid_options:
|
||||||
raise InvalidAttributeError(optname, val, valid_options)
|
raise InvalidAttributeError(optname, val, valid_options)
|
||||||
|
|
||||||
if type(val) == bool:
|
if type(val) == bool:
|
||||||
val = str(val).lower()
|
val = str(val).lower()
|
||||||
XML.SubElement(parent, xmlname).text = str(val)
|
|
||||||
|
if val in valid_dict:
|
||||||
|
XML.SubElement(parent, xmlname).text = str(valid_dict[val])
|
||||||
|
else:
|
||||||
|
XML.SubElement(parent, xmlname).text = str(val)
|
||||||
|
@ -70,7 +70,7 @@ class TestCaseTestHelpers(LoggingFixture, testtools.TestCase):
|
|||||||
required_mappings,
|
required_mappings,
|
||||||
fail_required=True)
|
fail_required=True)
|
||||||
|
|
||||||
# Test invalid user input
|
# Test invalid user input for list
|
||||||
user_input_root = XML.Element('testUserInput')
|
user_input_root = XML.Element('testUserInput')
|
||||||
user_input_data = yaml.load("user-input-string: bye")
|
user_input_data = yaml.load("user-input-string: bye")
|
||||||
valid_inputs = ['hello']
|
valid_inputs = ['hello']
|
||||||
@ -82,3 +82,29 @@ class TestCaseTestHelpers(LoggingFixture, testtools.TestCase):
|
|||||||
user_input_root,
|
user_input_root,
|
||||||
user_input_data,
|
user_input_data,
|
||||||
user_input_mappings)
|
user_input_mappings)
|
||||||
|
|
||||||
|
# Test invalid user input for dict
|
||||||
|
user_input_root = XML.Element('testUserInput')
|
||||||
|
user_input_data = yaml.load("user-input-string: later")
|
||||||
|
valid_inputs = {'hello': 'world'}
|
||||||
|
user_input_mappings = [('user-input-string', 'userInputString',
|
||||||
|
'user-input', valid_inputs)]
|
||||||
|
|
||||||
|
self.assertRaises(InvalidAttributeError,
|
||||||
|
convert_mapping_to_xml,
|
||||||
|
user_input_root,
|
||||||
|
user_input_data,
|
||||||
|
user_input_mappings)
|
||||||
|
|
||||||
|
# Test invalid key for dict
|
||||||
|
user_input_root = XML.Element('testUserInput')
|
||||||
|
user_input_data = yaml.load("user-input-string: world")
|
||||||
|
valid_inputs = {'hello': 'world'}
|
||||||
|
user_input_mappings = [('user-input-string', 'userInputString',
|
||||||
|
'user-input', valid_inputs)]
|
||||||
|
|
||||||
|
self.assertRaises(InvalidAttributeError,
|
||||||
|
convert_mapping_to_xml,
|
||||||
|
user_input_root,
|
||||||
|
user_input_data,
|
||||||
|
user_input_mappings)
|
||||||
|
Loading…
Reference in New Issue
Block a user