diff options
| author | 2019-06-26 14:37:26 +0100 | |
|---|---|---|
| committer | 2019-06-26 14:37:41 +0100 | |
| commit | c5785a2d1484a98616b0fb8b5a7cb728eb094435 (patch) | |
| tree | 470ace2c509805fb9b723592df196b4e9e0edb46 | |
| parent | Update IRCBot `except queue.Empty` comment (diff) | |
| signature | ||
implement @utils.kwarg() magic, use it for command.regex hooks
| -rw-r--r-- | modules/ducks.py | 12 | ||||
| -rw-r--r-- | modules/factoids.py | 11 | ||||
| -rw-r--r-- | modules/github.py | 22 | ||||
| -rw-r--r-- | modules/imgur.py | 17 | ||||
| -rw-r--r-- | modules/karma.py | 8 | ||||
| -rw-r--r-- | modules/sed.py | 6 | ||||
| -rw-r--r-- | modules/title.py | 11 | ||||
| -rw-r--r-- | modules/tweets/__init__.py | 8 | ||||
| -rw-r--r-- | modules/youtube.py | 12 | ||||
| -rw-r--r-- | src/ModuleManager.py | 20 | ||||
| -rw-r--r-- | src/utils/__init__.py | 5 | ||||
| -rw-r--r-- | src/utils/consts.py | 1 |
12 files changed, 75 insertions, 58 deletions
diff --git a/modules/ducks.py b/modules/ducks.py index 6b2f07d6..037c602b 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -1,7 +1,7 @@ #--depends-on commands #--depends-on config -import random, time +import random, re, time from src import EventManager, ModuleManager, utils DUCK = "・゜゜・。。・゜゜\_o< QUACK!" @@ -44,12 +44,12 @@ class Module(ModuleManager.BaseModule): if show_duck: self._trigger_duck(channel) - @utils.hook("command.regex", expect_output=False, ignore_action=False) + @utils.hook("command.regex") + @utils.kwarg("expect_output", False) + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "duck-trigger") + @utils.kwarg("patern", re.compile(".+")) def channel_message(self, event): - """ - :pattern: .+ - :command: duck-trigger - """ self._activity(event["target"]) def _trigger_duck(self, channel): diff --git a/modules/factoids.py b/modules/factoids.py index e4c93d34..955f7490 100644 --- a/modules/factoids.py +++ b/modules/factoids.py @@ -3,6 +3,8 @@ import re from src import ModuleManager, utils +REGEX_FACTOID = re.compile("{!factoid ([^}]+)}", re.I) + class Module(ModuleManager.BaseModule): def _get_factoid(self, server, factoid): name = factoid.lower().strip() @@ -26,12 +28,11 @@ class Module(ModuleManager.BaseModule): raise utils.EventError("Unknown factoid '%s'" % name) event["stdout"].write("%s: %s" % (name, value)) - @utils.hook("command.regex", ignore_action=False) + @utils.hook("command.regex") + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "factoid") + @utils.kwarg("pattern", REGEX_FACTOID) def channel_message(self, event): - """ - :command: factoid - :pattern: {!factoid ([^}]+)} - """ name, value = self._get_factoid(event["server"], event["match"].group(1)) if not value == None: diff --git a/modules/github.py b/modules/github.py index 9b245cc5..102f3ab2 100644 --- a/modules/github.py +++ b/modules/github.py @@ -15,6 +15,10 @@ COLOR_NEUTRAL = utils.consts.LIGHTGREY COLOR_NEGATIVE = utils.consts.RED COLOR_ID = utils.consts.PINK +REGEX_PR_OR_ISSUE = re.compile( + r"https?://github.com/([^/]+)/([^/]+)/(pull|issues)/(\d+)", re.I) +REGEX_REF = re.compile(r"(?:\S+(?:\/\S+)?)?#\d+") + API_ISSUE_URL = "https://api.github.com/repos/%s/%s/issues/%s" API_PULL_URL = "https://api.github.com/repos/%s/%s/pulls/%s" @@ -182,12 +186,11 @@ class Module(ModuleManager.BaseModule): else: return True - @utils.hook("command.regex", ignore_action=False) + @utils.hook("command.regex") + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "github") + @utils.kwarg("pattern", REGEX_PR_OR_ISSUE) def url_regex(self, event): - """ - :command: github - :pattern: https?://github.com/([^/]+)/([^/]+)/(pull|issues)/(\d+) - """ if event["target"].get_setting("auto-github", False): event.eat() ref = "%s/%s#%s" % (event["match"].group(1), @@ -202,12 +205,11 @@ class Module(ModuleManager.BaseModule): event["stdout"].hide_prefix() event["stdout"].write(result) - @utils.hook("command.regex", ignore_action=False) + @utils.hook("command.regex") + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "github") + @utils.kwarg("pattern", REGEX_REF) def ref_regex(self, event): - """ - :command: github - :pattern: (?:\S+(?:\/\S+)?)?#\d+ - """ if event["target"].get_setting("auto-github", False): event.eat() ref = event["match"].group(0) diff --git a/modules/imgur.py b/modules/imgur.py index 2cccaac6..fd68ed00 100644 --- a/modules/imgur.py +++ b/modules/imgur.py @@ -28,19 +28,20 @@ class Module(ModuleManager.BaseModule): text += "%s " % data["account_url"] return text - @utils.hook("command.regex", pattern=REGEX_IMAGE, ignore_action=False) + @utils.hook("command.regex") + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "imgur") + @utils.kwarg("pattern", REGEX_IMAGE) def _regex_image(self, event): - """ - :command: imgur - """ if event["target"].get_setting("auto-imgur", False): event["stdout"].write(self._parse_image(event["match"].group(1))) event.eat() - @utils.hook("command.regex", pattern=REGEX_GALLERY, ignore_action=False) + + @utils.hook("command.regex") + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "imgur") + @utils.kwarg("pattern", REGEX_GALLERY) def _regex_gallery(self, event): - """ - :command: imgur - """ if event["target"].get_setting("auto-imgur", False): event["stdout"].write(self._parse_gallery(event["match"].group(1))) event.eat() diff --git a/modules/karma.py b/modules/karma.py index 78eebe02..7650e430 100644 --- a/modules/karma.py +++ b/modules/karma.py @@ -8,6 +8,8 @@ from src import EventManager, ModuleManager, utils WORD_STOP = [",", ":"] KARMA_DELAY_SECONDS = 3 +REGEX_KARMA = re.compile(r"^(.*)(\+{2}|\-{2})$") + @utils.export("channelset", {"setting": "karma-verbose", "help": "Enable/disable automatically responding to karma changes", "validate": utils.bool_or_none, "example": "on"}) @@ -28,11 +30,9 @@ class Module(ModuleManager.BaseModule): event["user"].last_karma = None @utils.hook("command.regex") + @utils.kwarg("command", "karma") + @utils.kwarg("pattern", REGEX_KARMA) def channel_message(self, event): - """ - :command: karma - :pattern: ^(.*)(\+{2}|\-{2})$ - """ verbose = event["target"].get_setting("karma-verbose", False) nickname_only = event["server"].get_setting("karma-nickname-only", False) diff --git a/modules/sed.py b/modules/sed.py index 0c7d6f2a..20e85b68 100644 --- a/modules/sed.py +++ b/modules/sed.py @@ -19,11 +19,9 @@ class Module(ModuleManager.BaseModule): event["server"].get_setting(setting, default)) @utils.hook("command.regex") + @utils.kwarg("command", "sed") + @utils.kwarg("pattern", REGEX_SED) def channel_message(self, event): - """ - :command: sed - :pattern: ^s/ - """ sed_split = re.split(REGEX_SPLIT, event["message"], 3) if event["message"].startswith("s/") and len(sed_split) > 2: if not self._closest_setting(event, "sed", False): diff --git a/modules/title.py b/modules/title.py index 184b3393..a6e924cc 100644 --- a/modules/title.py +++ b/modules/title.py @@ -47,13 +47,12 @@ class Module(ModuleManager.BaseModule): else: return None - @utils.hook("command.regex", ignore_action=False, - priority=EventManager.PRIORITY_MONITOR) + @utils.hook("command.regex") + @utils.kwarg("ignore_action", False) + @utils.kwarg("priority", EventManager.PRIORITY_MONITOR) + @utils.kwarg("command", "title") + @utils.kwarg("pattern", utils.http.REGEX_URL) def channel_message(self, event): - """ - :command: title - :pattern-url: 1 - """ if event["target"].get_setting("auto-title", False): event.eat() url = event["match"].group(0) diff --git a/modules/tweets/__init__.py b/modules/tweets/__init__.py index 16f179d8..e58dbb6a 100644 --- a/modules/tweets/__init__.py +++ b/modules/tweets/__init__.py @@ -167,11 +167,11 @@ class Module(ModuleManager.BaseModule): else: event["stderr"].write("No tweet provided to get information about") - @utils.hook("command.regex", pattern=REGEX_TWITTERURL, ignore_action=False) + @utils.hook("command.regex") + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "tweet") + @utils.kwarg("pattern", REGEX_TWITTERURL) def regex(self, event): - """ - :command: tweet - """ if event["target"].get_setting("auto-tweet", False): event.eat() tweet_id = event["match"].group(1) diff --git a/modules/youtube.py b/modules/youtube.py index 2b89d021..f31b15ec 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -130,14 +130,12 @@ class Module(ModuleManager.BaseModule): else: event["stderr"].write("No search phrase provided") - @utils.hook("command.regex", ignore_action=False, - priority=EventManager.PRIORITY_LOW) + @utils.hook("command.regex") + @utils.kwarg("priority", EventManager.PRIORITY_LOW) + @utils.kwarg("ignore_action", False) + @utils.kwarg("command", "youtube") + @utils.kwarg("pattern", REGEX_YOUTUBE) def channel_message(self, event): - """ - :command: youtube - :-pattern: https?://(?:www.)? - (?:youtu.be/|youtube.com/watch\?[\S]*v=)([\w\-]{11}) - """ if event["target"].get_setting("auto-youtube", False): youtube_id = event["match"].group(1) video_details = self.video_details(youtube_id) diff --git a/src/ModuleManager.py b/src/ModuleManager.py index 57fbde3b..45866d23 100644 --- a/src/ModuleManager.py +++ b/src/ModuleManager.py @@ -205,12 +205,24 @@ class ModuleManager(object): if not hasattr(module_object, "_name"): module_object._name = definition.name.title() + + # @utils.hook() magic for attribute_name in dir(module_object): attribute = getattr(module_object, attribute_name) - for hook in self._get_magic(attribute, - utils.consts.BITBOT_HOOKS_MAGIC, []): - context_events.on(hook["event"]).hook(attribute, - **hook["kwargs"]) + if inspect.ismethod(attribute): + kwargs = self._get_magic(attribute, + utils.consts.BITBOT_KWARG_MAGIC, []) + kwargs = dict(list(d.items())[0] for d in kwargs) + + for hook in self._get_magic(attribute, + utils.consts.BITBOT_HOOKS_MAGIC, []): + new_kwargs = kwargs.copy() + new_kwargs.update(hook["kwargs"]) + + context_events.on(hook["event"]).hook(attribute, + **new_kwargs) + + # @utils.export() magic for export in self._get_magic(module_object, utils.consts.BITBOT_EXPORTS_MAGIC, []): context_exports.add(export["setting"], export["value"]) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 4e947c4b..de812f5b 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -184,6 +184,11 @@ def export(setting: str, value: typing.Any): {"setting": setting, "value": value}) return module return _export_func +def kwarg(key: str, value: typing.Any): + def _kwarg_func(func): + _set_get_append(func, consts.BITBOT_KWARG_MAGIC, {key: value}) + return func + return _kwarg_func class MultiCheck(object): def __init__(self, diff --git a/src/utils/consts.py b/src/utils/consts.py index f5272cab..0c57b4b0 100644 --- a/src/utils/consts.py +++ b/src/utils/consts.py @@ -2,6 +2,7 @@ import typing from . import _consts_256_color BITBOT_HOOKS_MAGIC = "__bitbot_hooks" +BITBOT_KWARG_MAGIC = "__bitbot_kwarg" BITBOT_EXPORTS_MAGIC = "__bitbot_exports" class IRCColor(object): |
