2 # Library to parse 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.
17 # Matches on a string specifying memory size
18 MEM
= re
.compile('^[1-9][0-9]*[MGTP]$')
20 # Multiplication factors to get Megabytes
25 "P": 1024 * 1024 * 1024
28 def parse(constraints
):
30 Constraints must be expressed as a string containing only spaces
31 and key value pairs joined by an '='.
34 if constraints
is None:
38 normalize_key(k
): normalize_value(v
) for k
, v
in [
39 s
.split("=") for s
in constraints
.split(" ")]}
44 def normalize_key(key
):
47 key
= key
.replace("-", "_") # Our _client lib wants "_" in place of "-"
51 def normalize_value(value
):
55 # Translate aliases to Megabytes. e.g. 1G = 10240
56 return int(value
[:-1]) * FACTORS
[value
[-1:]]
60 values
= value
.split(",")
61 values
= [normalize_value(v
) for v
in values
]