Statistics
| Branch: | Tag: | Revision:

root / commissioning / utils / clijson.py @ 50b21832

History | View | Annotate | Download (1 kB)

1
#!/usr/bin/env python
2

    
3
from re import split as re_split
4

    
5
keywords = set(['true', 'false', 'null'])
6

    
7
def clijson(argv):
8
    s = ','.join(argv)
9
    tokens = re_split('([^\w@_.-]+)', s)
10

    
11
    strlist = ['{']
12
    append = strlist.append
13
    quoting = 0
14

    
15
    for t in tokens:
16
        t = t.strip()
17
        if not t:
18
            continue
19

    
20
        if quoting:
21
            append(t)
22
            continue
23

    
24
        count = t.count('"')
25
        quoting = (quoting + count) & 1
26

    
27
        if t.startswith('"'):
28
            continue
29

    
30
        if not t[0].isalpha():
31
            if '=' in t:
32
                t = t.replace('=', ':')
33
                quote = 1
34
        elif t not in keywords:
35
            t = '"' + t + '"'
36

    
37
        append(t)
38

    
39
    append('}')
40
    z = len(strlist)
41
    if z <= 2:
42
        strlist = ['null']
43
    elif len(argv) < 2 and strlist[1][0] in '{[' or strlist[1] in keywords:
44
        strlist[0] = ''
45
        strlist[-1] = ''
46

    
47
    return ''.join(strlist)
48

    
49

    
50
if __name__ == '__main__':
51
    from sys import argv
52

    
53
    print clijson(argv[1:])
54