From e3070f52fffec8077648ef71143459e06269b78b Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 17 Jan 2020 15:31:12 +0000 Subject: [PATCH] refactor format_activity to use {FORMATTING} for maybe-colored things --- src/core_modules/format_activity.py | 187 +++++++++++----------------- 1 file changed, 73 insertions(+), 114 deletions(-) diff --git a/src/core_modules/format_activity.py b/src/core_modules/format_activity.py index 1d93eb94..6eb4f070 100644 --- a/src/core_modules/format_activity.py +++ b/src/core_modules/format_activity.py @@ -5,8 +5,20 @@ class Module(ModuleManager.BaseModule): def _color(self, nickname): return utils.irc.hash_colorize(nickname) - def _event(self, type, server, line, context, minimal=None, pretty=None, - channel=None, user=None, **kwargs): + def _event(self, type, server, line, context, minimal=None, channel=None, + user=None, formatting={}, **kwargs): + pretty = line + minimal = minimal or line + + if user: + formatting["NICK"] = user.nickname + line = line.format(**formatting) + minimal = minimal.format(**formatting) + + for key, value in formatting.items(): + formatting[key] = self._color(value) + pretty = pretty.format(**formatting) + self.events.on("formatted").on(type).call(server=server, context=context, line=line, channel=channel, user=user, minimal=minimal, pretty=pretty, **kwargs) @@ -25,44 +37,34 @@ class Module(ModuleManager.BaseModule): symbols = self._mode_symbols(user, channel, event["server"]) if event["action"]: - format = "* %s%s %s" + format = "* %s{NICK} %s" else: - format = "<%s%s> %s" + format = "<%s{NICK}> %s" - minimal = format % ("", user.nickname, event["message"]) - normal = format % (symbols, user.nickname, event["message"]) - pretty = format % (symbols, self._color(user.nickname), - event["message"]) - - return minimal, normal, pretty + return format % (symbols, event["message"]) @utils.hook("send.message.channel") @utils.hook("received.message.channel") def channel_message(self, event): - minimal, normal, pretty = self._privmsg(event, event["channel"], - event["user"]) + line = self._privmsg(event, event["channel"], event["user"]) - self._event("message.channel", event["server"], normal, + self._event("message.channel", event["server"], line, event["channel"].name, channel=event["channel"], user=event["user"], - parsed_line=event["line"], minimal=minimal, pretty=pretty) + parsed_line=event["line"]) def _on_notice(self, event, user, channel): symbols = "" if channel: symbols = self._mode_symbols(user, channel, event["server"]) - format = "-%s%s- %s" - minimal = format % ("", user.nickname, event["message"]) - normal = format % (symbols, user.nickname, event["message"]) - pretty = format % (symbols, self._color(user.nickname), - event["message"]) + return "-%s{NICK}- %s" % (symbols, event["message"]) - return minimal, normal, pretty def _channel_notice(self, event, user, channel): - minimal, normal, pretty = self._on_notice(event, user, channel) - self._event("notice.channel", event["server"], normal, + line = self._on_notice(event, user, channel) + + self._event("notice.channel", event["server"], line, event["channel"].name, parsed_line=event["line"], channel=channel, - user=event["user"], minimal=minimal, pretty=pretty) + user=event["user"]) @utils.hook("received.notice.channel") @utils.hook("send.notice.channel") @@ -72,24 +74,19 @@ class Module(ModuleManager.BaseModule): @utils.hook("received.notice.private") @utils.hook("send.notice.private") def private_notice(self, event): - minimal, normal, pretty = self._on_notice(event, event["user"], None) - self._event("notice.private", event["server"], normal, + line= self._on_notice(event, event["user"], None) + self._event("notice.private", event["server"], line, event["target"].nickname, parsed_line=event["line"], - user=event["user"], minimal=minimal, pretty=pretty) + user=event["user"]) def _on_join(self, event, user): channel_name = event["channel"].name - minimal = "%s joined %s" % (user.nickname, channel_name) + minimal = "{NICK} joined %s" % channel_name + line = "- {NICK} (%s) joined %s" % (user.userhost(), channel_name) - normal_format = "- %s (%s) joined %s" - normal = normal_format % (user.nickname, user.userhost(), channel_name) - pretty = normal_format % (self._color(user.nickname), user.userhost(), - channel_name) - - self._event("join", event["server"], normal, event["channel"].name, - channel=event["channel"], user=user, minimal=minimal, - pretty=pretty) + self._event("join", event["server"], line, event["channel"].name, + channel=event["channel"], user=user, minimal=minimal) @utils.hook("received.join") def join(self, event): self._on_join(event, event["user"]) @@ -102,32 +99,22 @@ class Module(ModuleManager.BaseModule): username = event["username"] hostname = event["hostname"] - format = "%s changed host to %s@%s" - minimal = format % (event["user"].nickname, username, hostname) + minimal = "{NICK} changed host to %s@%s" % (username, hostname) + line = "- %s" % minimal - normal_format = "- %s" % format - normal = normal_format % (event["user"].nickname, username, hostname) - pretty = normal_format % (self._color(event["user"].nickname), username, - hostname) - - self._event("chghost", event["server"], normal, None, - user=event["user"], minimal=minimal, pretty=pretty) + self._event("chghost", event["server"], line, None, user=event["user"], + minimal=minimal) def _on_part(self, event, user): channel_name = event["channel"].name reason = event["reason"] reason = "" if not reason else " (%s)" % reason - format = "%s left %s%s" - minimal = format % (user.nickname, channel_name, reason) + minimal = "{NICK} left %s%s" % (channel_name, reason) + line = "- %s" % minimal - normal_format = "- %s" % format - normal = normal_format % (user.nickname, channel_name, reason) - pretty = normal_format % (self._color(user.nickname), channel_name, - reason) - - self._event("part", event["server"], normal, event["channel"].name, - channel=event["channel"], user=user, minimal=minimal, pretty=pretty) + self._event("part", event["server"], line, event["channel"].name, + channel=event["channel"], user=user, minimal=minimal) @utils.hook("received.part") def part(self, event): self._on_part(event, event["user"]) @@ -136,19 +123,14 @@ class Module(ModuleManager.BaseModule): self._on_part(event, event["server"].get_user(event["server"].nickname)) def _on_nick(self, event, user): - old_nickname = event["old_nickname"] - new_nickname = event["new_nickname"] + formatting = {"ONICK": event["old_nickname"], + "NNICK": event["new_nickname"]} - format = "%s changed nickname to %s" - minimal = format % (old_nickname, new_nickname) + minimal = "{ONICK} changed nickname to {NNICK}" + line = "- %s" % minimal - normal_format = "- %s" % format - normal = normal_format % (old_nickname, new_nickname) - pretty = normal_format % ( - self._color(old_nickname), self._color(new_nickname)) - - self._event("nick", event["server"], normal, None, user=user, - minimal=minimal, pretty=pretty) + self._event("nick", event["server"], line, None, user=user, + minimal=minimal, formatting=formatting) @utils.hook("received.nick") def nick(self, event): self._on_nick(event, event["user"]) @@ -163,18 +145,14 @@ class Module(ModuleManager.BaseModule): @utils.hook("received.invite") def invite(self, event): - format = "%s invited %s to %s" + formatting = {"TNICK": event["target_user"].nickname} - sender = event["user"].nickname - target = event["target_user"].nickname channel_name = event["target_channel"] + minimal = "{NICK} invited {TNICK} to %s" % channel_name + line = "- %s" % minimal - minimal = format % (sender, target, channel_name) - normal = "- %s" % minimal - pretty = format % (self._color(sender), target, channel_name) - - self._event("invite", event["server"], normal, event["target_channel"], - minimal=minimal, pretty=pretty) + self._event("invite", event["server"], line, event["target_channel"], + user=user, minimal=minimal, formatting=formatting) @utils.hook("received.mode.channel") def mode(self, event): @@ -183,29 +161,21 @@ class Module(ModuleManager.BaseModule): if args: args = " %s" % args - format = "%s set mode %s%s" - minimal = format % (event["user"].nickname, modes, args) + minimal = "{NICK} set mode %s%s" % (modes, args) + line = "- %s" % minimal - normal_format = "- %s" % format - normal = normal_format % (event["user"].nickname, modes, args) - pretty = normal_format % (self._color(event["user"].nickname), modes, - args) - - self._event("mode.channel", event["server"], normal, + self._event("mode.channel", event["server"], line, event["channel"].name, channel=event["channel"], user=event["user"], - minimal=minimal, pretty=pretty) + minimal=minimal) def _on_topic(self, event, nickname, action, topic): - format = "topic %s by %s: %s" - minimal = format % (action, nickname, topic) + formatting = {"TNICK": nickname} + minimal = "topic %s by {TNICK}: %s" % (action, topic) + line = "- %s" % minimal - normal_format = "- %s" % format - normal = normal_format % (action, nickname, topic) - pretty = normal_format % (action, self._color(nickname), topic) - - self._event("topic", event["server"], normal, event["channel"].name, - channel=event["channel"], user=event.get("user", None), - minimal=minimal, pretty=pretty) + self._event("topic", event["server"], line, event["channel"].name, + channel=event["channel"], user=event["user"], minimal=minimal, + formatting=formatting) @utils.hook("received.topic") def on_topic(self, event): self._on_topic(event, event["user"].nickname, "changed", @@ -219,32 +189,25 @@ class Module(ModuleManager.BaseModule): utils.datetime.datetime_timestamp(event["set_at"])) minimal = "topic set at %s" % dt - normal = "- %s" % minimal + line = "- %s" % minimal - self._event("topic-timestamp", event["server"], normal, + self._event("topic-timestamp", event["server"], line, event["channel"].name, channel=event["channel"], minimal=minimal) def _on_kick(self, event, kicked_nickname): - sender_nickname = event["user"].nickname + formatting = {"KNICK": kicked_nickname} channel_name = event["channel"].name reason = "" if event["reason"]: reason = " (%s)" % event["reason"] - format = "%s kicked %s from %s%s" - minimal = format % (sender_nickname, kicked_nickname, channel_name, - reason) + minimal = "{NICK} kicked {KNICK} from %s%s" % (channel_name, reason) + line = "- %s" % minimal - normal_format = "- %s" % format - normal = normal_format % (sender_nickname, kicked_nickname, - channel_name, reason) - pretty = normal_format % (self._color(sender_nickname), - self._color(kicked_nickname), channel_name, reason) - - self._event("kick", event["server"], normal, event["channel"].name, - channel=event["channel"], user=event.get("user", None), - minimal=minimal, pretty=pretty) + self._event("kick", event["server"], line, event["channel"].name, + channel=event["channel"], user=event["user"], minimal=minimal, + formatting=formatting) @utils.hook("received.kick") def kick(self, event): self._on_kick(event, event["target_user"].nickname) @@ -255,15 +218,11 @@ class Module(ModuleManager.BaseModule): def _quit(self, event, user, reason): reason = "" if not reason else " (%s)" % reason - format = "%s quit%s" - minimal = format % (user.nickname, reason) + minimal = "{NICK} quit%s" % reason + line = "- %s" % minimal - normal_format = "- %s" % format - normal = normal_format % (user.nickname, reason) - pretty = normal_format % (self._color(user.nickname), reason) - - self._event("quit", event["server"], normal, None, user=user, - minimal=minimal, pretty=pretty) + self._event("quit", event["server"], line, None, user=user, + minimal=minimal) @utils.hook("received.quit") def on_quit(self, event): self._quit(event, event["user"], event["reason"])