summaryrefslogtreecommitdiff
path: root/ast2json/strings2json.py
blob: 0736a10c821d679e0e15d2e860f6656dacf09ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python3

import json
import sys

def strings2json(ast):
    ret = {}
    if ast[0]['_type'] != 'Init': raise TypeError('obj does not start with Init, wrong file?')
    for string in ast[0]['block'][0]['code']['ast']['body'][1:]:
        target = string['targets'][0]
        if target['_type'] == 'Attribute':
            name = string['targets'][0]['attr']
            value = string['value']
            vtype = value['_type']
            if vtype == 'Str':
                ret[name] = value['s']
    return ret

with open(sys.argv[1], 'r') as f:
    output = strings2json(json.load(f))

json.dump(output, open(sys.argv[2], 'w'), separators=(',', ':'))