-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
308 lines (241 loc) · 8.16 KB
/
events.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
import sys
import os
import traceback
import imp
import stat
pyending = os.extsep + 'py'
class error(Exception):
pass
class EventStopError(error):
pass
class CommandError(error):
pass
class data(object):
done = False
quiet = False
def __init__(self, **kwargs):
for attr in kwargs.items():
setattr(self, *attr)
trigger_sequence = ("pre", "setup", "on", "setdown", "post")
events = {}
loaded = {}
# An event has occurred, the e_name event!
def trigger(e_name, e_data=None, **kwargs):
if e_data is None:
e_data = data(**kwargs)
#print 'Event:', e_name, e_data
failure = True
error = None
if e_name in events:
for e_stage in trigger_sequence:
if e_stage in events[e_name]:
for f_ref, s_name in events[e_name][e_stage]:
try:
f_ref(e_data)
except EventStopError:
return
except CommandError, e:
error = e.args
continue
except:
traceback.print_exc()
failure = False
if failure:
return error
# Stop all processing of the current event now!
def halt():
raise EventStopError
# Registers a specific function with an event at the given sequence stage.
def register(e_name, e_stage, f_ref, s_name=""):
if e_name not in events:
events[e_name] = {}
if e_stage not in events[e_name]:
events[e_name][e_stage] = []
events[e_name][e_stage] += [(f_ref, s_name)]
# turn a filename (or module name) and trim it to the name of the module
def get_scriptname(name):
s_name = os.path.basename(name)
if s_name.endswith(pyending):
s_name = s_name[:-len(pyending)]
return s_name
#take a given script name and turn it into a filename
def get_filename(name):
# split the directory and filename
dirname = os.path.dirname(name)
s_name = get_scriptname(name)
for path in dirname and (dirname,) or sys.path:
filename = os.path.join(path, s_name + pyending)
if os.path.exists(filename):
return filename
raise ImportError("No urk script %s found" % name)
# register the events defined by obj
def register_all(name, obj):
# we look through everything defined in the file
for f in dir(obj):
# for each bit of the event sequence
for e_stage in trigger_sequence:
# if the function is for this bit
if f.startswith(e_stage):
# get a reference to a function
f_ref = getattr(obj, f)
# normalise to the event name
e_name = f.replace(e_stage, "", 1)
# add our function to the right event section
register(e_name, e_stage, f_ref, name)
break
#load a .py file into a new module object without affecting sys.modules
def load_pyfile(filename):
s_name = get_scriptname(filename)
module = imp.new_module(s_name)
module.__file__ = filename
# When a module gets collected, everything in its __dict__ gets set to None
# We can't let that happen until all the objects that need it are gone
# This should protect the module from being collected without __dict__
try:
module.__module__ = module
except AttributeError:
# IronPython
pass
f = file(filename,"U")
source = f.read()
f.close()
exec compile(source, filename, "exec") in module.__dict__
return module
# Load a python script and register
# the functions defined in it for events.
# Return True if we loaded the script, False if it was already loaded
def load(name):
s_name = get_scriptname(name)
filename = get_filename(name)
if s_name in loaded:
return False
loaded[s_name] = None
try:
loaded[s_name] = load_pyfile(filename)
except:
del loaded[s_name]
raise
register_all(s_name, loaded[s_name])
return True
# Is the script with the given name loaded?
def is_loaded(name):
return get_scriptname(name) in loaded
# Remove any function which was defined in the given script
def unload(name):
s_name = get_scriptname(name)
del loaded[s_name]
for e_name in list(events):
for e_stage in list(events[e_name]):
to_check = events[e_name][e_stage]
events[e_name][e_stage] = [(f, m) for f, m in to_check if m != s_name]
if not events[e_name][e_stage]:
del events[e_name][e_stage]
if not events[e_name]:
del events[e_name]
def reload(name):
s_name = get_scriptname(name)
if s_name not in loaded:
return False
temp = loaded[s_name]
unload(s_name)
try:
load(name)
return True
except:
loaded[s_name] = temp
register_all(s_name, temp)
raise
def run(text, window, network):
split = text.split(' ')
c_data = data(name=split.pop(0), text=text, window=window, network=network)
if split and split[0].startswith('-'):
c_data.switches = set(split.pop(0)[1:])
else:
c_data.switches = set()
c_data.args = split
event_name = "Command" + c_data.name.capitalize()
if event_name in events:
result = trigger(event_name, c_data)
if result:
c_data.window.write("* /%s: %s" % (c_data.name, result[0]))
else:
trigger("Command", c_data)
if not c_data.done:
c_data.window.write("* /%s: No such command exists" % (c_data.name))
# Script stuff starts here
def onCommandPyeval(e):
loc = sys.modules.copy()
loc.update(e.__dict__)
import pydoc #fix nonresponsive help() command
old_pager, pydoc.pager = pydoc.pager, pydoc.plainpager
try:
result = repr(eval(' '.join(e.args), loc))
if 's' in e.switches:
run(
'say - %s => %s' % (' '.join(e.args),result),
e.window,
e.network
)
else:
e.window.write(result)
except:
for line in traceback.format_exc().split('\n'):
e.window.write(line)
pydoc.pager = old_pager
def onCommandPyexec(e):
loc = sys.modules.copy()
loc.update(e.__dict__)
import pydoc #fix nonresponsive help() command
old_pager, pydoc.pager = pydoc.pager, pydoc.plainpager
try:
exec ' '.join(e.args) in loc
except:
for line in traceback.format_exc().split('\n'):
e.window.write(line)
pydoc.pager = old_pager
def onCommandLoad(e):
if e.args:
name = e.args[0]
else:
e.window.write('Usage: /load scriptname')
try:
if load(name):
e.window.write("* The script '%s' has been loaded." % name)
else:
raise CommandError("The script is already loaded; use /reload instead")
except:
e.window.write(traceback.format_exc(), line_ending='')
raise CommandError("Error loading the script")
def onCommandUnload(e):
if e.args:
name = e.args[0]
else:
e.window.write('Usage: /unload scriptname')
if is_loaded(name):
unload(name)
e.window.write("* The script '%s' has been unloaded." % name)
else:
raise CommandError("No such script is loaded")
def onCommandReload(e):
if e.args:
name = e.args[0]
else:
e.window.write('Usage: /reload scriptname')
try:
if reload(name):
e.window.write("* The script '%s' has been reloaded." % name)
else:
raise CommandError("The script isn't loaded yet; use /load instead")
except:
e.window.write(traceback.format_exc(), line_ending='')
def onCommandScripts(e):
e.window.write("Loaded scripts:")
for name in loaded:
e.window.write("* %s" % name)
def onCommandEcho(e):
e.window.write(' '.join(e.args))
name = ''
for name in globals():
if name.startswith('onCommand'):
register(name[2:], "on", globals()[name], '_events')
del name