2 # Module that parses constraints
4 # The current version of juju core expects the client to take
5 # constraints given in the form "mem=10G foo=bar" and parse them into
6 # json that looks like {"mem": 10240, "foo": "bar"}. This module helps us
7 # accomplish that task.
9 # We do not attempt to duplicate the checking done in
10 # client/_client.py:Value here. That class will verify that the
11 # constraints keys are valid, and that we can successfully dump the
12 # constraints dict to json.
14 # Once https://bugs.launchpad.net/juju/+bug/1645402 is addressed, this
15 # module should be deprecated.
20 # Matches on a string specifying memory size
21 MEM
= re
.compile('^[1-9][0-9]*[MGTP]$')
23 # Multiplication factors to get Megabytes
24 # https://github.com/juju/juju/blob/master/constraints/constraints.go#L666
29 "P": 1024 * 1024 * 1024
32 LIST_KEYS
= {'tags', 'spaces'}
34 SNAKE1
= re
.compile(r
'(.)([A-Z][a-z]+)')
35 SNAKE2
= re
.compile('([a-z0-9])([A-Z])')
38 def parse(constraints
):
40 Constraints must be expressed as a string containing only spaces
41 and key value pairs joined by an '='.
47 if type(constraints
) is dict:
48 # Fowards compatibilty: already parsed
53 normalize_list_value(v
) if k
in LIST_KEYS
else
55 ) for k
, v
in [s
.split("=") for s
in constraints
.split(" ")]}
60 def normalize_key(key
):
63 key
= key
.replace("-", "_") # Our _client lib wants "_" in place of "-"
65 # Convert camelCase to snake_case
66 key
= SNAKE1
.sub(r
'\1_\2', key
)
67 key
= SNAKE2
.sub(r
'\1_\2', key
).lower()
72 def normalize_value(value
):
76 # Translate aliases to Megabytes. e.g. 1G = 10240
77 return int(value
[:-1]) * FACTORS
[value
[-1:]]
85 def normalize_list_value(value
):
86 values
= value
.strip().split(',')
87 return [normalize_value(value
) for value
in values
]