128
def config_flags_parser(config_flags):
129
"""Parses config flags string into dict.
131
This parsing method supports a few different formats for the config
132
flag values to be parsed:
134
1. A string in the simple format of key=value pairs, with the possibility
135
of specifying multiple key value pairs within the same string. For
136
example, a string in the format of 'key1=value1, key2=value2' will
142
2. A string in the above format, but supporting a comma-delimited list
143
of values for the same key. For example, a string in the format of
144
'key1=value1, key2=value3,value4,value5' will return a dict of:
147
'key2', 'value2,value3,value4'}
149
3. A string containing a colon character (:) prior to an equal
150
character (=) will be treated as yaml and parsed as such. This can be
151
used to specify more complex key value pairs. For example,
152
a string in the format of 'key1: subkey1=value1, subkey2=value2' will
155
{'key1', 'subkey1=value1, subkey2=value2'}
157
The provided config_flags string may be a list of comma-separated values
158
which themselves may be comma-separated list of values.
160
# If we find a colon before an equals sign then treat it as yaml.
161
# Note: limit it to finding the colon first since this indicates assignment
163
colon = config_flags.find(':')
164
equals = config_flags.find('=')
166
if colon < equals or equals < 0:
167
return yaml.safe_load(config_flags)
169
if config_flags.find('==') >= 0:
170
log("config_flags is not in expected format (key=value)", level=ERROR)
173
# strip the following from each value.
174
post_strippers = ' ,'
175
# we strip any leading/trailing '=' or ' ' from the string then
177
split = config_flags.strip(' =').split('=')
180
for i in range(0, limit - 1):
183
vindex = next.rfind(',')
184
if (i == limit - 2) or (vindex < 0):
187
value = next[:vindex]
192
# if this not the first entry, expect an embedded key.
193
index = current.rfind(',')
195
log("Invalid config value(s) at index %s" % (i), level=ERROR)
197
key = current[index + 1:]
200
flags[key.strip(post_strippers)] = value.rstrip(post_strippers)
205
127
class OSContextGenerator(object):
206
128
"""Base class for all context generators."""