aboutsummaryrefslogtreecommitdiff
path: root/export_json.py
blob: e4f0d3d128a9465584f00cb37a6598ad3b55bea0 (plain) (blame)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import sys

import export
import re

def filename(s, extension):
    s_ = re.sub('[^A-Za-z0-9_+.-]', '_', s)
    assert s_, s
    return s_ + '.' +extension

def output(path, xs):
    with open(path, 'w') as f:
            f.write(xs)

def main():
    data = export.PkgData()
    everything = {}

    for (name, templates) in export.export_all(data):
        page = []

        try:
            # Force errors.
            templates = list(templates)
        except export.ExportFailure, e:
            export.warn('export failed: %s: %s' % (name.encode('utf-8').strip(), e.message.encode('utf-8').strip()))

        for template in templates:
            tname = template.name
            values = dict(template.values)
            page.append((tname, values))

        if page != []:
            #name=page[0][1]['Name']
            print name
            fn=filename(name, 'json')
            data = json.dumps(page, sort_keys=True, indent=4, separators=(',', ': '))
            output('output/'+fn, data)

if __name__ == '__main__':
    main()