forked from marianoguerra/y
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yel_utils.py
387 lines (294 loc) · 9.24 KB
/
yel_utils.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import pwd
import grp
import sys
import edn_format
import datetime
import collections
from edn_format.edn_parse import TaggedElement
from edn_format.edn_lex import Symbol, Keyword
from yel_status import OK
from yel_predicates import *
END_UNIT = "\n"
CACHED_USER_NAMES = {}
CACHED_GROUP_NAMES = {}
def get_user_from_uid(uid):
if uid in CACHED_USER_NAMES:
return CACHED_USER_NAMES[uid]
else:
try:
username = pwd.getpwuid(uid)[0]
except KeyError:
return str(uid)
CACHED_USER_NAMES[uid] = username
return username
def get_group_from_gid(gid):
if gid in CACHED_GROUP_NAMES:
return CACHED_GROUP_NAMES[gid]
else:
try:
groupname = grp.getgrgid(gid)[0]
except KeyError:
return str(gid)
CACHED_GROUP_NAMES[gid] = groupname
return groupname
def unwrap(data):
if isinstance(data, TaggedValue):
return data.value
else:
return data
def to_list(data):
if is_seq(data):
return data
else:
return [data]
def get_key(data, key, default):
if isinstance(data, collections.Sequence):
if key in data:
return data[key]
else:
return default
elif isinstance(data, collections.Mapping):
return data.get(key, default)
else:
return default
def pythonify_seq(obj):
result = []
for value in obj:
new_value = pythonify(value)
result.append(new_value)
return result
def pythonify(obj):
if isinstance(obj, Symbol):
return str(obj)
elif isinstance(obj, Keyword):
return str(obj)[1:]
elif isinstance(obj, dict):
result = {}
for key, value in obj.items():
new_key = pythonify(key)
new_value = pythonify(value)
result[new_key] = new_value
return result
elif isinstance(obj, list):
return pythonify_seq(obj)
elif isinstance(obj, tuple):
return tuple(pythonify_seq(obj))
elif isinstance(obj, set):
return set(pythonify_seq(obj))
else:
return obj
def make_printer(out):
def printer(obj, add_end_unit=True):
out.write(edn_format.dumps(obj))
if add_end_unit:
out.write(END_UNIT)
return printer
class Options(dict, TaggedElement):
def __init__(self, value, wrapped=False):
self.name = "y.O"
self.update(value)
self.wrapped = wrapped
def __str__(self):
return "#y.O {}".format(edn_format.dumps(self.to_dict()))
def to_dict(self):
return dict(self.items())
class Error(TaggedElement):
def __init__(self, value):
self.name = "y.E"
self.value = value
def __str__(self):
return "#y.E {}".format(edn_format.dumps(self.value))
class InputCommand(object):
def __init__(self, options, din, dout):
self.printer = make_printer(dout)
self.din = din
self.dout = dout
self.options = None
self.status = OK
self.finish = False
self.on_options(options)
def end(self, status=OK):
self.finish = True
self.status = status
def on_options(self, options):
self.options = pythonify(options)
def on_data(self, data):
self.printer(data)
def on_error(self, error):
self.printer(error)
def error(self, reason, status=500):
print_error(reason, status, self.dout)
def on_start(self):
pass
def on_end(self):
pass
def run(self):
self.on_start()
try:
skip, end, data = next_data(self.din, self.dout)
while not self.finish and not end:
if skip:
continue
if isinstance(data, Options):
self.on_options(data)
else:
self.on_data(data)
skip, end, data = next_data(self.din, self.dout)
self.on_end()
except KeyboardInterrupt:
pass
return self.status
class LineMapper(object):
def __init__(self, data):
self.remaining = to_list(data)
def next_data(self):
if len(self.remaining) > 0:
return False, False, self.remaining.pop(0)
else:
return False, True, None
def next_data(din, dout):
skip = False
data = None
if isinstance(din, LineMapper):
return din.next_data()
unit = din.readline()
end = not bool(unit)
if end:
return skip, end, data
try:
data = edn_format.loads(unit)
except SyntaxError as err:
print_error(str(err), 500, dout)
skip = True
return skip, end, data
class TypeCommand(InputCommand):
def __init__(self, type_, fun, options, din, dout):
InputCommand.__init__(self, options, din, dout)
self.type = type_
self.fun = fun
if type(type_) != tuple:
self.type_name = type_.__name__
else:
self.type_name = ", ".join(x.__name__ for x in type_)
def on_data(self, data):
if isinstance(data, self.type):
self.printer(self.fun(data))
else:
self.error("Data is not of type {}: {}".format(
self.type_name, type(data).__name__),
400)
class StrCommand(TypeCommand):
def __init__(self, method_name, options, din, dout):
TypeCommand.__init__(self, (str, Keyword, Symbol),
lambda x: getattr(pythonify(x), method_name)(), options,
din, dout)
def print_error(reason, status, out):
err = Error(dict(reason=reason, status=status))
out.write(edn_format.dumps(err))
out.write(END_UNIT)
def error(reason, status, out=sys.stdout):
print_error(reason, status, out)
sys.exit(status)
class TaggedValue(TaggedElement):
def __init__(self, name, value):
self.value = value
self.name = name
def __str__(self):
return "{} {}".format(self.name, self.value)
__repr__ = __str__
def __hash__(self):
return hash(self.value)
def __eq__(self, other):
if isinstance(other, TaggedValue):
return self.value == other.value
else:
return self.value == other
def to_human(self):
return str(self.value)
class TaggedString(TaggedValue):
def __str__(self):
# TODO: escape quotes
return '{} "{}"'.format(self.name, self.value)
class FileSize(TaggedValue):
def __init__(self, value):
TaggedValue.__init__(self, "#y.FileSize", value)
def to_human(self):
return "{} KBs".format(self.value / 1024)
class Uid(TaggedValue):
def __init__(self, value):
TaggedValue.__init__(self, "#y.Uid", value)
def to_human(self):
return get_user_from_uid(self.value)
class Gid(TaggedValue):
def __init__(self, value):
TaggedValue.__init__(self, "#y.Gid", value)
def to_human(self):
return get_group_from_gid(self.value)
class Path(TaggedString):
def __init__(self, value):
TaggedValue.__init__(self, "#y.Path", value)
class UnixPerms(TaggedValue):
def __init__(self, value):
TaggedValue.__init__(self, "#y.UnixPerms", value)
def __str__(self):
return '{} "{}"'.format(self.name, self.value)
__repr__ = __str__
class Timestamp(TaggedValue):
def __init__(self, value):
TaggedValue.__init__(self, "#y.Timestamp", value)
def to_human(self):
dtime = datetime.datetime.fromtimestamp(self.value)
return dtime.strftime("%c")
class FileType(TaggedString):
TO_HUMAN = {
"f": "File",
"d": "Dir",
"l": "Link",
"m": "Mount"
}
def __init__(self, value):
TaggedString.__init__(self, "#y.FileType", value)
def to_human(self):
return self.TO_HUMAN.get(self.value, "Unknwon")
class File(dict, TaggedElement):
def __init__(self, value):
self.update(value)
self.name = "#y.File"
@classmethod
def from_path(cls, fpath):
stat = os.stat(fpath)
size = FileSize(stat.st_size)
uid = Uid(stat.st_uid)
gid = Gid(stat.st_gid)
atime = Timestamp(stat.st_atime)
mtime = Timestamp(stat.st_mtime)
mode = UnixPerms(oct(stat.st_mode))
path = Path(os.path.abspath(fpath))
if os.path.isfile(fpath):
ftype = "f"
elif os.path.isdir(fpath):
ftype = "d"
elif os.path.islink(fpath):
ftype = "l"
elif os.path.ismount(fpath):
ftype = "m"
else:
ftype = "?"
file_type = FileType(ftype)
return cls(dict(path=path, size=size, uid=uid, gid=gid, atime=atime,
mtime=mtime, mode=mode, type=file_type))
def __str__(self):
return "#y.File {}".format(edn_format.dumps(self.to_dict()))
def to_dict(self):
return dict(self.items())
edn_format.add_tag("y.E", Error)
edn_format.add_tag("y.O", Options)
edn_format.add_tag("y.Uid", Uid)
edn_format.add_tag("y.Gid", Gid)
edn_format.add_tag("y.File", File)
edn_format.add_tag("y.FileSize", FileSize)
edn_format.add_tag("y.FileType", FileType)
edn_format.add_tag("y.Path", Path)
edn_format.add_tag("y.Timestamp", Timestamp)
edn_format.add_tag("y.UnixPerms", UnixPerms)