From 9dc14f69b7564cd3f01a1167b115d0c55e429a33 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 30 Jan 2022 04:06:07 -0500 Subject: [PATCH] clean up main.py and facilitate the calling of member destructors so cleanup actually works! i hate python's gc this is actual trash when i delete an object the destructor isn't called this is insane if you're not writing "pythonic" codethe secret police comes in the night and takes your right to function away --- client.py | 10 ++++++++-- main.py | 9 +++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/client.py b/client.py index 4164a65..887da08 100644 --- a/client.py +++ b/client.py @@ -5,12 +5,14 @@ from grim_logger import glog, glog_level from plugin_manager import plugin_manager class client(): - def __init__(self, config_path = "config.json"): + def __init__(self, config_path = None): self.logger = glog(level = glog_level.TRACE) self.ws = None self.last_sequence = None self.session_id = None self.user = None + if not config_path: # this really really really simplifies things even if it is technically messier than a default param itself + config_path = "config.json" self.logger.write(f"loading config from {config_path}") with open(config_path, "r") as conf_fd: self.config = json.loads(conf_fd.read()) @@ -57,4 +59,8 @@ class client(): # fstring wasn't working for the auth headers = {"Authorization": "Bot {0}".format(self.config["token"]), "User-Agent": "mbdf (cynic.moe, v1)", "Content-Type": "application/json"} res = requests.post(endpoint, headers = headers, data = json.dumps({"content": message})) - self.logger.write(headers) \ No newline at end of file + self.logger.write(headers) + def __enter__(self): + return self + def __exit__(self, type, value, traceback): + pass # this should destruct all members probably maybe \ No newline at end of file diff --git a/main.py b/main.py index 1d27b3b..587d482 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,6 @@ from client import client from sys import argv, exit -olive = None -if len(argv) < 2: - olive = client() # is this clean? who knows! -else: - olive = client(config_path = argv[1]) -olive.run() \ No newline at end of file +path = None +with client(config_path = None if (len(argv) < 2) else argv[1]) as c: + c.run()