forked from jimkeir/EDProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathijpython.py
More file actions
232 lines (211 loc) · 6.57 KB
/
ijpython.py
File metadata and controls
232 lines (211 loc) · 6.57 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
'''
Pure-python parsing backend.
'''
from __future__ import unicode_literals
import decimal
import re
from ijson import common
# BUFSIZE = 16 * 1024
BUFSIZE = 4 * 1024
LEXEME_RE = re.compile(r'[a-z0-9eE\.\+-]+|\S')
# my_lexer = None
class UnexpectedSymbol(common.JSONError):
def __init__(self, symbol, pos):
super(UnexpectedSymbol, self).__init__(
'Unexpected symbol %r at %d' % (symbol, pos)
)
def Lexer(f, buf_size=BUFSIZE):
buf = f.read(buf_size)
pos = 0
discarded = 0
while True:
match = LEXEME_RE.search(buf, pos)
if match:
lexeme = match.group()
if lexeme == '"':
pos = match.start()
start = pos + 1
while True:
try:
end = buf.index('"', start)
escpos = end - 1
while buf[escpos] == '\\':
escpos -= 1
if (end - escpos) % 2 == 0:
start = end + 1
else:
break
except ValueError:
data = f.read(buf_size)
if not data:
print buf, pos, end
print buf[pos:end + 1]
raise common.IncompleteJSONError('Incomplete string lexeme')
buf += data
yield discarded + pos, buf[pos:end + 1]
pos = end + 1
else:
while match.end() == len(buf):
data = f.read(buf_size)
if not data:
break
buf += data
match = LEXEME_RE.search(buf, pos)
lexeme = match.group()
yield discarded + match.start(), lexeme
pos = match.end()
else:
data = f.read(buf_size)
if not data:
break
discarded += len(buf)
buf = data
pos = 0
def unescape(s):
start = 0
result = ''
while start < len(s):
pos = s.find('\\', start)
if pos == -1:
if start == 0:
return s
result += s[start:]
break
result += s[start:pos]
pos += 1
esc = s[pos]
if esc == 'u':
result += unichr(int(s[pos + 1:pos + 5], 16))
pos += 4
elif esc == 'b':
result += '\b'
elif esc == 'f':
result += '\f'
elif esc == 'n':
result += '\n'
elif esc == 'r':
result += '\r'
elif esc == 't':
result += '\t'
else:
result += esc
start = pos + 1
return result
def parse_value(lexer, symbol=None, pos=0):
try:
if symbol is None:
pos, symbol = next(lexer)
if symbol == 'null':
yield ('null', None)
elif symbol == 'true':
yield ('boolean', True)
elif symbol == 'false':
yield ('boolean', False)
elif symbol == '[':
for event in parse_array(lexer):
yield event
elif symbol == '{':
for event in parse_object(lexer):
yield event
elif symbol[0] == '"':
yield ('string', unescape(symbol[1:-1]))
else:
try:
yield ('number', common.number(symbol))
except decimal.InvalidOperation:
raise UnexpectedSymbol(symbol, pos)
except StopIteration:
raise common.IncompleteJSONError('Incomplete JSON data')
def parse_array(lexer):
yield ('start_array', None)
try:
pos, symbol = next(lexer)
if symbol != ']':
while True:
for event in parse_value(lexer, symbol, pos):
yield event
pos, symbol = next(lexer)
if symbol == ']':
break
if symbol != ',':
raise UnexpectedSymbol(symbol, pos)
pos, symbol = next(lexer)
yield ('end_array', None)
except StopIteration:
raise common.IncompleteJSONError('Incomplete JSON data')
def parse_object(lexer):
yield ('start_map', None)
try:
pos, symbol = next(lexer)
if symbol != '}':
while True:
if symbol[0] != '"':
raise UnexpectedSymbol(symbol, pos)
yield ('map_key', unescape(symbol[1:-1]))
pos, symbol = next(lexer)
if symbol != ':':
raise UnexpectedSymbol(symbol, pos)
for event in parse_value(lexer, None, pos):
yield event
pos, symbol = next(lexer)
if symbol == '}':
break
if symbol != ',':
raise UnexpectedSymbol(symbol, pos)
pos, symbol = next(lexer)
yield ('end_map', None)
except StopIteration:
raise common.IncompleteJSONError('Incomplete JSON data')
# def basic_parse(f, buf_size=BUFSIZE):
# '''
# Iterator yielding unprefixed events.
#
# Parameters:
#
# - file: a readable file-like object with JSON input
# '''
# try:
# global my_lexer
# if not my_lexer:
# my_lexer = iter(Lexer(f, buf_size))
# for value in parse_value(my_lexer):
# yield value
# except StopIteration:
# pass
#
# # lexer = iter(Lexer(file, buf_size))
# # for value in parse_value(lexer):
# # yield value
# # try:
# # print next(lexer)
# # except StopIteration:
# # pass
# # else:
# # raise common.JSONError('Additional data')
#
#
# def parse(f, buf_size=BUFSIZE):
# '''
# Backend-specific wrapper for ijson.common.parse.
# '''
# return common.parse(basic_parse(f, buf_size=buf_size))
#
#
# def items(f, prefix = ''):
# '''
# Backend-specific wrapper for ijson.common.items.
# '''
# return common.items(parse(f), prefix)
class JsonItems(object):
def __init__(self, f, buf_size = BUFSIZE):
self._lexer = Lexer(f, buf_size)
def parse(self):
return common.parse(self.__basic_parse())
def get_item(self, prefix = ''):
return next(iter(common.items(self.parse(), prefix)))
def __basic_parse(self):
try:
for value in parse_value(self._lexer):
yield value
except StopIteration:
pass