Merge "Raise user-friendly exceptions in str2dict"

This commit is contained in:
Jenkins
2015-06-12 06:15:32 +00:00
committed by Gerrit Code Review
2 changed files with 16 additions and 3 deletions

View File

@@ -105,9 +105,15 @@ def str2dict(strdict):
:param strdict: key1=value1,key2=value2
"""
if not strdict:
return {}
return dict([kv.split('=', 1) for kv in strdict.split(',')])
result = {}
if strdict:
for kv in strdict.split(','):
key, sep, value = kv.partition('=')
if not sep:
msg = _("invalid key-value '%s', expected format: key=value")
raise argparse.ArgumentTypeError(msg % kv)
result[key] = value
return result
def http_log_req(_logger, args, kwargs):

View File

@@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import testtools
from neutronclient.common import exceptions
@@ -42,6 +44,11 @@ class TestUtils(testtools.TestCase):
expected = {}
self.assertEqual(expected, utils.str2dict(input_str))
def test_invalid_string_to_dictionary(self):
input_str = 'invalid'
self.assertRaises(argparse.ArgumentTypeError,
utils.str2dict, input_str)
def test_get_dict_item_properties(self):
item = {'name': 'test_name', 'id': 'test_id'}
fields = ('name', 'id')