Skip to content

Commit

Permalink
[module] Remove @command, make @nickname_commands support multiple co…
Browse files Browse the repository at this point in the history
…mmands

Also rename the latter from @nickname_command. Affects Issue #276
  • Loading branch information
embolalia committed Jun 24, 2013
1 parent d0dda11 commit eae8303
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions willie/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
willie.module.rule
willie.module.thread
willie.module.name (deprecated)
willie.module.command
willie.module.nickname_command
willie.module.commands
willie.module.nickname_commands
willie.module.priority
willie.module.event
willie.module.rate
Expand Down Expand Up @@ -119,13 +119,13 @@ def name(value):
" Replace tuple-form .rule with a regexp.")


def command(value):
"""Decorator. Triggers on lines starting with "<command prefix>command".
def commands(*command_list):
"""Decorator. Sets a command list for a callable.
This decorator can be used multiple times to add multiple rules. The
resulting match object will have the command as the first group, rest of
the line, excluding leading whitespace, as the second group. Parameters
1 through 4, seperated by whitespace, will be groups 3-6.
This decorator can be used to add multiple commands to one callable in
a single line. The resulting match object will have the command as the
first group, rest of the line, excluding leading whitespace, as the second
group. Parameters 1 through 4, seperated by whitespace, will be groups 3-6.
Args:
command: A string, which can be a regular expression.
Expand All @@ -138,22 +138,10 @@ def command(value):
@command("hello"):
If the command prefix is "\.", this would trigger on lines starting
with ".hello".
"""
def add_attribute(function):
if not hasattr(function, "commands"):
function.commands = []
function.commands.append(value)
return function
return add_attribute
def commands(*command_list):
"""Decorator. Sets a command list for a callable
This decorator can be used to add multiple commands to one callable in
a single line.
Example:
@commands('j', 'join')
If the command prefix is "\.", this would trigger on lines starting
with either ".j" or ".join".
"""
def add_attribute(function):
if not hasattr(function, "commands"):
Expand All @@ -162,7 +150,8 @@ def add_attribute(function):
return function
return add_attribute

def nickname_command(command):

def nickname_commands(*command_list):
"""Decorator. Triggers on lines starting with "$nickname: command".
This decorator can be used multiple times to add multiple rules. The
Expand Down Expand Up @@ -193,18 +182,18 @@ def add_attribute(function):
rule = r"""
^
$nickname[:,]? # Nickname.
\s+({command}) # Command as group 0.
\s+({command}) # Command as group 1.
(?:\s+ # Whitespace to end command.
( # Rest of the line as group 1.
(?:(\S+))? # Parameters 1-4 as groups 2-5.
( # Rest of the line as group 2.
(?:(\S+))? # Parameters 1-4 as groups 3-6.
(?:\s+(\S+))?
(?:\s+(\S+))?
(?:\s+(\S+))?
.* # Accept anything after the parameters. Leave it up to
# the module to parse the line.
))? # Group 1 must be None, if there are no parameters.
$ # EoL, so there are no partial matches.
""".format(command=command)
""".format(command='|'.join(command_list))
function.rule.append(rule)
return function

Expand Down

0 comments on commit eae8303

Please sign in to comment.