14
15
from cStringIO import StringIO
16
from StringIO import StringIO
17
from StringIO import StringIO
18
19
import weakref, os, time
23
24
except ImportError:
24
import dummy_threading as threading
25
import dummy_thread as thread
25
import dummy_threading as threading
26
import dummy_thread as thread
28
if sys.platform.startswith('win') or sys.platform.startswith('java'):
29
time_func = time.clock
27
33
def verify_directory(dir):
28
"""create and/or verify a filesystem directory."""
30
while not os.access(dir, os.F_OK):
33
os.makedirs(dir, 0750)
34
"""create and/or verify a filesystem directory."""
38
while not os.path.exists(dir):
41
os.makedirs(dir, 0750)
38
46
class SetLikeDict(dict):
39
47
"""a dictionary that has some setlike methods on it"""
40
48
def union(self, other):
44
52
x = SetLikeDict(**self)
48
56
class FastEncodingBuffer(object):
49
57
"""a very rudimentary buffer that is faster than StringIO, but doesnt crash on unicode data like cStringIO."""
50
def __init__(self, encoding=None, errors='strict'):
59
def __init__(self, encoding=None, errors='strict', unicode=False):
52
61
self.encoding = encoding
66
self.unicode = unicode
53
67
self.errors = errors
54
def write(self, text):
55
self.data.append(text)
68
self.write = self.data.append
56
70
def getvalue(self):
58
return u''.join(self.data).encode(self.encoding, self.errors)
72
return self.delim.join(self.data).encode(self.encoding, self.errors)
60
return u''.join(self.data)
74
return self.delim.join(self.data)
62
76
class LRUCache(dict):
63
77
"""A dictionary-like object that stores a limited number of items, discarding
64
78
lesser used items periodically.
66
80
this is a rewrite of LRUCache from Myghty to use a periodic timestamp-based
67
81
paradigm so that synchronization is not really needed. the size management
71
85
class _Item(object):
72
86
def __init__(self, key, value):
75
self.timestamp = time.time()
89
self.timestamp = time_func()
76
90
def __repr__(self):
77
91
return repr(self.value)
79
93
def __init__(self, capacity, threshold=.5):
80
94
self.capacity = capacity
81
95
self.threshold = threshold
83
97
def __getitem__(self, key):
84
98
item = dict.__getitem__(self, key)
85
item.timestamp = time.time()
99
item.timestamp = time_func()
89
103
return [i.value for i in dict.values(self)]
115
129
# if we couldnt find a key, most likely some other thread broke in
116
130
# on us. loop around and try again
133
def restore__ast(_ast):
134
"""Attempt to restore the required classes to the _ast module if it
135
appears to be missing them
137
if hasattr(_ast, 'AST'):
139
_ast.PyCF_ONLY_AST = 2 << 9
142
class Bar(object): pass
146
6 // 7 % 8 << 9 >> 10
149
-baz + (not +18) - ~17
150
baz and 'foo' or 'bar'
151
(mako is baz == baz) is not baz != mako
152
mako > baz < mako >= baz <= mako
153
mako in baz not in mako""", '<unknown>', 'exec', _ast.PyCF_ONLY_AST)
154
_ast.Module = type(m)
156
for cls in _ast.Module.__mro__:
157
if cls.__name__ == 'mod':
159
elif cls.__name__ == 'AST':
162
_ast.FunctionDef = type(m.body[0])
163
_ast.ClassDef = type(m.body[1])
164
_ast.If = type(m.body[2])
166
_ast.Name = type(m.body[3].targets[0])
167
_ast.Store = type(m.body[3].targets[0].ctx)
168
_ast.Str = type(m.body[3].value)
170
_ast.Sub = type(m.body[4].value.op)
171
_ast.Add = type(m.body[4].value.left.op)
172
_ast.Div = type(m.body[4].value.right.op)
173
_ast.Mult = type(m.body[4].value.right.left.op)
175
_ast.RShift = type(m.body[5].value.op)
176
_ast.LShift = type(m.body[5].value.left.op)
177
_ast.Mod = type(m.body[5].value.left.left.op)
178
_ast.FloorDiv = type(m.body[5].value.left.left.left.op)
180
_ast.BitOr = type(m.body[6].value.op)
181
_ast.BitXor = type(m.body[6].value.left.op)
182
_ast.BitAnd = type(m.body[6].value.left.left.op)
184
_ast.Or = type(m.body[7].value.op)
185
_ast.And = type(m.body[7].value.values[0].op)
187
_ast.Invert = type(m.body[8].value.right.op)
188
_ast.Not = type(m.body[8].value.left.right.op)
189
_ast.UAdd = type(m.body[8].value.left.right.operand.op)
190
_ast.USub = type(m.body[8].value.left.left.op)
192
_ast.Or = type(m.body[9].value.op)
193
_ast.And = type(m.body[9].value.values[0].op)
195
_ast.IsNot = type(m.body[10].value.ops[0])
196
_ast.NotEq = type(m.body[10].value.ops[1])
197
_ast.Is = type(m.body[10].value.left.ops[0])
198
_ast.Eq = type(m.body[10].value.left.ops[1])
200
_ast.Gt = type(m.body[11].value.ops[0])
201
_ast.Lt = type(m.body[11].value.ops[1])
202
_ast.GtE = type(m.body[11].value.ops[2])
203
_ast.LtE = type(m.body[11].value.ops[3])
205
_ast.In = type(m.body[12].value.ops[0])
206
_ast.NotIn = type(m.body[12].value.ops[1])