forked from aleksmaricic/print-pivotal-cards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONSoup.py
191 lines (159 loc) · 4.73 KB
/
JSONSoup.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import simplejson
import copy
import pdb
class JSONSoup:
def __init__(self):
self.parent = None
self.children = []
def findAll(self, attrs):
first_key_matches = self.matchAllKey(attrs.keys()[0])
parents_of_first_key_matches = [m.parent for m in first_key_matches]
matches = []
for potential_match in parents_of_first_key_matches:
raw_potential_match = potential_match.get_raw()
ismatch = True
if not set(attrs.keys()).issubset(raw_potential_match.keys()):
ismatch = False
continue
for k,v in attrs.iteritems():
if raw_potential_match[k] <> v:
ismatch = False
break
if ismatch:
matches.append(potential_match)
if len(matches) == 0:
return JSONSoup() # An empty soup
else:
results = JSONSoup()
results.children = matches
return results
def matchKey(self, key):
def dig(obj):
if isinstance(obj, JSONSoup):
obj = obj.children
for _v in dig(obj):
yield _v
elif isinstance(obj, dict):
if key in obj.keys():
yield obj[key]
else:
for v in obj.values():
for _v in dig(v):
yield _v
elif isinstance(obj, list):
for v in obj:
for _v in dig(v):
yield _v
else:
return # Something like a string or an int
new_soup = JSONSoup()
new_soup.children = [o for o in dig(self.children)]
return new_soup
def matchAllKey(self, key):
def dig(obj):
if isinstance(obj, JSONSoup):
obj = obj.children
for _v in dig(obj):
yield _v
elif isinstance(obj, dict):
if key in obj.keys():
yield obj[key]
for v in obj.values():
for _v in dig(v):
yield _v
elif isinstance(obj, list):
for v in obj:
for _v in dig(v):
yield _v
else:
return # Something like a string or an int
new_soup = JSONSoup()
new_soup.children = [o for o in dig(self.children)]
return new_soup
def matchValue(self, value):
def dig(obj):
if isinstance(obj, JSONSoup):
obj = obj.children
if isinstance(obj, dict):
for v in obj.values():
if value in dig(v):
yield obj
else:
for _v in dig(v):
yield _v
elif isinstance(obj, list):
for v in obj:
if value in dig(v):
yield obj
else:
for _v in dig(v):
yield _v
elif obj == value:
yield obj
def makeSoup(obj):
if not isinstance(obj, JSONSoup):
new_soup = JSONSoup()
new_soup.children = obj
return new_soup
else:
return obj
new_soup = JSONSoup()
objs = [o for o in dig(self.children)]
if objs == None:
objs = []
new_soup.children = map(makeSoup, objs)
return new_soup
def load(self, stuff):
if isinstance(stuff, JSONSoup):
self.__dict__ = stuff.__dict__.copy()
elif isinstance(stuff, dict) or isinstance(stuff, list):
self.load_obj(stuff)
elif isinstance(stuff, str):
self.loads(stuff)
else:
raise TypeError("Cannot load objects of this type")
def loads(self, s):
o = simplejson.loads(s)
self.load_obj(o)
def load_obj(self, obj):
def replace(obj, parent):
new_soup = JSONSoup()
new_soup.children = obj
new_soup.parent = parent
if isinstance(obj, dict):
for k,v in new_soup.children.iteritems():
new_soup.children[k] = replace(v, new_soup)
elif isinstance(obj, list):
new_soup.children = map(lambda c: replace(c, new_soup), new_soup.children)
return new_soup
new_soup = replace(copy.deepcopy(obj), None)
self.__dict__ = new_soup.__dict__
def __str__(self):
return self.get_raw().__str__()
def get_raw(self):
def dig(obj):
if isinstance(obj, JSONSoup):
obj = obj.children
obj = dig(obj)
elif isinstance(obj, dict):
for k,v in obj.iteritems():
obj[k] = dig(v)
elif isinstance(obj, list):
obj = map(dig, obj)
return obj
return dig(copy.deepcopy(self))
def __iter__(self):
for child in self.children:
yield child
def __setitem__(self, k, v):
self.children[k] = v
def prettify(self):
return simplejson.dumps(self.get_raw(), indent=2)
def __getitem__(self, k):
return self.children[k]
def __len__(self):
return len(self.children)
def __eq__(self, other):
if not isinstance(other, JSONSoup):
return False
return self.get_raw() == other.get_raw()