refactor command_spec.py

This commit is contained in:
jesopo 2020-01-26 15:40:36 +00:00
parent 211bcc70a8
commit ba1800b207

View file

@ -79,10 +79,11 @@ class Module(ModuleManager.BaseModule):
else: else:
error = "No user provided" error = "No user provided"
elif argument_type.type == "ouser": elif argument_type.type == "ouser":
if args and server.has_user_id(args[0]): if args:
value = server.get_user(args[0], create=True) if server.has_user_id(args[0]):
value = server.get_user(args[0], create=True)
error = "Unknown nickname"
n = 1 n = 1
error = "Unknown nickname"
elif argument_type.type == "nuser": elif argument_type.type == "nuser":
if args: if args:
value = server.get_user(args[0], create=True) value = server.get_user(args[0], create=True)
@ -91,6 +92,17 @@ class Module(ModuleManager.BaseModule):
options.append([argument_type, value, n, error]) options.append([argument_type, value, n, error])
return options return options
def _argument_types(self, options, args):
current_error = None
for argument_type, value, n, error in options:
if not value == None:
return [argument_type, n, value]
elif error:
current_error = error
elif n > len(args):
current_error = "Not enough arguments"
return [None, -1, current_error or "Invalid arguments"]
@utils.hook("preprocess.command") @utils.hook("preprocess.command")
@utils.kwarg("priority", EventManager.PRIORITY_HIGH) @utils.kwarg("priority", EventManager.PRIORITY_HIGH)
def preprocess(self, event): def preprocess(self, event):
@ -100,43 +112,50 @@ class Module(ModuleManager.BaseModule):
channel = event["target"] if event["is_channel"] else None channel = event["target"] if event["is_channel"] else None
user = event["user"] user = event["user"]
first_error = None overall_error = None
best_count = 0
for spec_arguments in specs: for spec_arguments in specs:
out = [] out = {}
args = event["args_split"].copy() args = event["args_split"].copy()
kwargs = {"channel": channel} kwargs = {"channel": channel}
failed = False failed = False
for spec_argument in spec_arguments: current_error = None
count = 0
for i, spec_argument in enumerate(spec_arguments):
argument_type_multi = len(set( argument_type_multi = len(set(
t.type for t in spec_argument.types)) > 1 t.type for t in spec_argument.types)) > 1
options = self._spec_value(server, kwargs["channel"], user, options = self._spec_value(server, kwargs["channel"], user,
spec_argument.types, args) spec_argument.types, args)
found = None argument_type, n, value = self._argument_types(options, args)
for argument_type, value, n, error in options: if not argument_type == None:
if not value == None: args = args[n:]
if argument_type.exported:
kwargs[argument_type.exported] = value
args = args[n:] if argument_type.exported:
if argument_type_multi: kwargs[argument_type.exported] = value
value = [argument_type.type, value]
found = value
break
elif not error and n > len(args):
error = "Not enough arguments"
first_error = first_error or error if argument_type_multi:
if not found == None: value = [argument_type.type, value]
out.append(found)
elif not spec_argument.optional: out[i] = value
argument_type_name = argument_type.name()
if argument_type_name:
out[argument_type_name] = value
elif spec_argument.optional:
out.append(None)
else:
failed = True failed = True
current_error = value
break break
if not failed: if not failed:
kwargs["spec"] = out kwargs["spec"] = out
event["kwargs"].update(kwargs) event["kwargs"].update(kwargs)
return return
else:
if count >= best_count:
overall_error = current_error
error = first_error or "Invalid arguments" return utils.consts.PERMISSION_HARD_FAIL, overall_error
return utils.consts.PERMISSION_HARD_FAIL, error