-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
262 lines (218 loc) · 6.21 KB
/
util.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
253
254
255
256
257
258
259
260
261
262
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import os, sys, mllib, traceback, time
__SELF__ = object()
class Constant:
def __init__(self, name, value=__SELF__):
self.name = name
if value is __SELF__:
self.value = self
else:
self.value = value
def __repr__(self):
return self.name
class InsufficientCapacity(Exception): pass
class Buffer:
def __init__(self, bytes="", capacity=None):
self.bytes = bytes
self.capacity = capacity
def read(self, n=None):
if n is None:
n = len(self.bytes)
result = self.bytes[:n]
self.bytes = self.bytes[n:]
return result
def peek(self, n=None):
if n is None:
return self.bytes
else:
return self.bytes[:n]
def write(self, bytes):
if self.capacity and len(self.bytes) + len(bytes) > self.capacity:
raise InsufficientCapacity()
self.bytes += bytes
def pending(self):
return len(self.bytes)
def parse(state):
while True:
next = state()
if next is None:
break
else:
state = next
return state
def load_xml(name):
xml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), name)
return mllib.xml_parse(xml_file)
def pythonize(name, camel=False):
if name is None:
return name
name = str(name)
if camel:
return "".join([p.capitalize() for p in name.split("-")])
else:
return name.replace("-", "_")
def decode_numeric_desc(descriptor):
high, low = [int(x, 0) for x in descriptor.split(":")]
return (high << 32) + low
def identity(x):
return x
class ConnectionSelectable:
def __init__(self, socket, connection, tick, period=None, timeout=lambda c: None):
self.socket = socket
self.connection = connection
self.tick = tick
self.period = period
self._timeout = timeout
if self.period:
self._timing = 0
else:
self._timing = None
def fileno(self):
return self.socket.fileno()
def timing(self):
return self._timing
def timeout(self, selector):
self._timing = time.time() + self.period
self._timeout(self.connection)
def reading(self):
return self.socket is not None
def writing(self):
if self.socket is None: return False
self.tick(self.connection)
return self.connection.pending()
def readable(self, selector):
# XXX: hardcoded buffer size
try:
bytes = self.socket.recv(64*1024)
except:
cls, exc, tb = sys.exc_info()
self.connection.trace("err", "".join(traceback.format_exception(cls, exc, tb)).strip())
try:
self.connection.error(exc)
self.tick(self.connection)
except:
self.connection.trace("err", traceback.format_exc().strip())
else:
try:
if bytes:
self.connection.write(bytes)
self.tick(self.connection)
return
else:
self.connection.closed()
self.tick(self.connection)
except:
self.connection.trace("err", traceback.format_exc().strip())
selector.unregister(self)
self.socket.close()
self.socket = None
def writeable(self, selector):
try:
n = self.socket.send(self.connection.peek())
bytes = self.connection.read(n)
return
except:
cls, exc, tb = sys.exc_info()
self.connection.trace("err", "".join(traceback.format_exception(cls, exc, tb)).strip())
self.connection.error(exc)
self.tick(self.connection)
selector.unregister(self)
self.socket.close()
self.socket = None
class Range:
def __init__(self, lower, upper = None):
self.lower = lower
if upper is None:
self.upper = self.lower
else:
self.upper = upper
assert self.lower <= self.upper
def __contains__(self, n):
return self.lower <= n and n <= self.upper
def __iter__(self):
i = self.lower
while i <= self.upper:
yield i
i += 1
def touches(self, r):
# XXX: are we doing more checks than we need?
return (self.lower - 1 in r or
self.upper + 1 in r or
r.lower - 1 in self or
r.upper + 1 in self or
self.lower in r or
self.upper in r or
r.lower in self or
r.upper in self)
def span(self, r):
return Range(min(self.lower, r.lower), max(self.upper, r.upper))
def intersect(self, r):
lower = max(self.lower, r.lower)
upper = min(self.upper, r.upper)
if lower > upper:
return None
else:
return Range(lower, upper)
def __repr__(self):
return "%s-%s" % (self.lower, self.upper)
class RangeSet:
def __init__(self, *args):
self.ranges = []
for n in args:
self.add(n)
def __contains__(self, n):
for r in self.ranges:
if n in r:
return True
return False
def add_range(self, range):
idx = 0
while idx < len(self.ranges):
r = self.ranges[idx]
if range.touches(r):
del self.ranges[idx]
range = range.span(r)
elif range.upper < r.lower:
self.ranges.insert(idx, range)
return
else:
idx += 1
self.ranges.append(range)
def add(self, lower, upper = None):
self.add_range(Range(lower, upper))
def empty(self):
for r in self.ranges:
if r.lower <= r.upper:
return False
return True
def max(self):
if self.ranges:
return self.ranges[-1].upper
else:
return None
def min(self):
if self.ranges:
return self.ranges[0].lower
else:
return None
def __iter__(self):
return iter(self.ranges)
def __repr__(self):
return str(self.ranges)