Commits

Jason R. Coombs committed 366a713

Moved handler registry into the Handler class

Comments (0)

Files changed (3)

 
 		messages = ()
 		matching_handlers = (
-			handler for handler in _handler_registry
+			handler for handler in Handler._registry
 			if handler.match(msg, channel))
 		for handler in matching_handlers:
 			exception_handler = functools.partial(
 			except:
 				pass
 
-_handler_registry = []
 _delay_registry = []
 _at_registry = []
 _join_registry = []
 
 class Handler(object):
+	_registry = []
+
 	class_priority = 1
 	"priority of this class relative to other classes, precedence to higher"
 
 		self.func = func
 		self.__dict__.update(kwargs)
 
+	@classmethod
+	def add_handler(cls, handler):
+		cls._registry.append(handler)
+		cls._registry.sort()
+
 	@property
 	def sort_key(self):
 		return -self.class_priority, -self.priority, -len(self.name)
 		doc=None, **kwargs):
 	def deco(func):
 		effective_priority = priority+1 if name == '#' else priority
-		_handler_registry.append(ContainsHandler(
+		Handler.register(ContainsHandler(
 			name=name.lower(),
 			func=func,
 			doc=doc,
 			rate=rate,
 			priority=effective_priority,
 			**kwargs))
-		_handler_registry.sort()
 		return func
 	return deco
 
 			doc=doc,
 			aliases=[],
 		)
-		_handler_registry.append(ch)
+		Handler.register(ch)
 		for alias in aliases:
 			ah = AliasHandler(
 				name=alias,
 				func=func,
 				doc=doc)
 			ch.aliases.append(ah)
-			_handler_registry.append(ah)
-		_handler_registry.sort()
+			Handler.register(ah)
 		return func
 	return deco
 
 def regexp(name, regexp, doc=None):
 	def deco(func):
-		_handler_registry.append(RegexpHandler(
+		Handler.register(RegexpHandler(
 			name=name.lower(),
 			func=func,
 			doc=doc,
 			pattern=re.compile(regexp, re.IGNORECASE),
 		))
-		_handler_registry.sort()
 		return func
 	return deco
 
 
 	_setup_logging()
 	_load_library_extensions()
-	if not _handler_registry:
+	if not Handler._registry:
 		raise RuntimeError("No handlers registered")
 
 	class_ = SilentCommandBot if config.silent_bot else LoggingCommandBot
 import pkg_resources
 
 import pmxbot.core
-from pmxbot.core import command, _handler_registry
+from pmxbot.core import command, Handler
 
 @command("help", aliases=('h',), doc="Help (this command)")
 def help(client, event, channel, nick, rest):
 	rs = rest.strip()
 	if rs:
 		# give help for matching commands
-		for handler in _handler_registry:
+		for handler in Handler._registry:
 			if handler.name == rs.lower():
 				yield '!%s: %s' % (handler.name, handler.doc)
 				break
 
 	# give help for all commands
 	def mk_entries():
-		handlers = (handler for handler in _handler_registry
+		handlers = (handler for handler in Handler._registry
 			if type(handler) is pmxbot.core.CommandHandler)
 		handlers = sorted(handlers, key=operator.attrgetter('name'))
 		for handler in handlers:

pmxbot/web/viewer.py

 		commands = []
 		contains = []
 		by_name = lambda handler: handler.name
-		for handler in sorted(pmxbot.core._handler_registry, key=by_name):
+		for handler in sorted(pmxbot.core.Handler._registry, key=by_name):
 			if type(handler) is pmxbot.core.CommandHandler:
 				commands.append((handler.name, handler.doc, handler.aliases))
 			elif isinstance(handler, pmxbot.core.ContainsHandler):