diff options
| author | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-10-15 22:16:03 +0000 |
|---|---|---|
| committer | Tarmac <> | 2010-10-15 22:16:03 +0000 |
| commit | f36cfc4d6c0557f440572f375923c76cd7d7b1df (patch) | |
| tree | c12c69784ae74d1ba57cb92e1d795f35506b7582 /nova/api | |
| parent | cbdba97b592536ae1fbad15d4374684b9ffaa94f (diff) | |
| parent | 1156100be1d358f5ffae58c6d892e0724f4d153c (diff) | |
This branch converts incoming data to the api into the proper type.
Diffstat (limited to 'nova/api')
| -rw-r--r-- | nova/api/ec2/apirequest.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/nova/api/ec2/apirequest.py b/nova/api/ec2/apirequest.py index a87c21fb3..3c8651582 100644 --- a/nova/api/ec2/apirequest.py +++ b/nova/api/ec2/apirequest.py @@ -44,6 +44,41 @@ def _underscore_to_xmlcase(str): res = _underscore_to_camelcase(str) return res[:1].lower() + res[1:] +def _try_convert(value): + """Return a non-string if possible""" + if value == 'None': + return None + if value == 'True': + return True + if value == 'False': + return False + valueneg = value[1:] if value[0] == '-' else value + if valueneg == '0': + return 0 + if valueneg == '': + return value + if valueneg[0] == '0': + if valueneg[1] in 'xX': + return int(value,16) + elif valueneg[1] in 'bB': + return int(value,2) + else: + try: + return int(value,8) + except ValueError: + pass + try: + return int(value) + except ValueError: + pass + try: + return float(value) + except ValueError: + pass + try: + return complex(value) + except ValueError: + return value class APIRequest(object): def __init__(self, controller, action): @@ -66,6 +101,10 @@ class APIRequest(object): for key, value in kwargs.items(): parts = key.split(".") key = _camelcase_to_underscore(parts[0]) + if isinstance(value, str) or isinstance(value, unicode): + # NOTE(vish): Automatically convert strings back + # into their respective values + value = _try_convert(value) if len(parts) > 1: d = args.get(key, {}) d[parts[1]] = value @@ -73,6 +112,7 @@ class APIRequest(object): args[key] = value for key in args.keys(): + # NOTE(vish): Turn numeric dict keys into lists if isinstance(args[key], dict): if args[key] != {} and args[key].keys()[0].isdigit(): s = args[key].items() |
