use utils.parse.format_token_replace() in aliases.py

This commit is contained in:
jesopo 2019-12-21 21:17:08 +00:00
parent bc1c77d8a8
commit 2218d4a888

View file

@ -1,35 +1,17 @@
#--depends-on commands #--depends-on commands
import re
from src import EventManager, ModuleManager, utils from src import EventManager, ModuleManager, utils
REGEX_ARG_NUMBER = re.compile(r"\$(?:(\d+)(-?)|(-))")
REGEX_ARG_NAME = re.compile(r"\$([^\$]+)\$")
SETTING_PREFIX = "command-alias-" SETTING_PREFIX = "command-alias-"
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _arg_replace(self, s, args_split, kwargs): def _arg_replace(self, s, args_split, kwargs):
parts = s.split("$$") vars = {}
for i, part in enumerate(parts): for i in range(len(args_split)):
for match in REGEX_ARG_NUMBER.finditer(part): vars[str(i)] = args_split[i]
if match.group(1): vars["%d-" % i] = " ".join(args_split[i:])
index = int(match.group(1)) vars["-"] = " ".join(args_split)
continuous = match.group(2) == "-" vars.update(kwargs)
if index >= len(args_split): return utils.parse.format_token_replace(s, vars)
raise IndexError("Unknown alias arg index")
else:
index = 0
continuous = True
if continuous:
replace = " ".join(args_split[index:])
else:
replace = args_split[index]
parts[i] = part.replace(match.group(0), replace)
for match in REGEX_ARG_NAME.finditer(part):
key = match.group(1).upper()
if key in kwargs:
parts[i] = part.replace(match.group(0), kwargs[key])
return "$".join(parts)
def _get_alias(self, server, target, command): def _get_alias(self, server, target, command):
setting = "%s%s" % (SETTING_PREFIX, command) setting = "%s%s" % (SETTING_PREFIX, command)