code cleanup
authorGeorgios Atheridis <atheridis@tutamail.com>
Fri, 25 Mar 2022 06:09:16 +0000 (08:09 +0200)
committerGeorgios Atheridis <atheridis@tutamail.com>
Fri, 25 Mar 2022 06:09:16 +0000 (08:09 +0200)
aptbot/__init__.py
aptbot/__main__.py
aptbot/args_logic.py [new file with mode: 0644]

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..cbed7dd5865c306da986ca46904f3bdb53fa6c88 100644 (file)
@@ -0,0 +1,14 @@
+import os
+
+if "XDG_CONFIG_HOME" in os.environ:
+    CONFIG_HOME = os.environ["XDG_CONFIG_HOME"]
+elif "APPDATA" in os.environ:
+    CONFIG_HOME = os.environ["APPDATA"]
+else:
+    CONFIG_HOME = os.path.join(os.environ["HOME"], ".config")
+
+CONFIG_PATH = os.path.join(CONFIG_HOME, f"aptbot")
+
+
+PORT = 26538
+LOCALHOST = "127.0.0.1"
index 1897fbcd6284b67d2810c379348aa444f1dfdf01..e891b421c29010463defee1dfd3cfcdccfb1c117 100644 (file)
@@ -1,6 +1,7 @@
 import socket
 import aptbot.args
 import time
+import aptbot.args_logic
 import aptbot.bot
 import os
 import sys
@@ -10,22 +11,10 @@ import traceback
 from threading import Thread
 from dotenv import load_dotenv
 from types import ModuleType
+from aptbot import *
 
 load_dotenv()
 
-if "XDG_CONFIG_HOME" in os.environ:
-    CONFIG_HOME = os.environ["XDG_CONFIG_HOME"]
-elif "APPDATA" in os.environ:
-    CONFIG_HOME = os.environ["APPDATA"]
-else:
-    CONFIG_HOME = os.path.join(os.environ["HOME"], ".config")
-
-CONFIG_PATH = os.path.join(CONFIG_HOME, f"aptbot")
-
-
-PORT = 26538
-LOCALHOST = "127.0.0.1"
-
 
 def loop(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]):
     update_modules(modules)
@@ -111,15 +100,15 @@ def listener():
         except IndexError:
             pass
         else:
-            if "JOIN" in command:
+            if aptbot.args_logic.Commands.JOIN.value in command:
                 bot.join_channel(channel)
-            elif "SEND" in command:
+            elif aptbot.args_logic.Commands.SEND.value in command:
                 bot.send_privmsg(channel, msg)
-            elif "KILL" in command:
+            elif aptbot.args_logic.Commands.KILL.value in command:
                 sys.exit()
-            elif "UPDATE" in command:
+            elif aptbot.args_logic.Commands.UPDATE.value in command:
                 update_modules(modules)
-            elif "PART" in command:
+            elif aptbot.args_logic.Commands.PART.value in command:
                 bot.leave_channel(channel)
 
         time.sleep(1)
@@ -134,70 +123,6 @@ def send(func,):
     return inner
 
 
-def add_account(s: socket.socket, acc: str):
-    account_path = os.path.join(CONFIG_PATH, f"{acc}")
-    hidden_account_path = os.path.join(CONFIG_PATH, f".{acc}")
-
-    try:
-        os.rename(hidden_account_path, account_path)
-    except FileNotFoundError:
-        pass
-    os.makedirs(account_path, exist_ok=True)
-
-    # print(os.listdir("."))
-    # shutil.copy("message_interpreter.py", account_path)
-    try:
-        f = open(os.path.join(account_path, "message_interpreter.py"), "r")
-    except FileNotFoundError:
-        f = open(os.path.join(account_path, "message_interpreter.py"), "a")
-        f.write("""from aptbot.bot import Bot, Message, Commands
-def main(bot, message: Message):
-    pass""")
-        f.close()
-    else:
-        f.close()
-
-    command = "JOIN"
-    channel = acc
-    msg = ""
-    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))
-
-
-def send_msg(s: socket.socket, msg: str):
-    command = "SEND"
-    channel = msg.split(' ')[0]
-    msg = msg[len(channel) + 1:]
-    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))
-
-
-def disable(s: socket.socket):
-    command = "KILL"
-    channel = ""
-    msg = ""
-    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))
-
-
-def disable_account(s: socket.socket, acc: str):
-    account_path = os.path.join(CONFIG_PATH, f"{acc}")
-    hidden_account_path = os.path.join(CONFIG_PATH, f".{acc}")
-    try:
-        os.rename(account_path, hidden_account_path)
-    except FileNotFoundError:
-        print(f"Account {acc} is already disabled.")
-
-    command = "PART"
-    channel = ""
-    msg = ""
-    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()
     os.makedirs(CONFIG_PATH, exist_ok=True)
