Skip to content

Commit

Permalink
Removed inspection of __doc__ strings; python -OO removes them
Browse files Browse the repository at this point in the history
The _handlecommand decorator inspected the __doc__ strings of methods to
find their help text. "python -OO" strips docstrings, though. This removes
the help text into an additional argument to _handlecommand.
  • Loading branch information
kstrauser committed Jun 6, 2011
1 parent e7d3400 commit 2d336ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
*.pyc
*.py[co]
MANIFEST
dist/*
daycos.ini
Expand Down
24 changes: 13 additions & 11 deletions seshat/server.py
Expand Up @@ -36,21 +36,21 @@

class CommandDefinition(object):
"""Describes a function that implements a chat command"""
def __init__(self, function, pattern, help_):
def __init__(self, function, pattern, helptext):
"""See: sqlitebackend.ChatInfo.__init__.__doc__"""
self.function = function
self.pattern = pattern
self.help = help_
self.helptext = helptext

def _handlecommand(pattern):
def _handlecommand(pattern, helptext):
"""Associate the decorated function with the given regular
expression. All commands start with '!', followed immediately by
the pattern, then optional whitespace until the end of the
line."""
def register(function):
"""Register the function"""
MODULELOG.debug("Registering %s to handle %s" % (function, pattern))
COMMANDPATTERNS.append(CommandDefinition(function, re.compile('^!%s\s*$' % pattern, re.IGNORECASE), function.__doc__))
COMMANDPATTERNS.append(CommandDefinition(function, re.compile('^!%s\s*$' % pattern, re.IGNORECASE), helptext))
return function
return register

Expand Down Expand Up @@ -147,6 +147,8 @@ def _messagehandler(self, con, event):
return
localuser = event.getFrom().getStripped()
message = event.getBody()
if message is None:
return
for pattern in COMMANDPATTERNS:
matchresult = pattern.pattern.match(message)
if matchresult is not None:
Expand Down Expand Up @@ -177,7 +179,7 @@ def _presencehandler(self, con, presence):

#### Command handlers - these act on commands from localusers

@_handlecommand('ACCEPT (\d+)')
@_handlecommand('ACCEPT (\d+)', '!ACCEPT n - Accept chat request #n')
def _command_accept(self, localuser, chatid):
"""!ACCEPT n - Accept chat request #n"""
chatid = int(chatid)
Expand Down Expand Up @@ -206,7 +208,7 @@ def _command_accept(self, localuser, chatid):
self._queueremote(chatid, "Your chat has started.")
MODULELOG.info("%s accepted chat #%d with %s" % (localuser, chatinfo.chatid, chatinfo.remoteuser))

@_handlecommand('CANCEL (\d+)')
@_handlecommand('CANCEL (\d+)', '!CANCEL n - Cancel chat request #n')
def _command_cancel(self, localuser, chatid):
"""!CANCEL n - Cancel chat request #n"""
chatid = int(chatid)
Expand All @@ -228,7 +230,7 @@ def _command_cancel(self, localuser, chatid):
self._queueremote(chatid, "Your chat was canceled.")
MODULELOG.info("%s canceled chat #%d" % (localuser, chatid))

@_handlecommand('FINISH')
@_handlecommand('FINISH', '!FINISH - Close your current chat')
def _command_finish(self, localuser):
"""!FINISH - Close your current chat"""
currentchat = self._getlocaluserchat(localuser)
Expand All @@ -239,13 +241,13 @@ def _command_finish(self, localuser):
self._localsend(localuser, "The chat is now closed.")
self._queueremote(currentchat.chatid, "The chat is now closed.")

@_handlecommand('HELP')
@_handlecommand('HELP', '!HELP - Show available commands')
def _command_help(self, localuser):
"""!HELP - Show available commands"""
self._localsend(localuser,
"Available options:\n" + '\n'.join(sorted(pattern.help for pattern in COMMANDPATTERNS)))
"Available options:\n" + '\n'.join(sorted(pattern.helptext for pattern in COMMANDPATTERNS)))

@_handlecommand('STATUS')
@_handlecommand('STATUS', '!STATUS - Show your current chat status')
def _command_status(self, localuser):
"""!STATUS - Show your current chat status"""
currentchat = self._getlocaluserchat(localuser)
Expand All @@ -255,7 +257,7 @@ def _command_status(self, localuser):
self._localsend(localuser, "You are in chat #%d with %s. Send '!FINISH' when you are done." % (
currentchat.chatid, currentchat.remoteuser))

@_handlecommand('WAITING')
@_handlecommand('WAITING', '!WAITING - Show all open chat requests')
def _command_waiting(self, localuser):
"""!WAITING - Show all open chat requests"""
waitingchats = self._getchatswithstatus(self.STATUS_WAITING) + self._getchatswithstatus(self.STATUS_NOTIFIED)
Expand Down

0 comments on commit 2d336ee

Please sign in to comment.