Skip to content

Instantly share code, notes, and snippets.

@anossov
Created June 8, 2014 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anossov/650080336854b0ed51e0 to your computer and use it in GitHub Desktop.
Save anossov/650080336854b0ed51e0 to your computer and use it in GitHub Desktop.
Config parser for github.com/acedrew/ubnt-mfi-py
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