forked from ellxc/piperbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrappers.py
372 lines (305 loc) · 11 KB
/
wrappers.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
import re
from multiprocessing import TimeoutError
import functools
import inspect
import string
from enum import IntEnum
import os
from multiprocessing import Process, Pipe
from scheduler import Task
from functools import partial
def plugin(desc=None, thread=False):
def wrapper(clas):
clas._plugin = True
clas._plugin_desc = desc
clas._plugin__init__ = _plugin__init__
clas._plugin_thread = thread
return clas
if inspect.isclass(desc):
return wrapper(desc)
else:
return wrapper
def on_load(func):
if not hasattr(func, '_onLoad'):
func._onLoad = True
return func
def on_unload(func):
if not hasattr(func, '_onUnload'):
func._onUnload = True
return func
def serialise(func):
if not hasattr(func, '_onNewUser'):
func._onNewUser = True
return func
def adv_command(name=None, **kwargs):
def _coroutine(func):
if hasattr(func, '_commands'):
func._commands.append(dict(kwargs,
command=name
if not inspect.isfunction(name) and name is not None
else func.__name__))
return func
func._commands = []
func2 = dict(kwargs, command=name if not inspect.isfunction(name) and name is not None else func.__name__)
func._commands.append(func2)
@functools.wraps(func)
def generator(self, args, target):
x = func(self, args, target)
next(x)
return x
return generator
if inspect.isfunction(name):
return _coroutine(name)
else:
return _coroutine
def command(name=None, **kwargs):
def _coroutine(func):
if hasattr(func, '_commands'):
func._commands.append(dict(kwargs,
command=name
if not inspect.isfunction(name) and name is not None
else func.__name__))
return func
func._commands = []
func2 = dict(kwargs, command=name if not inspect.isfunction(name) and name is not None else func.__name__)
func._commands.append(func2)
@functools.wraps(func)
def generator(self, args, target):
def inner(target):
arg = args
formats = len(list(string.Formatter().parse(arg.data)))
try:
while True:
line = yield
if line is None:
if formats:
x = func(self, arg.reply(arg.data.format(*([""] * formats)), args=arg.args))
else:
x = func(self, arg)
else:
if formats:
if line.data is not None:
x = func(self, line.reply(arg.data.format(*([line.data] * formats)), args=arg.args))
else:
x = func(self, line.reply(arg.data.format(*([""] * formats)), args=arg.args))
else:
x = func(self, line.reply(line.data, args=arg.args))
if x is not None:
if inspect.isgenerator(x):
for y in x:
target.send(y)
else:
target.send(x)
except GeneratorExit:
if target is not None:
target.close()
ret = inner(target)
next(ret)
return ret
return generator
if inspect.isfunction(name):
return _coroutine(name)
else:
return _coroutine
class extensiontype(IntEnum):
command = 1
trigger = 2
event = 3
regex = 4
def extension(priority=1, type=1):
def _coroutine(func):
if hasattr(func, "_extensions"):
func._extensions.append((priority, type))
return func
func._extensions = []
func._extensions.append((priority, type))
@functools.wraps(func)
def generator(self, original, target):
def inner(target):
try:
while True:
line = yield
if line is not None:
x = func(self, line)
if inspect.isgenerator(x):
for y in x:
target.send(y)
else:
target.send(x)
except GeneratorExit:
target.close()
ret = inner(target)
next(ret)
return ret
return generator
return _coroutine
def adv_extension(priority=1, type=1):
def _coroutine(func):
if hasattr(func, "_extensions"):
func._extensions.append((priority, type))
return func
func._extensions = []
func._extensions.append((priority, type))
@functools.wraps(func)
def generator(self, original, target):
x = func(self, original, target)
next(x)
return x
return generator
return _coroutine
def arg(arg=None, default=False):
def wrapper(func):
if not hasattr(func, '_args'):
func._args = {}
func._args[arg] = default
return func
if inspect.isfunction(arg):
raise Exception("no arg specified")
else:
return wrapper
def regex(regex=None):
def wrapper(func):
if hasattr(func, '_regexes'):
func._regexes.append(re.compile(regex))
return func
func._regexes = []
func._regexes.append(re.compile(regex))
@functools.wraps(func)
def generator(self, target):
message = yield
x = func(self, message)
if x is not None:
if inspect.isgenerator(x):
for y in x:
target.send(y)
else:
target.send(x)
return generator
if regex is None:
raise Exception("no regex specified")
else:
return wrapper
def event(event_):
def wrapper(func):
if hasattr(func, '_handlers'):
func._handlers.append(event_)
return func
func._handlers = []
func._handlers.append(event_)
@functools.wraps(func)
def generator(self, target):
message = yield
x = func(self, message)
if x is not None:
if inspect.isgenerator(x):
for y in x:
target.send(y)
else:
target.send(x)
return generator
return wrapper
def trigger(trigger_=None):
def wrapper(func):
if hasattr(func, '_triggers'):
func._triggers.append(trigger_)
return func
func._triggers = []
func._triggers.append(trigger_)
@functools.wraps(func)
def generator(self, target):
message = yield
x = func(self, message)
if x is not None:
if inspect.isgenerator(x):
for y in x:
target.send(y)
else:
target.send(x)
return generator
if trigger is None:
raise Exception("no trigger specified")
else:
return wrapper
def scheduled(task):
assert isinstance(task, Task)
def wrapper(func):
if not hasattr(func, "_scheduleds"):
func._scheduleds = []
func._scheduleds.append(task)
return func
return wrapper
def run_procced(p2, fun, args, kwargs):
os.nice(20)
try:
result = fun(*args, **kwargs)
print(result, file=open(os.devnull, "w")) # hack hack hack
p2.send(result)
except Exception as e:
p2.send(e)
def killproc(p):
p.terminate()
def timed(func, args=(), kwargs={}, timeout=2, proc=True):
p1, p2 = Pipe()
p = Process(target=run_procced, args=(p2, func, args, kwargs))
p.start()
try:
if p1.poll(timeout=timeout):
result = p1.recv()
if isinstance(result, Exception):
raise result
else:
return result
else:
raise TimeoutError
except TimeoutError as e:
pk = Process(target=killproc, args=(p,))
pk.start()
raise Exception("Took more than %s seconds" % timeout)
except MemoryError as e:
raise MemoryError("proccess ran out of memory")
def _plugin__init__(self, bot):
self.bot = bot
self._onLoads = []
self._onUnloads = []
self._onNewUser = None
self._triggers = []
self._commands = []
self._regexes = []
self._handlers = []
self._scheduleds = []
self._command_extensions = []
self._event_extensions = []
self._trigger_extensions = []
self._regex_extensions = []
for name, func in inspect.getmembers(self, predicate=inspect.ismethod):
if hasattr(func, '_onLoad'):
self._onLoads.append(func)
if hasattr(func, '_onUnload'):
self._onUnloads.append(func)
if hasattr(func, '_onNewUser'):
self._onNewUser = func
if hasattr(func, '_triggers'):
for trigger in func._triggers:
self._triggers.append((trigger, func))
if hasattr(func, '_commands'):
for args in func._commands:
self._commands.append((func, args))
if hasattr(func, '_regexes'):
for regex in func._regexes:
self._regexes.append((regex, func))
if hasattr(func, '_handlers'):
for event in func._handlers:
self._handlers.append((event, func))
if hasattr(func, '_scheduleds'):
for task in func._scheduleds:
self._scheduleds.append(task.do(partial(func)))
if hasattr(func, '_extensions'):
for priority, type in func._extensions:
if type == extensiontype.command:
self._command_extensions.append((priority, func))
elif type == extensiontype.regex:
self._regex_extensions.append((priority, func))
elif type == extensiontype.trigger:
self._trigger_extensions.append((priority, func))
elif type == extensiontype.event:
self._event_extensions.append((priority, func))