Created
January 10, 2010 13:19
-
-
Save bebraw/273515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Help: | |
aliases = 'help' | |
priority = 'low' | |
def execute(self, commands): | |
''' | |
>>> from mock import Mock | |
>>> clean = Mock() | |
>>> clean.aliases = 'clean' | |
>>> clean.description = 'Cleans up stored variables' | |
>>> variables = Mock() | |
>>> variables.aliases = ('variables', 'vars', ) | |
>>> variables.description = 'Shows stored variables' | |
>>> commands = [clean, variables] | |
>>> help = Help() | |
>>> help.execute(commands) | |
'clean - Cleans up stored variables\\nvariables, vars - Shows stored variables' | |
''' | |
ret = '' | |
for command in commands: | |
if hasattr(command, 'description'): | |
if hasattr(command.aliases, '__iter__'): | |
ret += ', '.join(command.aliases) | |
else: | |
ret += command.aliases | |
ret += ' - ' + command.description + '\n' | |
return ret.rstrip() | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SpecificHelp: | |
def matches(self, expression): | |
''' | |
>>> specific_help = SpecificHelp() | |
>>> specific_help.matches('help foo') | |
True | |
>>> specific_help.matches(' help foo ') | |
True | |
Accept just one parameter for now | |
>>> specific_help.matches('help foo bar') | |
False | |
''' | |
parts = expression.split() | |
if parts[0] == 'help': | |
if len(parts) == 2: | |
return True | |
return False | |
def execute(self, expression, commands): | |
''' | |
>>> from mock import Mock | |
>>> clean = Mock() | |
>>> clean.aliases = 'clean' | |
>>> clean.description = 'Cleans up stored variables' | |
>>> commands = Mock() | |
>>> def find(name): | |
... if name == 'clean': | |
... return clean | |
>>> commands.find = find | |
>>> specific_help = SpecificHelp() | |
>>> specific_help.execute(' help clean ', commands) | |
'Cleans up stored variables' | |
''' | |
target_name = expression.split()[1] | |
target_command = commands.find(target_name) | |
return target_command.description | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment