From 7bca26e07c2e277118ac97b2d86c227750fbb7d1 Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Mon, 8 May 2023 09:59:07 +0300 Subject: [PATCH] Edited according to PEP8 and added f-strings. --- module-testing.py | 51 ++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/module-testing.py b/module-testing.py index e31018f..28fb680 100644 --- a/module-testing.py +++ b/module-testing.py @@ -12,16 +12,18 @@ from telegram import Message, Chat # global variables STOP_REQUESTED = False + # some functions that increase readability of the code def readfile(filename): try: - return codecs.open(filename, encoding = "utf-8").read() + return codecs.open(filename, encoding="utf-8").read() except FileNotFoundError: return False except Exception as e: - print( "[ERROR] Unexpected error occured in readfile() ({0})".format(e) ) + print(f"[ERROR] Unexpected error occurred in readfile() ({e})") return False + # module object classes class ModuleV1: def __init__(self, path, code, enabled, alias, predefine): @@ -43,7 +45,8 @@ class ModuleV1: try: exec(self.predefine) except Exception as e: - print("[ERROR] module v1: module \"{}\" ({}) raised exception \"{}\" during predefine stage, disabling it...".format(self.path, self.alias, e)) + print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"" + f" during predefine stage, disabling it...") # running the module def process(self, msg): @@ -54,7 +57,7 @@ class ModuleV1: exec(self.code) return self.RESPONCE except Exception as e: - print("[ERROR] module v1: module \"{}\" ({}) raised exception \"{}\"".format(self.path, self.alias, e)) + print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"") return "" @@ -90,10 +93,10 @@ class ModuleControlUnit: try: meta_raw = readfile("modules/{}/meta.json".format(folder)) if not meta_raw: - print("[WARN] module_loader: no meta.json found in module folder \"{}\"".format(folder)) + print(f"[WARN] module_loader: no meta.json found in module folder \"{folder}\"") continue - meta = json.loads( meta_raw ) + meta = json.loads(meta_raw) if "version" in meta: if meta["version"] == 1: if "index_file" in meta: @@ -101,11 +104,11 @@ class ModuleControlUnit: else: index_file = "index.py" - code = readfile( "modules/{}/{}".format(folder, index_file) ) - if not code: # False both when readfile() returns False and when the code string is empty - print("[WARN] reload_modules: module {} does not have any code, skipping...".format(folder)) + code = readfile("modules/{}/{}".format(folder, index_file)) + if not code: # False both when readfile() returns False and when the code string is empty + print(f"[WARN] reload_modules: module {folder} does not have any code, skipping...") continue - + if "start_on_boot" in meta: enabled = meta["start_on_boot"] else: @@ -121,9 +124,10 @@ class ModuleControlUnit: else: predefine = False - self.modules.append( ModuleV1( "modules/{}/".format(folder), code, enabled, alias, predefine ) ) + self.modules.append(ModuleV1("modules/{}/".format(folder), code, enabled, alias, predefine)) - print("[INFO] reload_modules: successfully loaded {} as {} (start_on_boot: {})".format(folder, alias, enabled)) + print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} " + f"(start_on_boot: {enabled})") elif meta["version"] == 2: if "index_file" in meta: @@ -143,17 +147,19 @@ class ModuleControlUnit: self.modules.append(ModuleV2(f"modules/{folder}/", index_file, enabled, alias)) - print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} (start_on_boot: {enabled})") + print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} " + f"(start_on_boot: {enabled})") else: - print(f"[WARN] reload_modules: module {folder} requires unsupported version ({meta['version']} > 2), skipping...") + print(f"[WARN] reload_modules: module {folder} requires unsupported version " + f"({meta['version']} > 2), skipping...") except Exception as e: - print("[ERROR] module_loader: error while loading module \"{}\" ({})".format(folder, e)) + print(f"[ERROR] module_loader: error while loading module \"{folder}\" ({e})") # message queue object to go back to synchronous message processing -#class MessageQueue: +# class MessageQueue: # def __init__(self): # print("[INFO] Initializing the message queue...") # self.queue = [] @@ -165,13 +171,13 @@ def queue_processor(): try: if len(message_queue) > 0: msg = message_queue[0] - print("[DEBUG] queue_processor: {}".format(msg)) # debug + print("[DEBUG] queue_processor: {}".format(msg)) # debug # check for control commands if msg["chat"]["id"] == 575246355: if msg["text"][0] == "$": command = msg["text"][1:].split(" ") - + if len(command) >= 2 and command[0] == "module": if command[1] == "reload": print("[INFO] Module reloading triggered by a command") @@ -183,7 +189,7 @@ def queue_processor(): del mcu.modules[:] mcu.reload_modules() - + del message_queue[0] continue @@ -204,7 +210,7 @@ def queue_processor(): time.sleep(1) except Exception as e: - print("[ERROR] queue_processor: current message queue: {}".format(message_queue)) + print(f"[ERROR] queue_processor: current message queue: {message_queue}") print("[ERROR] queue_processor: error while processing message, trying to delete it...") try: del message_queue[0] @@ -224,10 +230,9 @@ message_queue = [] mcu = ModuleControlUnit() -processor_thread = threading.Thread( target = queue_processor, args = [] ) +processor_thread = threading.Thread(target=queue_processor, args=[]) processor_thread.start() - print("Enter testing messages one by one, end with an empty line") while True: @@ -235,4 +240,4 @@ while True: if len(new_msg) == 0: break - message_queue.append(Message(9, round(time.time()), Chat(575246355, 'supergroup'), text = new_msg)) + message_queue.append(Message(9, round(time.time()), Chat(575246355, 'supergroup'), text=new_msg))