root / kamaki / clients / commissioning / utils / clijson.py @ 6d192774
History | View | Annotate | Download (3.4 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
import re |
37 |
|
38 |
keywords = set(['true', 'false', 'null']) |
39 |
unquoted = set('{}[]"\'0123456789') |
40 |
name_matcher = re.compile('^[\w @_.+-]+$', re.UNICODE)
|
41 |
|
42 |
|
43 |
def is_name(token): |
44 |
if name_matcher.match(token):
|
45 |
return 1 |
46 |
return 0 |
47 |
|
48 |
|
49 |
def quote(token, is_dict): |
50 |
if not token: |
51 |
return '""' |
52 |
|
53 |
if not is_name(token[0]): |
54 |
comma = ', ' if token[-1] not in '{[' else '' |
55 |
return token + comma
|
56 |
|
57 |
k, sep, v = token.partition('=')
|
58 |
if not sep or not v.strip('='): |
59 |
k, sep, v = token.partition(':')
|
60 |
|
61 |
if not sep: |
62 |
if is_name(token) and token not in keywords: |
63 |
token = '"' + token + '"' |
64 |
|
65 |
comma = ', ' if token[-1] not in '{[' else '' |
66 |
return token + comma
|
67 |
|
68 |
k = '"' + k + '"' |
69 |
is_dict.add(1)
|
70 |
|
71 |
if not v: |
72 |
v = '""'
|
73 |
else:
|
74 |
if v.isalnum() and not v.isdigit() and v not in keywords: |
75 |
v = '"' + v + '"' |
76 |
|
77 |
comma = ', ' if v[-1] not in '{[' else '' |
78 |
return k + ':' + v + comma |
79 |
|
80 |
|
81 |
def clijson(argv): |
82 |
tokens = argv |
83 |
is_dict = set()
|
84 |
|
85 |
strlist = [] |
86 |
append = strlist.append |
87 |
dictionary = 0
|
88 |
token_join = None
|
89 |
|
90 |
dictionary += 0
|
91 |
|
92 |
for t in tokens: |
93 |
t = t.strip() |
94 |
|
95 |
if strlist and t and t in '}]': |
96 |
strlist[-1] = strlist[-1].rstrip(', ') |
97 |
|
98 |
if token_join:
|
99 |
t = token_join + t |
100 |
token_join = None
|
101 |
elif t.endswith(':'): |
102 |
token_join = t |
103 |
continue
|
104 |
|
105 |
t = quote(t, is_dict) |
106 |
append(t) |
107 |
|
108 |
if not strlist: |
109 |
return 'null' |
110 |
|
111 |
if strlist[0][0] not in '{[': |
112 |
strlist[-1] = strlist[-1].rstrip(', ') |
113 |
o, e = '{}' if is_dict else '[]' |
114 |
strlist = [o] + strlist + [e] |
115 |
|
116 |
if strlist[-1][-1] in ']}': |
117 |
strlist[-1] = strlist[-1].rstrip(',') |
118 |
return ''.join(strlist) |
119 |
|
120 |
|
121 |
if __name__ == '__main__': |
122 |
from sys import argv |
123 |
|
124 |
print clijson(argv[1:]) |