implement @utils.kwarg() magic, use it for command.regex hooks

This commit is contained in:
jesopo 2019-06-26 14:37:26 +01:00
parent 7060a0ac67
commit c5785a2d14
12 changed files with 75 additions and 58 deletions

View file

@ -1,7 +1,7 @@
#--depends-on commands #--depends-on commands
#--depends-on config #--depends-on config
import random, time import random, re, time
from src import EventManager, ModuleManager, utils from src import EventManager, ModuleManager, utils
DUCK = "・゜゜・。。・゜゜\_o< QUACK!" DUCK = "・゜゜・。。・゜゜\_o< QUACK!"
@ -44,12 +44,12 @@ class Module(ModuleManager.BaseModule):
if show_duck: if show_duck:
self._trigger_duck(channel) 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): def channel_message(self, event):
"""
:pattern: .+
:command: duck-trigger
"""
self._activity(event["target"]) self._activity(event["target"])
def _trigger_duck(self, channel): def _trigger_duck(self, channel):

View file

@ -3,6 +3,8 @@
import re import re
from src import ModuleManager, utils from src import ModuleManager, utils
REGEX_FACTOID = re.compile("{!factoid ([^}]+)}", re.I)
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _get_factoid(self, server, factoid): def _get_factoid(self, server, factoid):
name = factoid.lower().strip() name = factoid.lower().strip()
@ -26,12 +28,11 @@ class Module(ModuleManager.BaseModule):
raise utils.EventError("Unknown factoid '%s'" % name) raise utils.EventError("Unknown factoid '%s'" % name)
event["stdout"].write("%s: %s" % (name, value)) 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): def channel_message(self, event):
"""
:command: factoid
:pattern: {!factoid ([^}]+)}
"""
name, value = self._get_factoid(event["server"], name, value = self._get_factoid(event["server"],
event["match"].group(1)) event["match"].group(1))
if not value == None: if not value == None:

View file

@ -15,6 +15,10 @@ COLOR_NEUTRAL = utils.consts.LIGHTGREY
COLOR_NEGATIVE = utils.consts.RED COLOR_NEGATIVE = utils.consts.RED
COLOR_ID = utils.consts.PINK 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_ISSUE_URL = "https://api.github.com/repos/%s/%s/issues/%s"
API_PULL_URL = "https://api.github.com/repos/%s/%s/pulls/%s" API_PULL_URL = "https://api.github.com/repos/%s/%s/pulls/%s"
@ -182,12 +186,11 @@ class Module(ModuleManager.BaseModule):
else: else:
return True 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): def url_regex(self, event):
"""
:command: github
:pattern: https?://github.com/([^/]+)/([^/]+)/(pull|issues)/(\d+)
"""
if event["target"].get_setting("auto-github", False): if event["target"].get_setting("auto-github", False):
event.eat() event.eat()
ref = "%s/%s#%s" % (event["match"].group(1), ref = "%s/%s#%s" % (event["match"].group(1),
@ -202,12 +205,11 @@ class Module(ModuleManager.BaseModule):
event["stdout"].hide_prefix() event["stdout"].hide_prefix()
event["stdout"].write(result) 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): def ref_regex(self, event):
"""
:command: github
:pattern: (?:\S+(?:\/\S+)?)?#\d+
"""
if event["target"].get_setting("auto-github", False): if event["target"].get_setting("auto-github", False):
event.eat() event.eat()
ref = event["match"].group(0) ref = event["match"].group(0)

View file

@ -28,19 +28,20 @@ class Module(ModuleManager.BaseModule):
text += "%s " % data["account_url"] text += "%s " % data["account_url"]
return text 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): def _regex_image(self, event):
"""
:command: imgur
"""
if event["target"].get_setting("auto-imgur", False): if event["target"].get_setting("auto-imgur", False):
event["stdout"].write(self._parse_image(event["match"].group(1))) event["stdout"].write(self._parse_image(event["match"].group(1)))
event.eat() 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): def _regex_gallery(self, event):
"""
:command: imgur
"""
if event["target"].get_setting("auto-imgur", False): if event["target"].get_setting("auto-imgur", False):
event["stdout"].write(self._parse_gallery(event["match"].group(1))) event["stdout"].write(self._parse_gallery(event["match"].group(1)))
event.eat() event.eat()

View file

