Browse Source

Checkpoint of command decorator refactor.

Douglas William Thrift 11 years ago
parent
commit
43746be66b
1 changed files with 115 additions and 36 deletions
  1. 115 36
      locationbot.py

+ 115 - 36
locationbot.py

@@ -10,6 +10,7 @@ from ConfigParser import NoOptionError, SafeConfigParser
 from datetime import datetime, timedelta
 import functools
 import getpass
+import inspect
 import ircbot
 import irclib
 import oauth2client.client
@@ -343,6 +344,56 @@ class Location(object):
 		else:
 			return 'NNW'
 
+LOGIN	= 0b01
+LOGOUT	= 0b10
+ALWAYS	= 0b11
+
+CHANNEL	= 0b01
+PRIVATE	= 0b10
+BOTH	= 0b11
+
+class Command(object):
+	def __init__(self, admin, state, access, arguments, description, function):
+		self.admin = admin
+		self.state = state
+		self.access = access
+		self.arguments = arguments
+		self.description = description
+		self.function = function
+
+	@property
+	def is_admin(self):
+		return self.admin
+
+	@property
+	def is_login(self):
+		return bool(self.state & LOGIN)
+
+	@property
+	def is_logout(self):
+		return bool(self.state & LOGOUT)
+
+	@property
+	def is_channel(self):
+		return bool(self.access & CHANNEL)
+
+	@property
+	def is_private(self):
+		return bool(self.access & PRIVATE)
+
+	def __call__(self, *args, **kwargs):
+		self.function(*args, **kwargs)
+
+def command(admin, state, access, arguments, description):
+	def command(function):
+		clazz = inspect.getouterframes(inspect.currentframe())[1][0].f_locals
+
+		clazz.setdefault('commands', {})[function.func_name] = Command(admin, state, access, arguments, description, function)
+
+		return function
+
+	return command
+
 class LocationBot(ircbot.SingleServerIRCBot):
 	def __init__(self, bot = None):
 		self.__config = SafeConfigParser()
@@ -438,46 +489,76 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 		return db, db.cursor()
 
-	def __help(self, connection, nick, admin, login, arguments):
+	@command(False, ALWAYS, PRIVATE, '[command]', 'show this help message')
+	def help(self, connection, nick, **kwargs):
+		arguments = kwargs.get('arguments')
 		command = irclib.irc_lower(arguments.split(None, 1)[0].lstrip('!')) if arguments else None
-		commands = {
-			'help': ('[command]', 'show this help message'),
-			'status': ('[nick]', 'show where everybody or a nick is'),
-		}
-
-		if not login:
-			commands.update({
-				'login': ('[nick] [secret]', 'log in as nick with secret or using masks'),
-				'register': ('[nick] secret', 'register as nick with secret'),
-			})
-		else:
-			commands.update({
-				'logout': ('', 'log out as nick'),
-				'set': ('[variable [value]]', 'display or set variables'),
-				'unset': ('variable', 'unset a variable'),
-			})
-
-		if admin:
-			commands.update({
-				'join': ('channel', 'join a channel'),
-				'part': ('channel [message]', 'part from a channel'),
-				'quit': ('[message]', 'quit and do not come back'),
-				'reload': ('', 'reload with more up to date code'),
-				'restart': ('', 'quit and join running more up to date code'),
-				'say': ('nick|channel message', 'say message to nick or channel'),
-				'who': ('', 'show who is logged in'),
-			})
 
 		connection.privmsg(nick, '\x02command     arguments               description\x0f')
 
-		def help(command, arguments, description):
-			connection.privmsg(nick, '%-11s %-23s %s' % (command, arguments, description))
+		def help(name, command):
+			connection.privmsg(nick, '%-11s %-23s %s' % (name, command.arguments, command.description))
 
-		if command in commands:
-			help(command, *commands[command])
+		if command in self.commands:
+			help(command, self.commands[command])
 		else:
-			for command, (arguments, description) in sorted(commands.iteritems()):
-				help(command, arguments, description)
+			for name, command in sorted(self.commands.iteritems()):
+				help(name, command)
+
+	@command(False, ALWAYS, BOTH, '[nick]', 'show where everybody or a nick is')
+	def status(self, connection, nick, **kwargs):
+		pass
+
+	@command(False, LOGOUT, PRIVATE, '[nick] [secret]', 'log in as nick with secret or using masks')
+	def login(self, connection, nick, **kwargs):
+		pass
+
+	@command(False, LOGOUT, PRIVATE, '[nick] [secret', 'register as nick with secret')
+	def register(self, connection, nick, **kwargs):
+		pass
+
+	@command(False, LOGIN, PRIVATE, '', 'log out as nick')
+	def logout(self, connection, nick, **kwargs):
+		pass
+
+	@command(False, LOGIN, PRIVATE, '[variable [value]]', 'display or set variables')
+	def set(self, connection, nick, **kwargs):
+		pass
+
+	@command(False, LOGIN, PRIVATE, 'variable', 'unset a variable')
+	def unset(self, connection, nick, **kwargs):
+		pass
+
+	@command(True, ALWAYS, PRIVATE, 'channel', 'join a channel')
+	def join(self, connection, nick, **kwargs):
+		pass
+
+	@command(True, ALWAYS, PRIVATE, 'channel [message]', 'part from a channel')
+	def part(self, connection, nick, **kwargs):
+		pass
+
+	@command(True, ALWAYS, PRIVATE, '[message]', 'quit and do not come back')
+	def quit(self, connection, nick, **kwargs):
+		pass
+
+	@command(True, ALWAYS, PRIVATE, '', 'reload with more up to date code')
+	def reload(self, connection, nick, **kwargs):
+		pass
+
+	@command(True, ALWAYS, PRIVATE, '', 'quit and join running more up to date code')
+	def restart(self, connection, nick, **kwargs):
+		pass
+
+	@command(True, ALWAYS, PRIVATE, 'nick|channel message', 'say message to nick or channel')
+	def say(self, connection, nick, **kwargs):
+		pass
+
+	@command(True, ALWAYS, PRIVATE, '', 'show who is logged in')
+	def who(self, connection, nick, **kwargs):
+		pass
+
+	def __help(self, connection, nick, admin, login, arguments):
+		self.help(connection, nick, admin = admin, login = login, arguments = arguments)
 
 	def __join(self, connection, nick, arguments):
 		try:
@@ -1128,8 +1209,6 @@ if __name__ == '__main__':
 
 	try:
 		while bot.start():
-			import locationbot
-
 			try:
 				bot = reload(locationbot).LocationBot(bot)
 			except (ImportError, SyntaxError), error: