From 7917da0c3e44d01d81b8350901bf334f752b7bd4 Mon Sep 17 00:00:00 2001 From: Agnibho Mondal Date: Fri, 15 Sep 2023 01:46:00 +0530 Subject: [PATCH] Error handling --- config.py | 15 +++++++++------ prescription.py | 5 ++++- setting.py | 9 ++++++--- window.py | 7 +++++-- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/config.py b/config.py index ad0db95..91eed9b 100644 --- a/config.py +++ b/config.py @@ -47,10 +47,13 @@ default = { "certificate": "" } -with open(config_file) as conf: - read = json.loads(conf.read()) - -config = default | read +try: + with open(config_file) as conf: + read = json.loads(conf.read()) + config = default | read +except Exception as e: + print(e) + config=default config["filename"]=args.filename config["data_directory"]=os.path.abspath(os.path.join(real_dir, os.path.expanduser(config["data_directory"]))) @@ -65,11 +68,11 @@ if(args.prescriber is None): if (not config["prescriber"].endswith(".json")): config["prescriber"]=config["prescriber"]+".json" else: if(not os.path.isabs(args.prescriber)): - args.prescriber=os.path.join(config["config_directory"], args.prescriber) + args.prescriber=os.path.join(config["prescriber_directory"], args.prescriber) if(os.path.isfile(args.prescriber)): config["prescriber"]=args.prescriber else: - config["prescriber"]=os.path.join(config["config_directory"], config["prescriber"]) + config["prescriber"]=os.path.join(config["prescriber_directory"], config["prescriber"]) print("File "+args.prescriber+" not found.") os.makedirs(config["data_directory"], exist_ok=True) diff --git a/prescription.py b/prescription.py index ab89569..f9c63a1 100644 --- a/prescription.py +++ b/prescription.py @@ -90,7 +90,10 @@ class Prescription: def write_to(self, file): with open(file, "w") as f: - del self.file + try: + del self.file + except Exception as e: + print(e) f.write(self.get_json()) self.file=file diff --git a/setting.py b/setting.py index 3722db2..3699aa5 100644 --- a/setting.py +++ b/setting.py @@ -77,9 +77,12 @@ class EditConfiguration(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - with open(config_file) as f: - self.config=json.loads(f.read()) - self.config=self.config|config + try: + with open(config_file) as f: + self.config=json.loads(f.read()) + except Exception as e: + print(e) + self.config=config self.setWindowTitle("MedScript") self.setGeometry(200, 200, 300, 200) diff --git a/window.py b/window.py index 9b758ed..1bd8ed8 100644 --- a/window.py +++ b/window.py @@ -483,8 +483,11 @@ class MainWindow(QMainWindow): toolbar.addSeparator() self.input_template=QComboBox(self) templates=os.listdir(config["template_directory"]) - templates.remove(os.path.basename(config["template"])) - templates.insert(0, os.path.basename(config["template"])) + try: + templates.remove(os.path.basename(config["template"])) + templates.insert(0, os.path.basename(config["template"])) + except Exception as e: + print(e) self.input_template.addItems(templates) toolbar.addWidget(self.input_template) spacer=QWidget(self) -- 2.39.2