root / kamaki / clients / commissioning / utils / clijson.py @ 54069d1b
History | View | Annotate | Download (3.3 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 |
def is_name(token): |
43 |
if name_matcher.match(token):
|
44 |
return 1 |
45 |
return 0 |
46 |
|
47 |
def quote(token, is_dict): |
48 |
if not token: |
49 |
return '""' |
50 |
|
51 |
if not is_name(token[0]): |
52 |
comma = ', ' if token[-1] not in '{[' else '' |
53 |
return token + comma
|
54 |
|
55 |
k, sep, v = token.partition('=')
|
56 |
if not sep or not v.strip('='): |
57 |
k, sep, v = token.partition(':')
|
58 |
|
59 |
if not sep: |
60 |
if is_name(token) and token not in keywords: |
61 |
token = '"' + token + '"' |
62 |
|
63 |
comma = ', ' if token[-1] not in '{[' else '' |
64 |
return token + comma
|
65 |
|
66 |
k = '"' + k + '"' |
67 |
is_dict.add(1)
|
68 |
|
69 |
if not v: |
70 |
v = '""'
|
71 |
else:
|
72 |
if v.isalnum() and not v.isdigit() and v not in keywords: |
73 |
v = '"' + v + '"' |
74 |
|
75 |
comma = ', ' if v[-1] not in '{[' else '' |
76 |
return k + ':' + v + comma |
77 |
|
78 |
|
79 |
def clijson(argv): |
80 |
tokens = argv |
81 |
is_dict = set()
|
82 |
|
83 |
strlist = [] |
84 |
append = strlist.append |
85 |
dictionary = 0
|
86 |
token_join = None
|
87 |
|
88 |
for t in tokens: |
89 |
t = t.strip() |
90 |
|
91 |
if strlist and t and t in '}]': |
92 |
strlist[-1] = strlist[-1].rstrip(', ') |
93 |
|
94 |
if token_join:
|
95 |
t = token_join + t |
96 |
token_join = None
|
97 |
elif t.endswith(':'): |
98 |
token_join = t |
99 |
continue
|
100 |
|
101 |
t = quote(t, is_dict) |
102 |
append(t) |
103 |
|
104 |
if not strlist: |
105 |
return 'null' |
106 |
|
107 |
if strlist[0][0] not in '{[': |
108 |
strlist[-1] = strlist[-1].rstrip(', ') |
109 |
o, e = '{}' if is_dict else '[]' |
110 |
strlist = [o] + strlist + [e] |
111 |
|
112 |
if strlist[-1][-1] in ']}': |
113 |
strlist[-1] = strlist[-1].rstrip(',') |
114 |
return ''.join(strlist) |
115 |
|
116 |
|
117 |
if __name__ == '__main__': |
118 |
from sys import argv |
119 |
|
120 |
print clijson(argv[1:]) |
121 |
|