modulecore.py

changeset 118
dbf49689af0d
parent 117
6c0609395889
child 121
ac07779f788d
equal deleted inserted replaced
117:6c0609395889 118:dbf49689af0d
3 import time 3 import time
4 from configfile import Config 4 from configfile import Config
5 5
6 Modules = {} 6 Modules = {}
7 Commands = {} 7 Commands = {}
8 Hooks = {}
8 9
9 class CommandError (Exception): 10 class CommandError (Exception):
10 def __init__ (self, value): 11 def __init__ (self, value):
11 self.value = value 12 self.value = value
12 def __str__ (self): 13 def __str__ (self):
19 # 20 #
20 def init_data(): 21 def init_data():
21 global Commands 22 global Commands
22 global Modules 23 global Modules
23 files = os.listdir ('.') 24 files = os.listdir ('.')
25 numHooks = 0
24 26
25 for fn in files: 27 for fn in files:
26 if fn[0:4] != 'mod_' or fn[-3:] != '.py': 28 if fn[0:4] != 'mod_' or fn[-3:] != '.py':
27 continue 29 continue
28 30
49 if argname == '': 51 if argname == '':
50 continue 52 continue
51 53
52 cmd['argnames'].append (argname) 54 cmd['argnames'].append (argname)
53 55
56 if 'hooks' in module.ModuleData:
57 for key, hooks in module.ModuleData['hooks'].iteritems():
58 for hook in hooks:
59 if key not in Hooks:
60 Hooks[key] = []
61
62 Hooks[key].append ({'name': hook, 'func': getattr (module, hook)})
63 numHooks += 1
64
54 print "Loaded module %s" % fn 65 print "Loaded module %s" % fn
55 66
56 print 'Loaded %d commands in %d modules' % (len (Commands), len (Modules)) 67 print ('Loaded %d commands and %d hooks in %d modules' %
68 (len (Commands), numHooks, len (Modules)))
57 69
58 # 70 #
59 # command_error (message) 71 # command_error (message)
60 # 72 #
61 # Raises a command error 73 # Raises a command error
176 188
177 match = re.compile (cmd['regex']).match (message) 189 match = re.compile (cmd['regex']).match (message)
178 190
179 if match == None: 191 if match == None:
180 # didn't match 192 # didn't match
181 print "regex: %s" % cmd['regex']
182 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) 193 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args']))
183 194
184 # .more is special as it is an interface to the page system. 195 # .more is special as it is an interface to the page system.
185 # Anything else resets it. 196 # Anything else resets it.
186 if cmdname != 'more': 197 if cmdname != 'more':
203 commandObject['confirm'] = confirm_function 214 commandObject['confirm'] = confirm_function
204 commandObject['confirmed'] = 0 215 commandObject['confirmed'] = 0
205 commandObject['commandObject'] = commandObject 216 commandObject['commandObject'] = commandObject
206 commandObject['info'] = cmd 217 commandObject['info'] = cmd
207 commandObject['module'] = cmd['module'] 218 commandObject['module'] = cmd['module']
219 commandObject['error'] = command_error
208 exec_command (commandObject) 220 exec_command (commandObject)
209 221
210 # Print the first page of responses. 222 # Print the first page of responses.
211 if cmdname != 'more': 223 if cmdname != 'more':
212 print_responses (commandObject) 224 print_responses (commandObject)
225
226 def call_hook (bot, hookname, **kvargs):
227 global g_responsePages
228 global g_responsePageNum
229 hookObject = kvargs
230 hookObject['bot'] = bot
231 g_responsePages = [[]]
232 g_responsePageNum = 0
233
234 if 'replyto' in hookObject:
235 hookObject['reply'] = response_function
236
237 if hookname in Hooks:
238 for hook in Hooks[hookname]:
239 hook['func'] (**hookObject)
240
241 print_responses (hookObject)
213 242
214 def confirm (cmd, yes): 243 def confirm (cmd, yes):
215 global g_confirmCommand 244 global g_confirmCommand
216 245
217 if g_confirmCommand == None: 246 if g_confirmCommand == None:

mercurial