Skip to content

Commit beaf2ed

Browse files
author
kbehrman
committed
added bundled simplejson as fallback for python versions not including json
1 parent 1a66e96 commit beaf2ed

File tree

8 files changed

+4257
-1
lines changed

8 files changed

+4257
-1
lines changed

shotgun_api3/lib/simplejson/__init__.py

Lines changed: 438 additions & 0 deletions
Large diffs are not rendered by default.

shotgun_api3/lib/simplejson/_speedups.c

Lines changed: 2652 additions & 0 deletions
Large diffs are not rendered by default.

shotgun_api3/lib/simplejson/decoder.py

Lines changed: 421 additions & 0 deletions
Large diffs are not rendered by default.

shotgun_api3/lib/simplejson/encoder.py

Lines changed: 503 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""Drop-in replacement for collections.OrderedDict by Raymond Hettinger
2+
3+
http://code.activestate.com/recipes/576693/
4+
5+
"""
6+
from UserDict import DictMixin
7+
8+
# Modified from original to support Python 2.4, see
9+
# http://code.google.com/p/simplejson/issues/detail?id=53
10+
try:
11+
all
12+
except NameError:
13+
def all(seq):
14+
for elem in seq:
15+
if not elem:
16+
return False
17+
return True
18+
19+
class OrderedDict(dict, DictMixin):
20+
21+
def __init__(self, *args, **kwds):
22+
if len(args) > 1:
23+
raise TypeError('expected at most 1 arguments, got %d' % len(args))
24+
try:
25+
self.__end
26+
except AttributeError:
27+
self.clear()
28+
self.update(*args, **kwds)
29+
30+
def clear(self):
31+
self.__end = end = []
32+
end += [None, end, end] # sentinel node for doubly linked list
33+
self.__map = {} # key --> [key, prev, next]
34+
dict.clear(self)
35+
36+
def __setitem__(self, key, value):
37+
if key not in self:
38+
end = self.__end
39+
curr = end[1]
40+
curr[2] = end[1] = self.__map[key] = [key, curr, end]
41+
dict.__setitem__(self, key, value)
42+
43+
def __delitem__(self, key):
44+
dict.__delitem__(self, key)
45+
key, prev, next = self.__map.pop(key)
46+
prev[2] = next
47+
next[1] = prev
48+
49+
def __iter__(self):
50+
end = self.__end
51+
curr = end[2]
52+
while curr is not end:
53+
yield curr[0]
54+
curr = curr[2]
55+
56+
def __reversed__(self):
57+
end = self.__end
58+
curr = end[1]
59+
while curr is not end:
60+
yield curr[0]
61+
curr = curr[1]
62+
63+
def popitem(self, last=True):
64+
if not self:
65+
raise KeyError('dictionary is empty')
66+
# Modified from original to support Python 2.4, see
67+
# http://code.google.com/p/simplejson/issues/detail?id=53
68+
if last:
69+
key = reversed(self).next()
70+
else:
71+
key = iter(self).next()
72+
value = self.pop(key)
73+
return key, value
74+
75+
def __reduce__(self):
76+
items = [[k, self[k]] for k in self]
77+
tmp = self.__map, self.__end
78+
del self.__map, self.__end
79+
inst_dict = vars(self).copy()
80+
self.__map, self.__end = tmp
81+
if inst_dict:
82+
return (self.__class__, (items,), inst_dict)
83+
return self.__class__, (items,)
84+
85+
def keys(self):
86+
return list(self)
87+
88+
setdefault = DictMixin.setdefault
89+
update = DictMixin.update
90+
pop = DictMixin.pop
91+
values = DictMixin.values
92+
items = DictMixin.items
93+
iterkeys = DictMixin.iterkeys
94+
itervalues = DictMixin.itervalues
95+
iteritems = DictMixin.iteritems
96+
97+
def __repr__(self):
98+
if not self:
99+
return '%s()' % (self.__class__.__name__,)
100+
return '%s(%r)' % (self.__class__.__name__, self.items())
101+
102+
def copy(self):
103+
return self.__class__(self)
104+
105+
@classmethod
106+
def fromkeys(cls, iterable, value=None):
107+
d = cls()
108+
for key in iterable:
109+
d[key] = value
110+
return d
111+
112+
def __eq__(self, other):
113+
if isinstance(other, OrderedDict):
114+
return len(self)==len(other) and \
115+
all(p==q for p, q in zip(self.items(), other.items()))
116+
return dict.__eq__(self, other)
117+
118+
def __ne__(self, other):
119+
return not self == other
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""JSON token scanner
2+
"""
3+
import re
4+
def _import_c_make_scanner():
5+
try:
6+
from simplejson._speedups import make_scanner
7+
return make_scanner
8+
except ImportError:
9+
return None
10+
c_make_scanner = _import_c_make_scanner()
11+
12+
__all__ = ['make_scanner']
13+
14+
NUMBER_RE = re.compile(
15+
r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
16+
(re.VERBOSE | re.MULTILINE | re.DOTALL))
17+
18+
def py_make_scanner(context):
19+
parse_object = context.parse_object
20+
parse_array = context.parse_array
21+
parse_string = context.parse_string
22+
match_number = NUMBER_RE.match
23+
encoding = context.encoding
24+
strict = context.strict
25+
parse_float = context.parse_float
26+
parse_int = context.parse_int
27+
parse_constant = context.parse_constant
28+
object_hook = context.object_hook
29+
object_pairs_hook = context.object_pairs_hook
30+
memo = context.memo
31+
32+
def _scan_once(string, idx):
33+
try:
34+
nextchar = string[idx]
35+
except IndexError:
36+
raise StopIteration
37+
38+
if nextchar == '"':
39+
return parse_string(string, idx + 1, encoding, strict)
40+
elif nextchar == '{':
41+
return parse_object((string, idx + 1), encoding, strict,
42+
_scan_once, object_hook, object_pairs_hook, memo)
43+
elif nextchar == '[':
44+
return parse_array((string, idx + 1), _scan_once)
45+
elif nextchar == 'n' and string[idx:idx + 4] == 'null':
46+
return None, idx + 4
47+
elif nextchar == 't' and string[idx:idx + 4] == 'true':
48+
return True, idx + 4
49+
elif nextchar == 'f' and string[idx:idx + 5] == 'false':
50+
return False, idx + 5
51+
52+
m = match_number(string, idx)
53+
if m is not None:
54+
integer, frac, exp = m.groups()
55+
if frac or exp:
56+
res = parse_float(integer + (frac or '') + (exp or ''))
57+
else:
58+
res = parse_int(integer)
59+
return res, m.end()
60+
elif nextchar == 'N' and string[idx:idx + 3] == 'NaN':
61+
return parse_constant('NaN'), idx + 3
62+
elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity':
63+
return parse_constant('Infinity'), idx + 8
64+
elif nextchar == '-' and string[idx:idx + 9] == '-Infinity':
65+
return parse_constant('-Infinity'), idx + 9
66+
else:
67+
raise StopIteration
68+
69+
def scan_once(string, idx):
70+
try:
71+
return _scan_once(string, idx)
72+
finally:
73+
memo.clear()
74+
75+
return scan_once
76+
77+
make_scanner = c_make_scanner or py_make_scanner
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
r"""Command-line tool to validate and pretty-print JSON
2+
3+
Usage::
4+
5+
$ echo '{"json":"obj"}' | python -m simplejson.tool
6+
{
7+
"json": "obj"
8+
}
9+
$ echo '{ 1.2:3.4}' | python -m simplejson.tool
10+
Expecting property name: line 1 column 2 (char 2)
11+
12+
"""
13+
import sys
14+
import simplejson as json
15+
16+
def main():
17+
if len(sys.argv) == 1:
18+
infile = sys.stdin
19+
outfile = sys.stdout
20+
elif len(sys.argv) == 2:
21+
infile = open(sys.argv[1], 'rb')
22+
outfile = sys.stdout
23+
elif len(sys.argv) == 3:
24+
infile = open(sys.argv[1], 'rb')
25+
outfile = open(sys.argv[2], 'wb')
26+
else:
27+
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
28+
try:
29+
obj = json.load(infile,
30+
object_pairs_hook=json.OrderedDict,
31+
use_decimal=True)
32+
except ValueError, e:
33+
raise SystemExit(e)
34+
json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True)
35+
outfile.write('\n')
36+
37+
38+
if __name__ == '__main__':
39+
main()

shotgun_api3/shotgun.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@
5757
import simplejson as json
5858
except ImportError:
5959
LOG.debug("simplejson not found, dropping back to json")
60-
import json as json
60+
try:
61+
import json as json
62+
except ImportError:
63+
LOG.debug("json not found, dropping back to embedded simplejson")
64+
lib_path = os.path.join('shotgun_api3','lib')
65+
sys.path.append(lib_path)
66+
import lib.simplejson as json
67+
sys.path.pop()
6168

6269
# ----------------------------------------------------------------------------
6370
# Version

0 commit comments

Comments
 (0)