@ -8,6 +8,8 @@ from src import EventManager, ModuleManager, utils
WORD_STOP = [",", ":"] WORD_STOP = [",", ":"]
KARMA_DELAY_SECONDS = 3 KARMA_DELAY_SECONDS = 3
REGEX_KARMA = re.compile(r"^(.*)(\+{2}|\-{2})$")
@utils.export("channelset", {"setting": "karma-verbose", @utils.export("channelset", {"setting": "karma-verbose",
"help": "Enable/disable automatically responding to karma changes", "help": "Enable/disable automatically responding to karma changes",
"validate": utils.bool_or_none, "example": "on"}) "validate": utils.bool_or_none, "example": "on"})
@ -28,11 +30,9 @@ class Module(ModuleManager.BaseModule):
event["user"].last_karma = None event["user"].last_karma = None
@utils.hook("command.regex") @utils.hook("command.regex")
@utils.kwarg("command", "karma")
@utils.kwarg("pattern", REGEX_KARMA)
def channel_message(self, event): def channel_message(self, event):
"""
:command: karma
:pattern: ^(.*)(\+{2}|\-{2})$
"""
verbose = event["target"].get_setting("karma-verbose", False) verbose = event["target"].get_setting("karma-verbose", False)
nickname_only = event["server"].get_setting("karma-nickname-only", nickname_only = event["server"].get_setting("karma-nickname-only",
False) False)

View file

@ -19,11 +19,9 @@ class Module(ModuleManager.BaseModule):
event["server"].get_setting(setting, default)) event["server"].get_setting(setting, default))
@utils.hook("command.regex") @utils.hook("command.regex")
@utils.kwarg("command", "sed")
@utils.kwarg("pattern", REGEX_SED)
def channel_message(self, event): def channel_message(self, event):
"""
:command: sed
:pattern: ^s/
"""
sed_split = re.split(REGEX_SPLIT, event["message"], 3) sed_split = re.split(REGEX_SPLIT, event["message"], 3)
if event["message"].startswith("s/") and len(sed_split) > 2: if event["message"].startswith("s/") and len(sed_split) > 2:
if not self._closest_setting(event, "sed", False): if not self._closest_setting(event, "sed", False):

View file

@ -47,13 +47,12 @@ class Module(ModuleManager.BaseModule):
else: else:
return None return None
@utils.hook("command.regex", ignore_action=False, @utils.hook("command.regex")
priority=EventManager.PRIORITY_MONITOR) @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): def channel_message(self, event):
"""
:command: title
:pattern-url: 1
"""
if event["target"].get_setting("auto-title", False): if event["target"].get_setting("auto-title", False):
event.eat() event.eat()
url = event["match"].group(0) url = event["match"].group(0)

View file

@ -167,11 +167,11 @@ class Module(ModuleManager.BaseModule):
else: else:
event["stderr"].write("No tweet provided to get information about") 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): def regex(self, event):
"""
:command: tweet
"""
if event["target"].get_setting("auto-tweet", False): if event["target"].get_setting("auto-tweet", False):
event.eat() event.eat()
tweet_id = event["match"].group(1) tweet_id = event["match"].group(1)

View file

@ -130,14 +130,12 @@ class Module(ModuleManager.BaseModule):
else: else:
event["stderr"].write("No search phrase provided") event["stderr"].write("No search phrase provided")
@utils.hook("command.regex", ignore_action=False, @utils.hook("command.regex")
priority=EventManager.PRIORITY_LOW) @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): 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): if event["target"].get_setting("auto-youtube", False):
youtube_id = event["match"].group(1) youtube_id = event["match"].group(1)
video_details = self.video_details(youtube_id) video_details = self.video_details(youtube_id)

View file

@ -205,12 +205,24 @@ class ModuleManager(object):
if not hasattr(module_object, "_name"): if not hasattr(module_object, "_name"):
module_object._name = definition.name.title() module_object._name = definition.name.title()
# @utils.hook() magic
for attribute_name in dir(module_object): for attribute_name in dir(module_object):
attribute = getattr(module_object, attribute_name) attribute = getattr(module_object, attribute_name)
for hook in self._get_magic(attribute, if inspect.ismethod(attribute):
utils.consts.BITBOT_HOOKS_MAGIC, []): kwargs = self._get_magic(attribute,
context_events.on(hook["event"]).hook(attribute, utils.consts.BITBOT_KWARG_MAGIC, [])
**hook["kwargs"]) 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, for export in self._get_magic(module_object,
utils.consts.BITBOT_EXPORTS_MAGIC, []): utils.consts.BITBOT_EXPORTS_MAGIC, []):
context_exports.add(export["setting"], export["value"]) context_exports.add(export["setting"], export["value"])

View file

@ -184,6 +184,11 @@ def export(setting: str, value: typing.Any):
{"setting": setting, "value": value}) {"setting": setting, "value": value})
return module return module
return _export_func 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): class MultiCheck(object):
def __init__(self, def __init__(self,

View file

@ -2,6 +2,7 @@ import typing
from . import _consts_256_color from . import _consts_256_color
BITBOT_HOOKS_MAGIC = "__bitbot_hooks" BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
BITBOT_KWARG_MAGIC = "__bitbot_kwarg"
BITBOT_EXPORTS_MAGIC = "__bitbot_exports" BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
class IRCColor(object): class IRCColor(object):