From e5a5fa5c4b40918247be8486d27a1676b6d2b044 Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 28 Aug 2018 12:23:57 +0100 Subject: [PATCH] modules/logging.py -> IRCLogging.py; IRCLog.py -> IRCBuffer.py; change logging to be an object on the server object instead of an event call --- Database.py | 10 +++---- EventManager.py | 24 ++++++++++++++-- IRCBot.py | 3 +- IRCChannel.py | 7 +++-- IRCLineHandler.py | 4 +-- IRCLog.py | 43 ----------------------------- modules/logging.py => IRCLogging.py | 32 ++++++++------------- IRCServer.py | 4 +-- IRCUser.py | 8 ++++-- modules/commands.py | 8 +++--- modules/define.py | 2 +- modules/google.py | 2 +- modules/print_activity.py | 4 +-- modules/soundcloud.py | 2 +- modules/title.py | 2 +- modules/translate.py | 2 +- modules/twitter.py | 2 +- modules/youtube.py | 2 +- 18 files changed, 66 insertions(+), 95 deletions(-) delete mode 100644 IRCLog.py rename modules/logging.py => IRCLogging.py (63%) diff --git a/Database.py b/Database.py index 3c6787c3..9f0f7f8b 100644 --- a/Database.py +++ b/Database.py @@ -261,9 +261,8 @@ class Database(object): def _execute_fetch(self, query, fetch_func, params=[]): printable_query = " ".join(query.split()) - self.bot.events.on("log.debug").call( - message="executing query: \"%s\" (params: %s)", - params=[printable_query, params]) + self.bot.log.debug("executing query: \"%s\" (params: %s)", + [printable_query, params]) start = time.monotonic() cursor = self.cursor() @@ -272,9 +271,8 @@ class Database(object): end = time.monotonic() total_milliseconds = (end - start) * 1000 - self.bot.events.on("log.debug").call( - message="executed in %fms", - params=[total_milliseconds]) + self.bot.log.debug("executed in %fms", [total_milliseconds]) + return value def execute_fetchall(self, query, params=[]): return self._execute_fetch(query, diff --git a/EventManager.py b/EventManager.py index e46d682b..e17800ae 100644 --- a/EventManager.py +++ b/EventManager.py @@ -1,4 +1,4 @@ -import traceback +import time, traceback PRIORITY_URGENT = 0 PRIORITY_HIGH = 1 @@ -42,9 +42,10 @@ class MultipleEventHook(object): event_hook.call(max, **kwargs) class EventHook(object): - def __init__(self, bot, name=None): + def __init__(self, bot, name=None, parent=None): self.bot = bot self.name = name + self.parent = parent self._children = {} self._hooks = [] self._hook_notify = None @@ -52,6 +53,14 @@ class EventHook(object): self._call_notify = None self._stored_events = [] + def _get_path(self): + path = [self.name] + parent = self.parent + while not parent == None and not parent.name == None: + path.append(parent.name) + parent = parent.parent + return ".".join(path[::-1]) + def hook(self, function, priority=PRIORITY_LOW, replay=False, **kwargs): callback = EventCallback(function, self.bot, priority, **kwargs) if self._hook_notify: @@ -87,6 +96,10 @@ class EventHook(object): results = self.call(max=max, **kwargs) return default if not len(results) else results[0] def call(self, max=None, **kwargs): + self.bot.log.debug("calling event: \"%s\" (params: %s)", + [self._get_path(), kwargs]) + start = time.monotonic() + event = Event(self.bot, self.name, **kwargs) if self._call_notify: self._call_notify(self, event) @@ -109,13 +122,18 @@ class EventHook(object): # message="Failed to call event callback", # data=traceback.format_exc()) called += 1 + + end = time.monotonic() + total_milliseconds = (end - start) * 1000 + self.bot.log.debug("event called in %fms", [total_milliseconds]) + return returns def get_child(self, child_name): child_name_lower = child_name.lower() if not child_name_lower in self._children: self._children[child_name_lower] = EventHook(self.bot, - child_name) + child_name, self) if self._child_notify: self._child_notify(self, self._children[ child_name_lower]) diff --git a/IRCBot.py b/IRCBot.py index c178a4f3..7d15cd4d 100644 --- a/IRCBot.py +++ b/IRCBot.py @@ -1,5 +1,5 @@ import os, select, sys, threading, time, traceback -import EventManager, IRCServer, ModuleManager, Timer +import EventManager, IRCLogging, IRCServer, ModuleManager, Timer class Bot(object): def __init__(self): @@ -13,6 +13,7 @@ class Bot(object): self.poll = select.epoll() self.modules = ModuleManager.ModuleManager(self) self.events = EventManager.EventHook(self) + self.log = IRCLogging.Log(self) self.timers = [] self.events.on("timer").on("reconnect").hook(self.reconnect) self.events.on("boot").on("done").hook(self.setup_timers) diff --git a/IRCChannel.py b/IRCChannel.py index a572be10..2cc46ca4 100644 --- a/IRCChannel.py +++ b/IRCChannel.py @@ -1,5 +1,5 @@ import uuid -import IRCLog +import IRCBuffer class Channel(object): def __init__(self, name, id, server, bot): @@ -15,7 +15,10 @@ class Channel(object): self.users = set([]) self.modes = {} self.created_timestamp = None - self.log = IRCLog.Log(bot) + self.buffer = IRCBuffer.Buffer(bot) + + def __repr__(self): + return "IRCChannel.Channel(%s|%s)" % (self.server.name, self.name) def set_topic(self, topic): self.topic = topic diff --git a/IRCLineHandler.py b/IRCLineHandler.py index 6267fc06..38bf49a0 100644 --- a/IRCLineHandler.py +++ b/IRCLineHandler.py @@ -302,12 +302,12 @@ def handle_PRIVMSG(data): bot.events.on("received").on("message").on("channel").call( user=user, message=message, message_split=message_split, channel=channel, action=action, server=data.server) - channel.log.add_line(user.nickname, message, action) + channel.buffer.add_line(user.nickname, message, action) elif server.is_own_nickname(target): bot.events.on("received").on("message").on("private").call( user=user, message=message, message_split=message_split, action=action, server=data.server) - user.log.add_line(user.nickname, message, action) + user.buffer.add_line(user.nickname, message, action) @handler(description="we've received a notice") def handle_NOTICE(data): diff --git a/IRCLog.py b/IRCLog.py deleted file mode 100644 index 834939e2..00000000 --- a/IRCLog.py +++ /dev/null @@ -1,43 +0,0 @@ -import re - -class Line(object): - def __init__(self, sender, message, action, from_self): - self.sender = sender - self.message = message - self.action = action - self.from_self = from_self - -class Log(object): - def __init__(self, bot): - self.lines = [] - self.max_lines = 64 - self._skip_next = False - def add_line(self, sender, message, action, from_self=False): - if not self._skip_next: - line = Line(sender, message, action, from_self) - self.lines.insert(0, line) - if len(self.lines) > self.max_lines: - self.lines.pop() - self._skip_next = False - def get(self, index=0, **kwargs): - from_self = kwargs.get("from_self", True) - for line in self.lines: - if line.from_self and not from_self: - continue - return line - def find(self, pattern, **kwargs): - from_self = kwargs.get("from_self", True) - for_user = kwargs.get("for_user", "") - for_user = for_user.lower() if for_user else None - not_pattern = kwargs.get("not_pattern", None) - for line in self.lines: - if line.from_self and not from_self: - continue - elif re.search(pattern, line.message): - if not_pattern and re.search(not_pattern, line.message): - continue - if for_user and not line.sender.lower() == for_user: - continue - return line - def skip_next(self): - self._skip_next = True diff --git a/modules/logging.py b/IRCLogging.py similarity index 63% rename from modules/logging.py rename to IRCLogging.py index 46f8bb29..7b01acee 100644 --- a/modules/logging.py +++ b/IRCLogging.py @@ -13,7 +13,7 @@ class BitBotFormatter(logging.Formatter): s = "%s.%03d" % (t, record.msecs) return s -class Module(object): +class Log(object): def __init__(self, bot): self.logger = logging.getLogger(__name__) self.logger.setLevel(logging.DEBUG) @@ -33,23 +33,15 @@ class Module(object): file_handler.setFormatter(formatter) self.logger.addHandler(file_handler) - bot.events.on("log.debug").hook(self.debug) - bot.events.on("log.info").hook(self.info) - bot.events.on("log.warn").hook(self.warn) - bot.events.on("log.error").hook(self.error) - bot.events.on("log.critical").hook(self.critical) - - def debug(self, event): - self._log(event, logging.DEBUG) - def info(self, event): - self._log(event, logging.INFO) - def warn(self, event): - self._log(event, logging.WARN) - def error(self, event): - self._log(event, logging.ERROR) - def critical(self, event): - self._log(event, logging.CRITICAL) - def _log(self, event, level): - message = event["message"] - params = event.get("params", []) + def debug(self, message, params): + self._log(message, params, logging.DEBUG) + def info(self, message, params): + self._log(message, params, logging.INFO) + def warn(self, message, params): + self._log(message, params, logging.WARN) + def error(self, message, params): + self._log(message, params, logging.ERROR) + def critical(self, message, params): + self._log(message, params, logging.CRITICAL) + def _log(self, message, params, level): self.logger.log(level, message, *params) diff --git a/IRCServer.py b/IRCServer.py index c53cc5fd..ee280010 100644 --- a/IRCServer.py +++ b/IRCServer.py @@ -267,13 +267,13 @@ class Server(object): full_message_split = full_message.split() if self.has_channel(target): channel = self.get_channel(target) - channel.log.add_line(None, message, action, True) + channel.buffer.add_line(None, message, action, True) self.bot.events.on("self").on("message").on("channel").call( message=full_message, message_split=full_message_split, channel=channel, action=action, server=self) else: user = self.get_user(target) - user.log.add_line(None, message, action, True) + user.buffer.add_line(None, message, action, True) self.bot.events.on("self").on("message").on("private").call( message=full_message, message_split=full_message_split, user=user, action=action, server=self) diff --git a/IRCUser.py b/IRCUser.py index b9ede9e9..c99372db 100644 --- a/IRCUser.py +++ b/IRCUser.py @@ -1,5 +1,5 @@ import uuid -import IRCLog +import IRCBuffer class User(object): def __init__(self, nickname, id, server, bot): @@ -11,7 +11,11 @@ class User(object): self.server = server self.bot = bot self.channels = set([]) - self.log = IRCLog.Log(bot) + self.buffer = IRCBuffer.Buffer(bot) + + def __repr__(self): + return "IRCUser.User(%s|%s)" % (self.server.name, self.name) + def set_nickname(self, nickname): self.nickname = nickname self.nickname_lower = nickname.lower() diff --git a/modules/commands.py b/modules/commands.py index 610a0bfd..c0158287 100644 --- a/modules/commands.py +++ b/modules/commands.py @@ -102,7 +102,7 @@ class Module(object): if is_channel and hook.kwargs.get("private_only"): return - log = target.log + buffer = target.buffer module_name = hook.function.__self__._name stdout, stderr = StdOut(module_name, target), StdErr(module_name, @@ -114,7 +114,7 @@ class Module(object): for returned in returns: if returned: stderr.write(returned).send() - log.skip_next() + buffer.skip_next() return args_split = list(filter(None, event["message_split"][args_index:])) min_args = hook.kwargs.get("min_args") @@ -130,7 +130,7 @@ class Module(object): server = event["server"] user = event["user"] self.bot.events.on("received").on("command").on(command).call( - 1, user=user, server=server, target=target, log=log, + 1, user=user, server=server, target=target, buffer=buffer, args=args, args_split=args_split, stdout=stdout, stderr=stderr, command=command.lower(), is_channel=is_channel) if not hook.kwargs.get("skip_out", False): @@ -138,7 +138,7 @@ class Module(object): stderr.send() target.last_stdout = stdout target.last_stderr = stderr - log.skip_next() + buffer.skip_next() def channel_message(self, event): diff --git a/modules/define.py b/modules/define.py index b452e0fa..c5e62b6a 100644 --- a/modules/define.py +++ b/modules/define.py @@ -15,7 +15,7 @@ class Module(object): if event["args"]: word = event["args"] else: - word = event["log"].get(from_self=False) + word = event["buffer"].get(from_self=False) page = Utils.get_url(URL_WORDNIK % event["args"], get_params={ "useCanonical": "true", "limit": 1, "sourceDictionaries": "wiktionary", "api_key": self.bot.config[ diff --git a/modules/google.py b/modules/google.py index 237a1c58..b961186e 100644 --- a/modules/google.py +++ b/modules/google.py @@ -13,7 +13,7 @@ class Module(object): usage="[search term]") def google(self, event): - phrase = event["args"] or event["log"].get() + phrase = event["args"] or event["buffer"].get() if phrase: page = Utils.get_url(URL_GOOGLESEARCH, get_params={ "q": phrase, "key": self.bot.config[ diff --git a/modules/print_activity.py b/modules/print_activity.py index 8344255e..21ce9796 100644 --- a/modules/print_activity.py +++ b/modules/print_activity.py @@ -36,9 +36,7 @@ class Module(object): target = str(event["server"]) if not channel == None: target += channel - self.bot.events.on("log.info").call( - message="%s | %s", - params=[target, line]) + self.bot.log.info("%s | %s", [target, line]) def _on_message(self, event, nickname): if not self.bot.args.verbose: diff --git a/modules/soundcloud.py b/modules/soundcloud.py index 97125255..2bdd4276 100644 --- a/modules/soundcloud.py +++ b/modules/soundcloud.py @@ -25,7 +25,7 @@ class Module(object): else: query = event["args"] else: - last_soundcloud = event["log"].find(REGEX_SOUNDCLOUD) + last_soundcloud = event["buffer"].find(REGEX_SOUNDCLOUD) if last_soundcloud: url = re.match(REGEX_SOUNDCLOUD, last_soundcloud.message).string diff --git a/modules/title.py b/modules/title.py index 6ccc7044..df19a6ec 100644 --- a/modules/title.py +++ b/modules/title.py @@ -14,7 +14,7 @@ class Module(object): if len(event["args"]) > 0: url = event["args_split"][0] else: - url = event["log"].find(REGEX_URL) + url = event["buffer"].find(REGEX_URL) if url: url = re.search(REGEX_URL, url.message).group(0) if not url: diff --git a/modules/translate.py b/modules/translate.py index c1345500..a4b72785 100644 --- a/modules/translate.py +++ b/modules/translate.py @@ -14,7 +14,7 @@ class Module(object): def translate(self, event): phrase = event["args"] if not phrase: - phrase = event["log"].get() + phrase = event["buffer"].get() if phrase: phrase = phrase.message if not phrase: diff --git a/modules/twitter.py b/modules/twitter.py index a1308df5..d5b451db 100644 --- a/modules/twitter.py +++ b/modules/twitter.py @@ -32,7 +32,7 @@ class Module(object): if event["args"]: target = event["args"] else: - target = event["log"].find(REGEX_TWITTERURL) + target = event["buffer"].find(REGEX_TWITTERURL) if target: target = target.message if target: diff --git a/modules/youtube.py b/modules/youtube.py index 96f25689..51bcac38 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -72,7 +72,7 @@ class Module(object): if event["args"]: search = event["args"] else: - last_youtube = event["log"].find(REGEX_YOUTUBE) + last_youtube = event["buffer"].find(REGEX_YOUTUBE) if last_youtube: video_id = re.search(REGEX_YOUTUBE, last_youtube.message).group(1) if search or video_id: