From a6a893c247e3e01a2fab975a89900a9b33905251 Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Wed, 23 Mar 2022 05:33:53 +0200 Subject: [PATCH] added manual update --- aptbot/__main__.py | 68 ++++++++++++++++++++++++++++++++++++---------- aptbot/args.py | 7 +++++ aptbot/bot.py | 13 ++++++--- 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/aptbot/__main__.py b/aptbot/__main__.py index 9227412..972927a 100644 --- a/aptbot/__main__.py +++ b/aptbot/__main__.py @@ -6,8 +6,10 @@ import os import sys import importlib import importlib.util +import traceback from threading import Thread from dotenv import load_dotenv +from types import ModuleType if "XDG_CONFIG_HOME" in os.environ: @@ -26,29 +28,52 @@ PORT = 26538 LOCALHOST = "127.0.0.1" -def loop(bot: aptbot.bot.Bot): +def loop(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): + update_modules(modules) while True: messages = bot.receive_messages() for message in messages: if message.channel: - account_path = os.path.join(CONFIG_PATH, f"{message.channel}") - module_path = os.path.join( - account_path, f"message_interpreter.py") - spec = importlib.util.spec_from_file_location( - "message_interpreter", - module_path, + method = Thread( + target=modules[message.channel].main, + args=(bot, message, ) ) - if spec and spec.loader: - foo = importlib.util.module_from_spec(spec) - spec.loader.exec_module(foo) - method = Thread(target=foo.main, args=(bot, message, )) - method.daemon = True - method.start() - # foo.main(bot, message) + method.daemon = True + method.start() print(message) time.sleep(0.001) +def update_modules(modules: dict[str, ModuleType]): + modules.clear() + # modules = {} + channels = os.listdir(CONFIG_PATH) + for channel in channels: + account_path = os.path.join(CONFIG_PATH, f"{channel}") + module_path = os.path.join( + account_path, f"message_interpreter.py") + spec = importlib.util.spec_from_file_location( + "message_interpreter", + module_path, + ) + account_path = os.path.join(CONFIG_PATH, f"{channel}") + module_path = os.path.join( + account_path, f"message_interpreter.py") + spec = importlib.util.spec_from_file_location( + "message_interpreter", + module_path, + ) + if spec and spec.loader: + foo = importlib.util.module_from_spec(spec) + try: + spec.loader.exec_module(foo) + except Exception as e: + print(traceback.format_exc()) + print(f"Problem Loading Module: {e}") + else: + modules[channel] = foo + + def initialize(bot: aptbot.bot.Bot): channels = os.listdir(CONFIG_PATH) for channel in channels: @@ -64,7 +89,8 @@ def listener(): else: sys.exit(1) bot.connect() - message_loop = Thread(target=loop, args=(bot,)) + modules = {} + message_loop = Thread(target=loop, args=(bot, modules, )) message_loop.daemon = True message_loop.start() s = socket.socket() @@ -89,6 +115,9 @@ def listener(): bot.send_privmsg(channel, msg) elif "KILL" in command: sys.exit() + elif "UPDATE" in command: + update_modules(modules) + time.sleep(1) @@ -132,6 +161,13 @@ def disable(s: socket.socket): s.send(bytes(f"{command}==={channel}==={msg}", "utf-8")) +def update(s: socket.socket): + command = "UPDATE" + channel = "" + msg = "" + s.send(bytes(f"{command}==={channel}==={msg}", "utf-8")) + + def main(): argsv = aptbot.args.parse_arguments() if argsv.enable: @@ -146,6 +182,8 @@ def main(): send_msg(s, argsv.send_message) if argsv.disable: disable(s) + if argsv.update: + update(s) s.close() diff --git a/aptbot/args.py b/aptbot/args.py index 78f5f51..2aba280 100644 --- a/aptbot/args.py +++ b/aptbot/args.py @@ -35,4 +35,11 @@ def parse_arguments() -> argparse.Namespace: help=f"Disable the bot" ) + arg_parser.add_argument( + "--update", + default=False, + action="store_true", + help=f"Update the bot" + ) + return arg_parser.parse_args() diff --git a/aptbot/bot.py b/aptbot/bot.py index 3c609a0..dd5b615 100644 --- a/aptbot/bot.py +++ b/aptbot/bot.py @@ -23,7 +23,7 @@ class Commands(Enum): @dataclass class Message: - tags: dict + tags: dict[str, str] nick: str command: Optional[Commands] channel: str @@ -114,16 +114,21 @@ class Bot: messages = [] i = 0 while i < 5: + if i: + try: + self.connect() + self.join_channels(self.connected_channels) + except OSError as e: + print(f"Connection failed {e}") try: received_msgs = self.irc.recv(2048).decode() except ConnectionResetError as e: print(f"There was an error connecting with error {e}") print("Trying to connect again") - time.sleep(2**i) - self.connect() - self.join_channels(self.connected_channels) + time.sleep(2**i+1) i += 1 else: + print("broke") break else: sys.exit(1) -- 2.30.2