modulecore.py

changeset 154
df862cca1773
parent 149
1759c15cd0ca
child 155
5a9b5065f53f
equal deleted inserted replaced
153:497b7290977d 154:df862cca1773
28 28
29 from __future__ import print_function 29 from __future__ import print_function
30 import os 30 import os
31 import re 31 import re
32 import time 32 import time
33 import sys
33 from configfile import Config 34 from configfile import Config
34 35
35 Modules = {} 36 Modules = {}
36 Commands = {} 37 Commands = {}
37 Hooks = {} 38 Hooks = {}
61 globals = {} 62 globals = {}
62 module = __import__ (fn) 63 module = __import__ (fn)
63 Modules[fn] = module 64 Modules[fn] = module
64 65
65 for cmd in module.ModuleData['commands']: 66 for cmd in module.ModuleData['commands']:
66 if cmd['args'] == None: 67 install_command (cmd, module)
67 cmd['args'] = ''
68
69 cmd['module'] = module
70 cmd['regex'] = make_regex (cmd['args'])
71 cmd['argnames'] = []
72 Commands[cmd['name']] = cmd
73
74 for argname in cmd['args'].split (' '):
75 argname = argname[1:-1]
76
77 if argname[-3:] == '...':
78 argname = argname[0:-3]
79
80 if argname == '':
81 continue
82
83 cmd['argnames'].append (argname)
84 68
85 if 'hooks' in module.ModuleData: 69 if 'hooks' in module.ModuleData:
86 for key, hooks in module.ModuleData['hooks'].items(): 70 for key, hooks in module.ModuleData['hooks'].items():
87 for hook in hooks: 71 for hook in hooks:
88 if key not in Hooks: 72 if key not in Hooks:
93 77
94 print ('''Loaded module %s''' % fn) 78 print ('''Loaded module %s''' % fn)
95 79
96 print ('''Loaded %d commands and %d hooks in %d modules''' % 80 print ('''Loaded %d commands and %d hooks in %d modules''' %
97 (len (Commands), numHooks, len (Modules))) 81 (len (Commands), numHooks, len (Modules)))
82
83 def install_command (cmd, module):
84 if cmd['args'] == None:
85 cmd['args'] = ''
86
87 Modules[module.__name__] = module
88 cmd['module'] = module
89 cmd['regex'] = make_regex (cmd['args'])
90 cmd['argnames'] = []
91
92 for argname in cmd['args'].split (' '):
93 argname = argname[1:-1]
94
95 if argname[-3:] == '...':
96 argname = argname[0:-3]
97
98 if argname == '':
99 continue
100
101 cmd['argnames'].append (argname)
102
103 Commands[cmd['name']] = cmd
98 104
99 # 105 #
100 # command_error (message) 106 # command_error (message)
101 # 107 #
102 # Raises a command error 108 # Raises a command error
179 185
180 def exec_command (commandObject): 186 def exec_command (commandObject):
181 global g_lastConfirm 187 global g_lastConfirm
182 global g_confirmCommand 188 global g_confirmCommand
183 189
184 if 'function' in commandObject: 190 if 'function' in commandObject['info']:
185 func = commandObject['function'] 191 func = commandObject['info']['function']
186 else: 192 else:
187 cmdname = commandObject['cmdname'] 193 cmdname = commandObject['cmdname']
188 194
189 try: 195 try:
190 func = getattr (commandObject['module'], 'cmd_' + cmdname) 196 func = getattr (commandObject['module'], 'cmd_' + cmdname)
377 383
378 if not gotvariadic: 384 if not gotvariadic:
379 regex += r'\s*' 385 regex += r'\s*'
380 386
381 return '^[^ ]+%s$' % regex 387 return '^[^ ]+%s$' % regex
388
389 def irc_command (**command):
390 def result (fn):
391 command['name'] = fn.__name__
392 command['description'] = fn.__doc__
393 command['function'] = fn
394
395 if command['description'] == None:
396 command['description'] = ''
397
398 if 'level' not in command:
399 command['level'] = 'normal'
400
401 if 'args' not in command:
402 command['args'] = None
403
404 install_command (command, sys.modules[fn.__module__])
405 return fn
406 return result

mercurial