added manual update
authorGeorgios Atheridis <atheridis@tutamail.com>
Wed, 23 Mar 2022 03:33:53 +0000 (05:33 +0200)
committerGeorgios Atheridis <atheridis@tutamail.com>
Wed, 23 Mar 2022 03:33:53 +0000 (05:33 +0200)
aptbot/__main__.py
aptbot/args.py
aptbot/bot.py

index 9227412fa8e62b20dd12c0864ca72b2f3e7966a7..972927a151b2a154edb2df0bf94077ba7b7500c2 100644 (file)
@@ -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()
 
 
index 78f5f5163388e69e1bcd7f40544aa5a74de5283c..2aba2809c4e0275449361093f44244da1b8c40cf 100644 (file)
@@ -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()
index 3c609a0c875c8ede25557a733f9d051dd185024b..dd5b615595d31552baf809a42e223973f4103b7e 100644 (file)
@@ -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)