Edited according to PEP8 and added f-strings.

This commit is contained in:
Rhinemann 2023-05-08 09:59:07 +03:00
parent 1f9212c033
commit 7bca26e07c
1 changed files with 28 additions and 23 deletions

View File

@ -12,6 +12,7 @@ from telegram import Message, Chat
# global variables
STOP_REQUESTED = False
# some functions that increase readability of the code
def readfile(filename):
try:
@ -19,9 +20,10 @@ def readfile(filename):
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,7 +93,7 @@ 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)
@ -103,7 +106,7 @@ class ModuleControlUnit:
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))
print(f"[WARN] reload_modules: module {folder} does not have any code, skipping...")
continue
if "start_on_boot" in meta:
@ -123,7 +126,8 @@ class ModuleControlUnit:
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,13 +147,15 @@ 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
@ -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]
@ -227,7 +233,6 @@ mcu = ModuleControlUnit()
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: