-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.py
317 lines (271 loc) · 6.97 KB
/
runtime.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- coding: utf8 -*-
# This file is part of SM (Stream Manipulator).
#
# SM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SM. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import functools
import io, re, subprocess
from collections import deque
re_type = type(re.compile('foo'))
class Lazy:
def __init__ (self, func):
self.func = func
def __call__ (self):
if not hasattr (self, "resolved"):
self.resolved = self.func ()
return self.resolved
@staticmethod
def resolve (obj):
while isinstance (obj, Lazy):
obj = obj()
return obj
@staticmethod
def deepresolve (obj):
obj = Lazy.resolve (obj)
if isinstance(obj, list):
for i in range(len(obj)):
obj[i] = Lazy.deepresolve(obj[i])
return obj
@staticmethod
def deepdiscard (obj):
q = deque([obj])
while q:
obj = Lazy.resolve (q.popleft ())
if isinstance (obj, list):
for sub in obj:
q.append (sub)
return obj
class EOS:
def __repr__ (self):
return "⟂"
class Stream:
def __init__ (self, sm):
self.sm = Lazy.resolve (sm)
self.buffer = ""
def read (self, count):
res = self.buffer[:count]
self.buffer = self.buffer[count:]
while len(res) < count and isinstance (self.sm, list):
x,ns = self.sm
res += Lazy.resolve (x)
self.sm = Lazy.resolve (ns)
if len(res) > count:
self.buffer += res[len(res)-count:]
res = res[:count]
return res
class Object:
def __init__ (self, obj):
self.obj = obj
def __getitem__ (self, name):
if hasattr(self.obj, '__getitem__'):
return self.obj[name]
else:
return getattr(self.obj, name)
def __getattr__ (self, name):
return getattr(self.obj, name)
def __str__ (self):
return str(self.obj)
def __repr__ (self):
return repr(self.obj)
class Runtime:
def __init__ (self):
self.eos = EOS ()
def _print (self, *objs):
objs = map (Lazy.deepresolve, objs)
# import pprint
# pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(*objs)
print (*objs, end='')
return objs[0]
def printS (self, s, *objs):
s = Lazy.resolve (s)
if s == self.eos:
return s
if not isinstance (s, list):
print (s)
return s
while s != self.eos and Lazy.resolve(s[0]) != self.eos:
print (Lazy.deepresolve (s[0]), end='')
if len (s) > 1:
s = Lazy.resolve (s[1])
else:
break
return s
def stream (self, obj, *args):
obj = Lazy.resolve (obj)
if obj == self.eos:
return self.eos
if isinstance (obj, list):
def _func(l):
if not l:
return self.eos
else:
return [l[0], Lazy(functools.partial (_func, l[1:]))]
return _func (obj)
return [obj]
def _readstream (self, h, *args):
def _read():
c = h.read (1)
if not c:
h.close()
return self.eos
return [c, Lazy (_read)]
return _read()
def read (self, uri, *args):
import urllib2
if uri.startswith ("http://"):
h = urllib2.urlopen(uri)
else:
h = io.open (uri, "rb")
return self._readstream(h)
def shell (self, cmd, *args):
import os
DEVNULL = open(os.devnull, 'wb')
p = subprocess.Popen (['/bin/bash', '-c', cmd], close_fds=True,
stdout=subprocess.PIPE, stderr=DEVNULL)
return self._readstream(p.stdout)
def write (self, uri, *args):
h = open (uri, "wb")
def _write(c, *args):
c = Lazy.resolve (c)
if c == self.eos:
h.close ()
return self.eos
else:
h.write (c)
return _write
return _write
def unique (self, *args):
return object()
def string (self, obj, *args):
obj = Lazy.resolve (obj)
res = ""
for x in obj:
res += Lazy.resolve (x)
return res
def fromJson (self, s, *args):
import ijson
it = ijson.parse (Stream (s))
def _read():
try:
e = it.next()
except StopIteration:
return self.eos
return [[e[1], e[2]], Lazy (_read)]
return _read()
def parseHtml (self, s, *args):
import lxml.html
# no stream reader found :(
root = lxml.html.parse (Stream (s)).getroot()
def _read(it):
try:
e = it.next()
except StopIteration:
return self.eos
return [[e, Lazy (lambda: _read (iter (e)))], Lazy (lambda: _read(it))]
return _read (iter (root))
def fromXml (self, s, *args):
import libxml2
input_source = libxml2.inputBuffer(Stream (s))
reader = input_source.newTextReader("")
nodetypes = ["none", "element", "attribute", "text", "cdata", "entref", "entity",
"procinst", "comment", "document", "doctype", "docfragment", "notation",
"whitespace", "preservewhitespace", "endelement", "endentity", "xmldecl"]
def _read():
if not reader.Read():
return self.eos
return [[reader.Name(), nodetypes[reader.NodeType()]], Lazy (_read)]
return _read()
def _not (self, obj, *args):
obj = Lazy.resolve (obj)
return self.empty (obj)
def match (self, p, *args):
def _match (t):
r = Lazy.resolve(p)
t = Lazy.resolve(t)
if isinstance (r, re_type):
it = r.finditer (t)
else:
it = re.compile(re.escape(r)).finditer (t)
def _read():
try:
e = it.next()
except StopIteration:
return self.eos
return [Object (e), Lazy (_read)]
return _read()
# for partial application
if len(args) > 0:
t = args[0]
return _match (t)
else:
return _match
def split (self, p, *args):
def _split (t):
r = Lazy.resolve(p)
t = Lazy.resolve(t)
if isinstance (r, re_type):
res = r.split (t)
else:
res = t.split (r)
return self.stream (res)
# for partial application
if len(args) > 0:
t = args[0]
return _split (t)
else:
return _split
def replace (self, p, *args):
def _replace (r, t):
pp = Lazy.resolve(p)
r = Lazy.resolve(r)
t = Lazy.resolve(t)
if isinstance (pp, re_type):
res = pp.sub (r, t)
else:
res = t.replace (pp, r)
return res
# for partial application
if len(args) > 0:
r = args[0]
if len(args) > 1:
t = args[1]
return _replace (t)
else:
return functools.partial (_replace, r)
else:
return functools.partial (functools.partial, _replace)
def empty (self, obj, *args):
obj = Lazy.resolve (obj)
return not obj or obj == self.eos or obj == [self.eos]
def _int (self, obj, *args):
obj = Lazy.resolve (obj)
return int(obj)
def _float (self, obj, *args):
obj = Lazy.resolve (obj)
return float(obj)
def _str (self, obj, *args):
obj = Lazy.resolve (obj)
return str(obj)
def _bool (self, obj, *args):
obj = Lazy.resolve (obj)
return bool(obj)
def _list (self, obj, *args):
obj = Lazy.resolve (obj)
res = []
while isinstance (obj, list):
x,ns = obj
res.append (x)
obj = Lazy.resolve (ns)
return res