diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core_modules/command_spec.py | 48 | ||||
| -rw-r--r-- | src/utils/__init__.py | 2 | ||||
| -rw-r--r-- | src/utils/decorators.py | 15 | ||||
| -rw-r--r-- | src/utils/parse.py | 27 |
4 files changed, 59 insertions, 33 deletions
diff --git a/src/core_modules/command_spec.py b/src/core_modules/command_spec.py index 96a08e42..98b3c1db 100644 --- a/src/core_modules/command_spec.py +++ b/src/core_modules/command_spec.py @@ -38,12 +38,12 @@ class Module(ModuleManager.BaseModule): n = 0 error = None - if spec_type == "time" and args: + if spec_type.name == "time" and args: time, _ = utils.parse.timed_args(args) chunk = time n = 1 error = "Invalid timeframe" - elif spec_type == "rchannel": + elif spec_type.name == "rchannel": if channel: chunk = channel elif args: @@ -53,53 +53,53 @@ class Module(ModuleManager.BaseModule): error = "No such channel" else: error = "No channel provided" - elif spec_type == "channel" and args: + elif spec_type.name == "channel" and args: if args[0] in server.channels: chunk = server.channels.get(args[0]) n = 1 error = "No such channel" - elif spec_type == "cuser" and args: + elif spec_type.name == "cuser" and args: tuser = server.get_user(args[0], create=False) if tuser and channel.has_user(tuser): chunk = tuser n = 1 error = "That user is not in this channel" - elif spec_type == "ruser": + elif spec_type.name == "ruser": if args: chunk = server.get_user(args[0], create=False) n = 1 else: chunk = user error = "No such user" - elif spec_type == "user": + elif spec_type.name == "user": if args: chunk = server.get_user(args[0], create=False) n = 1 error = "No such user" else: error = "No user provided" - elif spec_type == "ouser" and args: + elif spec_type.name == "ouser" and args: if server.has_user_id(args[0]): chunk = server.get_user(args[0]) n = 1 error = "Unknown nickname" - elif spec_type == "word": + elif spec_type.name == "word": if args: chunk = args[0] n = 1 - elif spec_type == "...": + elif spec_type.name == "...": if args: chunk = " ".join(args) n = max(1, len(args)) - options.append([chunk, n, error]) + options.append([spec_type, chunk, n, error]) return options @utils.hook("preprocess.command") @utils.kwarg("priority", EventManager.PRIORITY_HIGH) def preprocess(self, event): - spec = event["hook"].get_kwarg("spec", None) - if not spec == None: + spec_types = event["hook"].get_kwarg("spec", None) + if not spec_types == None: server = event["server"] channel = event["target"] if event["is_channel"] else None user = event["user"] @@ -108,30 +108,20 @@ class Module(ModuleManager.BaseModule): out = [] kwargs = {"channel": channel} - for word in spec.split(): - optional = word[0] == "?" - word = word[1:] - - raw_spec_types = word.split("|") - spec_types = [t.replace("~", "", 1) for t in raw_spec_types] - + for item in spec_types: options = self._spec_chunk(server, kwargs["channel"], user, - spec_types, args) + item.types, args) found = None first_error = None - for i, (chunk, n, error) in enumerate(options): - spec_type = spec_types[i] - raw_spec_type = raw_spec_types[i] - - + for spec_type, chunk, n, error in options: if not chunk == None: - if "~" in raw_spec_type: - kwargs[raw_spec_type.split("~", 1)[1]] = chunk + if spec_type.exported: + kwargs[spec_type.exported] = chunk found = True args = args[n:] - if len(spec_types) > 1: + if len(item.types) > 1: chunk = [spec_type, chunk] found = chunk break @@ -143,7 +133,7 @@ class Module(ModuleManager.BaseModule): out.append(found) - if not optional and not found: + if not item.optional and not found: error = first_error or "Invalid arguments" return utils.consts.PERMISSION_HARD_FAIL, error diff --git a/src/utils/__init__.py b/src/utils/__init__.py index aad6f7a8..303c70c5 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -2,7 +2,7 @@ import contextlib, enum, ipaddress, multiprocessing, queue, signal, threading import typing from . import cli, consts, datetime, decorators, irc, http, parse, security -from .decorators import export, hook, kwarg +from .decorators import export, hook, kwarg, spec from .settings import (BoolSetting, FunctionSetting, IntRangeSetting, IntSetting, OptionsSetting, sensitive_format, SensitiveSetting, Setting) from .errors import (EventError, EventNotEnoughArgsError, EventResultsError, diff --git a/src/utils/decorators.py b/src/utils/decorators.py index dc4adf3a..ca47ea6e 100644 --- a/src/utils/decorators.py +++ b/src/utils/decorators.py @@ -1,4 +1,5 @@ import typing +from .parse import argument_spec BITBOT_MAGIC = "__bitbot" @@ -42,10 +43,18 @@ def export(setting: str, value: typing.Any): magic.add_export(setting, value) return module return _export_func + +def _kwarg(key: str, value: typing.Any, func: typing.Any): + magic = get_magic(func) + magic.add_kwarg(key, value) + return func + def kwarg(key: str, value: typing.Any): def _kwarg_func(func): - magic = get_magic(func) - magic.add_kwarg(key, value) - return func + return _kwarg(key, value, func) return _kwarg_func +def spec(spec: str): + def _spec_func(func): + return _kwarg("spec", argument_spec(spec), func) + return _spec_func diff --git a/src/utils/parse.py b/src/utils/parse.py index b45ca6fe..258b97dc 100644 --- a/src/utils/parse.py +++ b/src/utils/parse.py @@ -155,3 +155,30 @@ def format_token_replace(s: str, vars: typing.Dict[str, str], for i, token in tokens: s = s[:i] + vars[token.replace(sigil, "", 1)] + s[i+len(token):] return s + +class ArgumentSpecType(object): + def __init__(self, name: str, exported: str): + self.name = name + self.exported = exported + +class ArgumentSpec(object): + def __init__(self, optional: bool, types: typing.List[ArgumentSpecType]): + self.optional = optional + self.types = types + +def argument_spec(spec: str) -> typing.List[ArgumentSpec]: + out: typing.List[ArgumentSpec] = [] + for type_names_str in spec.split(" "): + optional = type_names_str[0] == "?" + type_names_str = type_names_str[1:] + + spec_types: typing.List[ArgumentSpecType] = [] + for type_name in type_names_str.split("|"): + exported_name = "" + if "~" in type_name: + exported_name = type_name + type_name = type_name.replace("~", "", 1) + + spec_types.append(ArgumentSpecType(type_name, exported_name)) + out.append(ArgumentSpec(optional, spec_types)) + return out |
