Created
June 8, 2014 18:45
-
-
Save anossov/650080336854b0ed51e0 to your computer and use it in GitHub Desktop.
Config parser for github.com/acedrew/ubnt-mfi-py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from collections import defaultdict | |
def parse_config(conf): | |
def Tree(): | |
return defaultdict(Tree) | |
data = Tree() | |
for line in conf.splitlines(): | |
if not line: | |
continue | |
path, val = line.split('=') | |
fields = path.split('.') | |
prop = fields.pop() | |
obj = data | |
for f in fields: | |
if f.isdigit(): | |
items = obj.setdefault('items', []) | |
idx = int(f) - 1 | |
while len(items) < idx + 1: | |
items.append(Tree()) | |
obj = items[idx] | |
else: | |
obj = obj[f] | |
obj[prop] = val | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment