summaryrefslogtreecommitdiff
path: root/materials/ratdb.py
blob: 2b14761e9dc9e86eb3e14f818924d5af2d97145c (plain)
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
import re
import json

trailing_comma = re.compile(r',[ ]*\]')
trailing_double_marker = re.compile(r'(\d+[.]?)(d)([^+\-\d])')
trailing_period = re.compile(r'(\d+\.)(\W)')
double_marker = re.compile(r'(\d+)(d)([+-]?\d)')
comments = re.compile(r'//.*')
key = re.compile(r'([ ]*)(\w*)([ ]*:.*)')

def load(f):
    s = f.read()
    s = trailing_comma.sub(']', s)
    s = trailing_double_marker.sub(r'\1\3', s)
    s = double_marker.sub(r'\1e\3', s)
    s = comments.sub('', s)
    s = key.sub(r'\1"\2"\3', s)
    s = trailing_period.sub(r'\g<1>0\2', s)

    db = {}
    for data in re.findall('{.*?}', s, flags=re.DOTALL):
        d = json.loads(data)

        name = d['name']
        index = d['index']

        if name not in db:
            db[name] = {}

        if index not in db[name]:
            db[name][index] = {}

        db[name][index].update(d)

    return db