Fix errors in cmdparser for post-index match-separators

This commit is contained in:
Griatch 2021-01-19 20:59:53 +01:00
parent b633b48141
commit 703ebf1fa1
2 changed files with 23 additions and 20 deletions

View file

@ -71,7 +71,7 @@ def build_matches(raw_string, cmdset, include_prefixes=False):
for cmdname in [cmd.key] + cmd.aliases for cmdname in [cmd.key] + cmd.aliases
if cmdname if cmdname
and l_raw_string.startswith(cmdname.lower()) and l_raw_string.startswith(cmdname.lower())
and (not cmd.arg_regex or cmd.arg_regex.match(l_raw_string[len(cmdname) :])) and (not cmd.arg_regex or cmd.arg_regex.match(l_raw_string[len(cmdname):]))
] ]
) )
else: else:
@ -90,7 +90,7 @@ def build_matches(raw_string, cmdset, include_prefixes=False):
if ( if (
cmdname cmdname
and l_raw_string.startswith(cmdname.lower()) and l_raw_string.startswith(cmdname.lower())
and (not cmd.arg_regex or cmd.arg_regex.match(l_raw_string[len(cmdname) :])) and (not cmd.arg_regex or cmd.arg_regex.match(l_raw_string[len(cmdname):]))
): ):
matches.append(create_match(cmdname, raw_string, cmd, raw_cmdname)) matches.append(create_match(cmdname, raw_string, cmd, raw_cmdname))
except Exception: except Exception:
@ -98,7 +98,7 @@ def build_matches(raw_string, cmdset, include_prefixes=False):
return matches return matches
def try_num_prefixes(raw_string): def try_num_differentiators(raw_string):
""" """
Test if user tried to separate multi-matches with a number separator Test if user tried to separate multi-matches with a number separator
(default 1-name, 2-name etc). This is usually called last, if no other (default 1-name, 2-name etc). This is usually called last, if no other
@ -126,7 +126,7 @@ def try_num_prefixes(raw_string):
# with a #num-command style syntax. We expect the regex to # with a #num-command style syntax. We expect the regex to
# contain the groups "number" and "name". # contain the groups "number" and "name".
mindex, new_raw_string = (num_ref_match.group("number"), num_ref_match.group("name")) mindex, new_raw_string = (num_ref_match.group("number"), num_ref_match.group("name"))
return mindex, new_raw_string return int(mindex), new_raw_string
else: else:
return None, None return None, None
@ -170,19 +170,22 @@ def cmdparser(raw_string, cmdset, caller, match_index=None):
if not raw_string: if not raw_string:
return [] return []
# find mathces, first using the full name # find matches, first using the full name
matches = build_matches(raw_string, cmdset, include_prefixes=True) matches = build_matches(raw_string, cmdset, include_prefixes=True)
if not matches:
# try to match a number 1-cmdname, 2-cmdname etc if not matches or len(matches) > 1:
mindex, new_raw_string = try_num_prefixes(raw_string) # no single match, try parsing for optional numerical tags like 1-cmd
if mindex is not None: # or cmd-2, cmd.2 etc
return cmdparser(new_raw_string, cmdset, caller, match_index=int(mindex)) match_index, new_raw_string = try_num_differentiators(raw_string)
if _CMD_IGNORE_PREFIXES: if match_index is not None:
# still no match. Try to strip prefixes matches.extend(build_matches(new_raw_string, cmdset, include_prefixes=True))
raw_string = (
raw_string.lstrip(_CMD_IGNORE_PREFIXES) if len(raw_string) > 1 else raw_string if not matches and _CMD_IGNORE_PREFIXES:
) # still no match. Try to strip prefixes
matches = build_matches(raw_string, cmdset, include_prefixes=False) raw_string = (
raw_string.lstrip(_CMD_IGNORE_PREFIXES) if len(raw_string) > 1 else raw_string
)
matches = build_matches(raw_string, cmdset, include_prefixes=False)
# only select command matches we are actually allowed to call. # only select command matches we are actually allowed to call.
matches = [match for match in matches if match[2].access(caller, "cmd")] matches = [match for match in matches if match[2].access(caller, "cmd")]

View file

@ -1182,10 +1182,10 @@ class TestCmdParser(TestCase):
) )
@override_settings(SEARCH_MULTIMATCH_REGEX=r"(?P<number>[0-9]+)-(?P<name>.*)") @override_settings(SEARCH_MULTIMATCH_REGEX=r"(?P<number>[0-9]+)-(?P<name>.*)")
def test_num_prefixes(self): def test_num_differentiators(self):
self.assertEqual(cmdparser.try_num_prefixes("look me"), (None, None)) self.assertEqual(cmdparser.try_num_differentiators("look me"), (None, None))
self.assertEqual(cmdparser.try_num_prefixes("look me-3"), ("3", "look me")) self.assertEqual(cmdparser.try_num_differentiators("look me-3"), (3, "look me"))
self.assertEqual(cmdparser.try_num_prefixes("look me-567"), ("567", "look me")) self.assertEqual(cmdparser.try_num_differentiators("look me-567"), (567, "look me"))
@override_settings( @override_settings(
SEARCH_MULTIMATCH_REGEX=r"(?P<number>[0-9]+)-(?P<name>.*)", CMD_IGNORE_PREFIXES="@&/+" SEARCH_MULTIMATCH_REGEX=r"(?P<number>[0-9]+)-(?P<name>.*)", CMD_IGNORE_PREFIXES="@&/+"