| 1 |
|
| 2 |
|
| 3 |
def parse_info(text): |
| 4 |
"""See test_parse.py.""" |
| 5 |
text = text.lstrip() |
| 6 |
result = {} |
| 7 |
if (text+':').index(':') > (text+'=').index('='): |
| 8 |
|
| 9 |
current = {0: ''} |
| 10 |
indentation_prefix = None |
| 11 |
for line in text.splitlines(): |
| 12 |
line = line.rstrip() |
| 13 |
if not line: |
| 14 |
continue |
| 15 |
realline = line.lstrip() |
| 16 |
indent = len(line) - len(realline) |
| 17 |
|
| 18 |
|
| 19 |
if indentation_prefix is not None: |
| 20 |
assert indent > max(current) |
| 21 |
current[indent] = indentation_prefix |
| 22 |
indentation_prefix = None |
| 23 |
|
| 24 |
else: |
| 25 |
|
| 26 |
for n in current.keys(): |
| 27 |
if n > indent: |
| 28 |
del current[n] |
| 29 |
|
| 30 |
prefix = current[indent] |
| 31 |
|
| 32 |
if realline.startswith('[') and realline.endswith(']'): |
| 33 |
indentation_prefix = prefix + realline[1:-1] + '.' |
| 34 |
else: |
| 35 |
|
| 36 |
i = realline.index(' = ') |
| 37 |
key = prefix + realline[:i] |
| 38 |
value = realline[i+3:] |
| 39 |
value = eval(value, {}) |
| 40 |
result[key] = value |
| 41 |
|
| 42 |
else: |
| 43 |
|
| 44 |
for line in text.splitlines(): |
| 45 |
i = line.index(':') |
| 46 |
key = line[:i].strip() |
| 47 |
value = line[i+1:].strip() |
| 48 |
try: |
| 49 |
value = int(value) |
| 50 |
except ValueError: |
| 51 |
if value in ('True', 'False', 'None'): |
| 52 |
value = eval(value, {}) |
| 53 |
result[key] = value |
| 54 |
|
| 55 |
return result |