@@ -211,15 +136,15 @@ def main():
         pass
 
     if argsv.add_account:
-        add_account(s, argsv.add_account)
+        aptbot.args_logic.add_account(s, argsv.add_account)
     if argsv.disable_account:
-        disable_account(s, argsv.disable_account)
+        aptbot.args_logic.disable_account(s, argsv.disable_account)
     if argsv.send_message:
-        send_msg(s, argsv.send_message)
+        aptbot.args_logic.send_msg(s, argsv.send_message)
     if argsv.disable:
-        disable(s)
+        aptbot.args_logic.disable(s)
     if argsv.update:
-        update(s)
+        aptbot.args_logic.update(s)
     s.close()
 
 
diff --git a/aptbot/args_logic.py b/aptbot/args_logic.py
new file mode 100644 (file)
index 0000000..c6b8526
--- /dev/null
@@ -0,0 +1,76 @@
+import socket
+import os
+from enum import Enum
+from aptbot import CONFIG_PATH
+
+
+class Commands(Enum):
+    JOIN = "JOIN"
+    PART = "PART"
+    SEND = "SEND"
+    KILL = "KILL"
+    UPDATE = "UPDATE"
+
+
+def add_account(s: socket.socket, acc: str):
+    account_path = os.path.join(CONFIG_PATH, f"{acc}")
+    hidden_account_path = os.path.join(CONFIG_PATH, f".{acc}")
+
+    try:
+        os.rename(hidden_account_path, account_path)
+    except FileNotFoundError:
+        pass
+    os.makedirs(account_path, exist_ok=True)
+
+    # print(os.listdir("."))
+    # shutil.copy("message_interpreter.py", account_path)
+    try:
+        f = open(os.path.join(account_path, "message_interpreter.py"), "r")
+    except FileNotFoundError:
+        f = open(os.path.join(account_path, "message_interpreter.py"), "a")
+        f.write("""from aptbot.bot import Bot, Message, Commands
+def main(bot, message: Message):
+    pass""")
+        f.close()
+    else:
+        f.close()
+
+    command = Commands.JOIN.value
+    channel = acc
+    msg = ""
+    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))
+
+
+def send_msg(s: socket.socket, msg: str):
+    command = Commands.SEND.value
+    channel = msg.split(' ')[0]
+    msg = msg[len(channel) + 1:]
+    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))
+
+
+def disable(s: socket.socket):
+    command = Commands.KILL.value
+    channel = ""
+    msg = ""
+    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))
+
+
+def disable_account(s: socket.socket, acc: str):
+    account_path = os.path.join(CONFIG_PATH, f"{acc}")
+    hidden_account_path = os.path.join(CONFIG_PATH, f".{acc}")
+    try:
+        os.rename(account_path, hidden_account_path)
+    except FileNotFoundError:
+        print(f"Account {acc} is already disabled.")
+
+    command = Commands.PART.value
+    channel = ""
+    msg = ""
+    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))
+
+
+def update(s: socket.socket):
+    command = Commands.UPDATE.value
+    channel = ""
+    msg = ""
+    s.send(bytes(f"{command}==={channel}==={msg}", "utf-8"))