1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
def parse_info(text):
"""See test_parse.py."""
text = text.lstrip()
result = {}
if (text+':').index(':') > (text+'=').index('='):
# found a '=' before a ':' means that we have the new format
current = {0: ''}
indentation_prefix = None
for line in text.splitlines():
line = line.rstrip()
if not line:
continue
realline = line.lstrip()
indent = len(line) - len(realline)
#
# 'indentation_prefix' is set when the previous line was a [group]
if indentation_prefix is not None:
assert indent > max(current) # missing indent?
current[indent] = indentation_prefix
indentation_prefix = None
#
else:
# in case of dedent, must kill the extra items from 'current'
for n in current.keys():
if n > indent:
del current[n]
#
prefix = current[indent] # KeyError if bad dedent
#
if realline.startswith('[') and realline.endswith(']'):
indentation_prefix = prefix + realline[1:-1] + '.'
else:
# build the whole dotted key and evaluate the value
i = realline.index(' = ')
key = prefix + realline[:i]
value = realline[i+3:]
value = eval(value, {})
result[key] = value
#
else:
# old format
for line in text.splitlines():
i = line.index(':')
key = line[:i].strip()
value = line[i+1:].strip()
try:
value = int(value)
except ValueError:
if value in ('True', 'False', 'None'):
value = eval(value, {})
result[key] = value
#
return result
|