From eae8303f3c270dd0dc99f5f39933f1aae3b4f2e5 Mon Sep 17 00:00:00 2001 From: Edward Powell Date: Mon, 24 Jun 2013 14:07:28 -0400 Subject: [PATCH] [module] Remove @command, make @nickname_commands support multiple commands Also rename the latter from @nickname_command. Affects Issue #276 --- willie/module.py | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/willie/module.py b/willie/module.py index 6debcb2c61..cd6b2aacab 100644 --- a/willie/module.py +++ b/willie/module.py @@ -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 @@ -119,13 +119,13 @@ def name(value): " Replace tuple-form .rule with a regexp.") -def command(value): - """Decorator. Triggers on lines starting with "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. @@ -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"): @@ -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 @@ -193,10 +182,10 @@ 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+))? @@ -204,7 +193,7 @@ def add_attribute(function): # 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