X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=juju%2Fconstraints.py;h=998862d02508499dd28d0f20983e1f5858498213;hb=5cf8d29d1ec5ff9360920dd831ff6ead145e3d11;hp=43a80a358e16e662b1ee16f042b70cb18c06cf4d;hpb=ea587bcc29128652a98b0a7c1327dd3336f4f484;p=osm%2FN2VC.git diff --git a/juju/constraints.py b/juju/constraints.py index 43a80a3..998862d 100644 --- a/juju/constraints.py +++ b/juju/constraints.py @@ -11,6 +11,9 @@ # constraints keys are valid, and that we can successfully dump the # constraints dict to json. # +# Once https://bugs.launchpad.net/juju/+bug/1645402 is addressed, this +# module should be deprecated. +# import re @@ -18,6 +21,7 @@ import re MEM = re.compile('^[1-9][0-9]*[MGTP]$') # Multiplication factors to get Megabytes +# https://github.com/juju/juju/blob/master/constraints/constraints.go#L666 FACTORS = { "M": 1, "G": 1024, @@ -25,15 +29,23 @@ FACTORS = { "P": 1024 * 1024 * 1024 } +SNAKE1 = re.compile(r'(.)([A-Z][a-z]+)') +SNAKE2 = re.compile('([a-z0-9])([A-Z])') + + def parse(constraints): """ Constraints must be expressed as a string containing only spaces and key value pairs joined by an '='. """ - if constraints is None: + if not constraints: return None + if type(constraints) is dict: + # Fowards compatibilty: already parsed + return constraints + constraints = { normalize_key(k): normalize_value(v) for k, v in [ s.split("=") for s in constraints.split(" ")]} @@ -45,6 +57,11 @@ def normalize_key(key): key = key.strip() key = key.replace("-", "_") # Our _client lib wants "_" in place of "-" + + # Convert camelCase to snake_case + key = SNAKE1.sub(r'\1_\2', key) + key = SNAKE2.sub(r'\1_\2', key).lower() + return key @@ -61,4 +78,7 @@ def normalize_value(value): values = [normalize_value(v) for v in values] return values + if value.isdigit(): + return int(value) + return value