forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazyjson.py
253 lines (221 loc) · 7.65 KB
/
lazyjson.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""Implements a lazy JSON file class that wraps around json data."""
import io
import weakref
from contextlib import contextmanager
from collections import Mapping, Sequence
try:
import simplejson as json
except ImportError:
import json
from xonsh.tools import string_types
def _to_json_with_size(obj, offset=0, sort_keys=False):
if isinstance(obj, string_types):
s = json.dumps(obj)
o = offset
n = size = len(s.encode()) # size in bytes
elif isinstance(obj, Mapping):
s = '{'
j = offset + 1
o = {}
size = {}
items = sorted(obj.items()) if sort_keys else obj.items()
for key, val in items:
s_k, o_k, n_k, size_k = _to_json_with_size(key, offset=j,
sort_keys=sort_keys)
s += s_k + ': '
j += n_k + 2
s_v, o_v, n_v, size_v = _to_json_with_size(val, offset=j,
sort_keys=sort_keys)
o[key] = o_v
size[key] = size_v
s += s_v + ', '
j += n_v + 2
if s.endswith(', '):
s = s[:-2]
s += '}\n'
n = len(s)
o['__total__'] = offset
size['__total__'] = n
elif isinstance(obj, Sequence):
s = '['
j = offset + 1
o = []
size = []
for x in obj:
s_x, o_x, n_x, size_x = _to_json_with_size(x, offset=j,
sort_keys=sort_keys)
o.append(o_x)
size.append(size_x)
s += s_x + ', '
j += n_x + 2
if s.endswith(', '):
s = s[:-2]
s += ']\n'
n = len(s)
o.append(offset)
size.append(n)
else:
s = json.dumps(obj, sort_keys=sort_keys)
o = offset
n = size = len(s)
return s, o, n, size
def index(obj, sort_keys=False):
"""Creates an index for a JSON file."""
idx = {}
json_obj = _to_json_with_size(obj, sort_keys=sort_keys)
s, idx['offsets'], _, idx['sizes'] = json_obj
return s, idx
JSON_FORMAT = \
"""{{"locs": [{iloc:>10}, {ilen:>10}, {dloc:>10}, {dlen:>10}],
"index": {index},
"data": {data}
}}
"""
def dumps(obj, sort_keys=False):
"""Dumps an object to JSON with an index."""
data, idx = index(obj, sort_keys=sort_keys)
jdx = json.dumps(idx, sort_keys=sort_keys)
iloc = 69
ilen = len(jdx)
dloc = iloc + ilen + 11
dlen = len(data)
s = JSON_FORMAT.format(index=jdx, data=data, iloc=iloc, ilen=ilen,
dloc=dloc, dlen=dlen)
return s
def dump(obj, fp, sort_keys=False):
"""Dumps an object to JSON file."""
s = dumps(obj, sort_keys=sort_keys)
fp.write(s)
class Node(Mapping, Sequence):
"""A proxy node for JSON nodes. Acts as both sequence and mapping."""
def __init__(self, offsets, sizes, root):
"""Parameters
----------
offsets : dict, list, or int
offsets of corresponding data structure, in bytes
sizes : dict, list, or int
sizes of corresponding data structure, in bytes
root : weakref.proxy of LazyJSON
weakref back to root node, which should be a LazyJSON object.
"""
self.offsets = offsets
self.sizes = sizes
self.root = root
self.is_mapping = isinstance(self.offsets, Mapping)
self.is_sequence = isinstance(self.offsets, Sequence)
def __len__(self):
# recall that for maps, the '__total__' key is added and for
# sequences the last element represents the total size/offset.
return len(self.sizes) - 1
def load(self):
"""Returns the Python data structure represented by the node."""
if self.is_mapping:
offset = self.offsets['__total__']
size = self.sizes['__total__']
elif self.is_sequence:
offset = self.offsets[-1]
size = self.sizes[-1]
elif isinstance(self.offsets, int):
offset = self.offsets
size = self.sizes
return self._load_or_node(offset, size)
def _load_or_node(self, offset, size):
if isinstance(offset, int):
with self.root._open(newline='\n') as f:
f.seek(self.root.dloc + offset)
s = f.read(size)
val = json.loads(s)
elif isinstance(offset, (Mapping, Sequence)):
val = Node(offset, size, self.root)
else:
raise TypeError('incorrect types for offset node')
return val
def _getitem_mapping(self, key):
if key == '__total__':
raise KeyError('"__total__" is a special LazyJSON key!')
offset = self.offsets[key]
size = self.sizes[key]
return self._load_or_node(offset, size)
def _getitem_sequence(self, key):
if isinstance(key, int):
rtn = self._load_or_node(self.offsets[key], self.sizes[key])
elif isinstance(key, slice):
key = slice(*key.indices(len(self)))
rtn = list(map(self._load_or_node, self.offsets[key],
self.sizes[key]))
else:
raise TypeError('only integer indexing available')
return rtn
def __getitem__(self, key):
if self.is_mapping:
rtn = self._getitem_mapping(key)
elif self.is_sequence:
rtn = self._getitem_sequence(key)
else:
raise NotImplementedError
return rtn
def __iter__(self):
if self.is_mapping:
keys = set(self.offsets.keys())
keys.discard('__total__')
yield from iter(keys)
elif self.is_sequence:
i = 0
n = len(self)
while i < n:
yield self._load_or_node(self.offsets[i], self.sizes[i])
i += 1
else:
raise NotImplementedError
class LazyJSON(Node):
"""Represents a lazy json file. Can be used like a normal Python
dict or list.
"""
def __init__(self, f, reopen=True):
"""Parameters
----------
f : file handle or str
JSON file to open.
reopen : bool, optional
Whether new file handle should be opened for each load.
"""
self._f = f
self.reopen = reopen
if not reopen and isinstance(f, string_types):
self._f = open(f, 'r', newline='\n')
self._load_index()
self.root = weakref.proxy(self)
self.is_mapping = isinstance(self.offsets, Mapping)
self.is_sequence = isinstance(self.offsets, Sequence)
def __del__(self):
self.close()
def close(self):
"""Close the file handle, if appropriate."""
if not self.reopen and isinstance(self._f, io.IOBase):
self._f.close()
@contextmanager
def _open(self, *args, **kwargs):
if self.reopen and isinstance(self._f, string_types):
f = open(self._f, *args, **kwargs)
yield f
f.close()
else:
yield self._f
def _load_index(self):
"""Loads the index from the start of the file."""
with self._open(newline='\n') as f:
# read in the location data
f.seek(9)
locs = f.read(48)
locs = json.loads(locs)
self.iloc, self.ilen, self.dloc, self.dlen = locs
# read in the index
f.seek(self.iloc)
idx = f.read(self.ilen)
idx = json.loads(idx)
self.offsets = idx['offsets']
self.sizes = idx['sizes']
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()