]> Softwares of Agnibho - medscript.git/commitdiff
Log exception
authorAgnibho Mondal <mondal@agnibho.com>
Thu, 9 Nov 2023 16:03:02 +0000 (21:33 +0530)
committerAgnibho Mondal <mondal@agnibho.com>
Thu, 9 Nov 2023 16:03:02 +0000 (21:33 +0530)
14 files changed:
config.py
customform.py
editpreset.py
filehandler.py
index.py
installer.py
plugin.py
prescription.py
renderbox.py
renderer.py
setting.py
signature.py
tabular.py
window.py

index d3bfc79a645663f751811c0f8f3daaee55963c37..6a5c616ef3af71f33b6841e0270394b98f291dc3 100644 (file)
--- a/config.py
+++ b/config.py
@@ -53,8 +53,11 @@ try:
     with open(config_file) as conf:
         read = json.loads(conf.read())
     config = default | read
+except FileNotFoundError as e:
+    logging.critical(e)
+    config=default
 except Exception as e:
-    logging.warning(e)
+    logging.exception(e)
     config=default
 
 config["filename"]=args.filename
index 547ecfc00edcbe76ed32824056a3765a1c6d12d0..4d82eec311ef80e8fb73dd5d72fad029891ff53c 100644 (file)
@@ -23,7 +23,7 @@ class CustomForm(QWidget):
                 try:
                     self.forms.append(json.loads(f.read()))
                 except Exception as e:
-                    logging.warning(e)
+                    logging.exception(e)
         for i in self.forms:
             try:
                 for j in i["form"]:
@@ -40,8 +40,10 @@ class CustomForm(QWidget):
                         self.inputs.append([j["description"], QCheckBox()])
                     else:
                         self.inputs.append([j["description"], QLineEdit()])
+            except KeyError as e:
+                logging.warning(e)
             except Exception as e:
-                raise(e)
+                logging.exception(e)
 
     def getData(self):
         try:
@@ -55,7 +57,7 @@ class CustomForm(QWidget):
                 elif(isinstance(self.inputs[index][1], QDateTimeEdit)):
                     self.custom[index][list(item)[0]]=self.inputs[index][1].text()
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
         return(self.custom)
 
     def setData(self, custom=False):
@@ -74,7 +76,7 @@ class CustomForm(QWidget):
                     d=QDateTime.fromString(pdate.strftime("%Y-%m-%d %H:%M:%S"), "yyyy-MM-dd hh:mm:ss")
                     self.inputs[index][1].setDateTime(d)
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
 
     def __init__(self, *args, **kwargs):
index 7b05304d7c38d161cbe888919df0bcee5d497793..ce9d1455afe87acb68182b4f5a05d6a4da95dd46 100644 (file)
@@ -61,7 +61,7 @@ class EditPreset(QMainWindow):
             self.load(file)
             QMessageBox.information(self,"File saved", "Changes saved. Please restart the program.")
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def cmd_row(self):
         tablerow=[]
@@ -107,7 +107,7 @@ class EditPreset(QMainWindow):
             self.table.resizeRowsToContents()
             textedit=QTextEdit()
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def confirm(self):
         return QMessageBox.StandardButton.Yes==QMessageBox.question(self,"Confirm action", "Unsaved changes may be lost. Continue?")
index 514e0797c604a76f804efe886fc0b3c8c80c30b4..e59f5b3b07e92e047202d60bb0ababd7d9ff09c6 100644 (file)
@@ -91,14 +91,14 @@ class FileHandler():
         try:
             os.unlink(os.path.join(self.directory.name, "attachment", os.path.basename(item)))
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def delete_sign(self):
         try:
             os.unlink(os.path.join(self.directory.name, "certificate.pem"))
             os.unlink(os.path.join(self.directory.name, "signature"))
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def has_template(self):
         return(os.path.exists(os.path.join(self.directory.name, "template", "index.html")))
index 0b64400b6fb5377c1eb01329d8f5089c1b3e6b19..a08ff820f9a430f037b7f08cc442b3b91a5b764f 100644 (file)
--- a/index.py
+++ b/index.py
@@ -90,35 +90,46 @@ class Index(QMainWindow):
 
     def cmd_view(self):
         try:
-            with ZipFile(self.getSelectedFile()) as zf:
-                with zf.open("prescription.json") as pf:
-                    prescription=json.loads(pf.read())
-            self.unrenderbox.show(prescription).exec()
+            file=self.getSelectedFile()
+            if(file):
+                with ZipFile(file) as zf:
+                    with zf.open("prescription.json") as pf:
+                        prescription=json.loads(pf.read())
+                self.unrenderbox.show(prescription).exec()
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def cmd_open(self):
         try:
-            self.signal_open.emit(self.getSelectedFile())
-            self.hide()
+            file=self.getSelectedFile()
+            if(file):
+                self.signal_open.emit()
+                self.hide()
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def cmd_copy(self):
         try:
-            with ZipFile(self.getSelectedFile()) as zf:
-                with zf.open("prescription.json") as pf:
-                    pres=json.loads(pf.read())
-            self.signal_copy.emit(pres)
-            self.hide()
+            file=self.getSelectedFile()
+            if(file):
+                with ZipFile(file) as zf:
+                    with zf.open("prescription.json") as pf:
+                        pres=json.loads(pf.read())
+                self.signal_copy.emit(pres)
+                self.hide()
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
 
     def getSelectedFile(self):
-        selection=self.table.selectedIndexes()
-        file=selection[-1].data()
-        return file
+        try:
+            selection=self.table.selectedIndexes()
+            file=selection[-1].data()
+            return file
+        except IndexError as e:
+            logging.warning(e)
+        except Exception as e:
+            logging.exception(e)
 
     def build(self):
         files=glob(os.path.join(config["document_directory"], "**", "*.mpaz"), recursive=True)
@@ -130,10 +141,12 @@ class Index(QMainWindow):
                         with zf.open("prescription.json") as pf:
                             pres=json.loads(pf.read())
                             self.index.append([pres["pid"], pres["id"], pres["name"], pres["dob"], pres["age"], pres["sex"], pres["date"], pres["diagnosis"], file])
-                    except Exception as e:
+                    except KeyError as e:
                         logging.warning(e)
+                    except Exception as e:
+                        logging.exception(e)
             except Exception as e:
-                logging.warning(e)
+                logging.exception(e)
 
     def load(self):
         model=QStandardItemModel()
index 6134115ca84fa4d771aeb11ed9e00f22ae25459d..4a53f3f599d9a73c5cf8053a48b131d8c5b7840d 100644 (file)
@@ -64,7 +64,7 @@ class Installer(QMainWindow):
                             QMessageBox.information(self, "File exists", "PLUGIN <strong>"+name+"</strong> is already installed.")
             QMessageBox.information(self, "Restart", "Please restart MedScript for the changes to take effect.")
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def cmd_uninstall(self):
         txt=self.installed.currentItem().text().split("\t")
@@ -168,4 +168,4 @@ class Installer(QMainWindow):
             for i in self.plugin["name"]:
                 self.installed.addItem("[plugin]\t"+i)
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
index a45b801c7c95f1bfb1c79ea5d84da4c2eb794615..3be5067bdbfd5d39cf01ca3a9394e5d973504398 100644 (file)
--- a/plugin.py
+++ b/plugin.py
@@ -56,7 +56,7 @@ class Plugin():
                     if(message):
                         self.showMessage(message)
             except Exception as e:
-                logging.warning(e)
+                logging.exception(e)
 
     def open(self, prescription):
         for i in self.plugins:
@@ -68,7 +68,7 @@ class Plugin():
                     if(message):
                         self.showMessage(message)
             except Exception as e:
-                logging.warning(e)
+                logging.exception(e)
 
     def save(self, prescription):
         for i in self.plugins:
@@ -80,7 +80,7 @@ class Plugin():
                     if(message):
                         self.showMessage(message)
             except Exception as e:
-                logging.warning(e)
+                logging.exception(e)
 
     def refresh(self, prescription):
         for i in self.plugins:
@@ -92,7 +92,7 @@ class Plugin():
                     if(message):
                         self.showMessage(message)
             except Exception as e:
-                logging.warning(e)
+                logging.exception(e)
 
     def run(self, module, prescription):
         try:
@@ -115,7 +115,7 @@ class Plugin():
                     if(message):
                         self.showMessage(message)
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def input(self):
         try:
@@ -125,7 +125,7 @@ class Plugin():
             else:
                 return ""
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def showMessage(self, message, index=None):
         QMessageBox.information(None, "Information", message)
@@ -148,6 +148,9 @@ class Worker(QThread):
         self.index=index
 
     def run(self):
-        prescription_copy=copy.deepcopy(self.prescription)
-        message=self.function(prescription_copy)
-        self.pluginComplete.emit(message, self.index)
+        try:
+            prescription_copy=copy.deepcopy(self.prescription)
+            message=self.function(prescription_copy)
+            self.pluginComplete.emit(message, self.index)
+        except Exception as e:
+            logging.exception(e)
index 493e8d766537116fd01819cc26ff0bdf6bd3733b..893a3035658c93b55926470a2da0eb1bcab8d523 100644 (file)
@@ -37,7 +37,7 @@ class Prescriber:
             else:
                 self.properties = None
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def read_from(self, file):
         try:
@@ -120,7 +120,7 @@ class Prescription:
             else:
                 self.properties = None
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def get_json(self):
         return(json.dumps(self, default=lambda o: o.__dict__, indent=4))
@@ -132,7 +132,7 @@ class Prescription:
             except AttributeError as e:
                 pass
             except Exception as e:
-                logging.warning(e)
+                logging.exception(e)
             f.write(self.get_json())
         self.file=file
 
index 137416530cc19ca42ec5babb195a1d294dd394e2..864df924ee80eae9928dbac3e47669aa6cd82c78 100644 (file)
@@ -73,7 +73,7 @@ class RenderBox(QMainWindow):
         except Exception as e:
             QMessageBox.warning(self,"Display failed", "Failed to display file.")
             self.hide()
-            logging.warning(e)
+            logging.exception(e)
 
 
 class UnrenderBox(QDialog):
index dfb28b75c874d25cdb3d51be20b4ec7478e34f2a..2cde157adc5a17edcfaa4ff05e16b5d75c3ac603 100644 (file)
@@ -29,7 +29,7 @@ class Renderer:
                 try:
                     data["date"]=datetime.datetime.strptime(data["date"], "%Y-%m-%d %H:%M:%S")
                 except Exception as e:
-                    logging.warning(e)
+                    logging.exception(e)
                 output=template_data.render(data)
                 target_file.write(output)
         return(target)
index 69a285dfe103a2d195efb3db7a9fbac253691a6f..5b3ce2cefbf11de8d1b60f25b8cb853648982751 100644 (file)
@@ -53,7 +53,7 @@ class EditConfiguration(QDialog):
             self.input_root.setText(self.config["root_bundle"])
         except Exception as e:
             QMessageBox.critical(self,"Failed to load", "Failed to load the data into the application.")
-            logging.warning(e)
+            logging.exception(e)
 
     def save(self):
         if(QMessageBox.StandardButton.Yes==QMessageBox.question(self,"Confirm Save", "This action will overwrite the previous configuration. Continue?")):
@@ -77,7 +77,7 @@ class EditConfiguration(QDialog):
                 self.close()
             except Exception as e:
                 QMessageBox.critical(self,"Failed to save", "Failed to save the data to the file.")
-                logging.warning(e)
+                logging.exception(e)
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
@@ -86,7 +86,7 @@ class EditConfiguration(QDialog):
             with open(config_file) as f:
                 self.config=json.loads(f.read()) | config
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
             self.config=config
 
         self.setWindowTitle("Configuration")
index 259e1d826dfce9925334082492a3a747b0e5d448..9a35ad61865ae571eb6196cbf0dbd349b4a4dca7 100644 (file)
@@ -32,7 +32,7 @@ class Signature():
             if(not Signature.verify_chain(certificate)):
                 return False
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
             return False
 
         with open(certificate, "rb") as f:
@@ -45,7 +45,7 @@ class Signature():
                 subattr+=i.oid._name+":"+i.value+"\n"
             return subattr
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
             return False
 
     def verify_chain(cert_chain_path):
index b351a9a8ad03216c072dc252add1f63a4a788608..7fb5774c16833b60c9085c19c1a19f3ad29bdb85 100644 (file)
@@ -13,15 +13,20 @@ import logging, os, json, csv
 class Tabular():
 
     def export(filename):
-        files=glob(os.path.join(config["document_directory"], "**", "*.mpaz"), recursive=True)
-        with open(filename, "w", newline="") as ss:
-            writer=csv.writer(ss, delimiter=config["preset_delimiter"], quoting=csv.QUOTE_MINIMAL)
-            writer.writerow(["pid", "id", "date", "name", "dob", "age", "sex", "address", "contact", "extra", "mode", "daw", "diagnosis", "note", "report", "advice", "investigation", "medication", "additional", "certificate", "prescriber"])
-            for file in files:
-                try:
-                    with ZipFile(file) as zf:
-                        with zf.open("prescription.json") as pf:
-                            pres=json.loads(pf.read())
-                            writer.writerow([pres["pid"], pres["id"], pres["date"], pres["name"], pres["dob"], pres["age"], pres["sex"], pres["address"], pres["contact"], pres["extra"], pres["mode"], pres["daw"], pres["diagnosis"], pres["note"], pres["report"], pres["advice"], pres["investigation"], pres["medication"], pres["additional"], pres["certificate"], pres["prescriber"]["name"]])
-                except Exception as e:
-                    logging.warning(e)
+        try:
+            files=glob(os.path.join(config["document_directory"], "**", "*.mpaz"), recursive=True)
+            with open(filename, "w", newline="") as ss:
+                writer=csv.writer(ss, delimiter=config["preset_delimiter"], quoting=csv.QUOTE_MINIMAL)
+                writer.writerow(["pid", "id", "date", "name", "dob", "age", "sex", "address", "contact", "extra", "mode", "daw", "diagnosis", "note", "report", "advice", "investigation", "medication", "additional", "certificate", "prescriber"])
+                for file in files:
+                    try:
+                        with ZipFile(file) as zf:
+                            with zf.open("prescription.json") as pf:
+                                pres=json.loads(pf.read())
+                                writer.writerow([pres["pid"], pres["id"], pres["date"], pres["name"], pres["dob"], pres["age"], pres["sex"], pres["address"], pres["contact"], pres["extra"], pres["mode"], pres["daw"], pres["diagnosis"], pres["note"], pres["report"], pres["advice"], pres["investigation"], pres["medication"], pres["additional"], pres["certificate"], pres["prescriber"]["name"]])
+                    except Exception as e:
+                        logging.exception(e)
+        except FileNotFoundError as e:
+            logging.warning(e)
+        except Exception as e:
+            logging.exception(e)
index 2ad05f8b752b5678f6590ca79b8d0c596914e5c6..619d6e3a99978e039788bf37dc84741526183a32 100644 (file)
--- a/window.py
+++ b/window.py
@@ -63,9 +63,11 @@ class MainWindow(QMainWindow):
                 self.save_state=md5(self.prescription.get_json().encode()).hexdigest()
                 self.load_attachment(self.current_file.list())
                 self.unchanged_state=True
+            except FileNotFoundError as e:
+                logging.warning(e)
             except Exception as e:
                 QMessageBox.warning(self,"Open failed", "Failed to open file.")
-                logging.warning(e)
+                logging.exception(e)
 
     def cmd_copy(self, data):
         self.cmd_new()
@@ -104,7 +106,7 @@ class MainWindow(QMainWindow):
                 self.save_state=md5(self.prescription.get_json().encode()).hexdigest()
             except Exception as e:
                 QMessageBox.warning(self,"Save failed", "Failed to save file.")
-                logging.warning(e)
+                logging.exception(e)
 
     def cmd_save_as(self):
         suggest=self.prescription.id if(self.prescription.id) else self.prescription.name
@@ -164,10 +166,10 @@ class MainWindow(QMainWindow):
                         logging.warning(e)
                         QMessageBox.information(self, "Failed to load", "Failed to sign. Please check if certificate and key match.")
                     except Exception as e:
-                        logging.warning(e)
+                        logging.exception(e)
                         QMessageBox.information(self, "Failed", "Failed to sign.")
                 except Exception as e:
-                    logging.warning(e)
+                    logging.exception(e)
         else:
            QMessageBox.information(self, "Save first", "Please save the file before signing.")
 
@@ -190,7 +192,7 @@ class MainWindow(QMainWindow):
             logging.warning(e)
             QMessageBox.warning(self, "No Siganture", "No signature was found.")
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
             QMessageBox.warning(self, "Failed", "Failed to verify.")
 
     def cmd_tabular(self):
@@ -199,7 +201,7 @@ class MainWindow(QMainWindow):
             Tabular.export(filename)
             QMessageBox.information(self, "Data Exported", "Data exported to."+filename)
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
             QMessageBox.critical(self, "Export failed", "Failed to export the data.")
 
     def cmd_index(self):
@@ -343,7 +345,7 @@ class MainWindow(QMainWindow):
                     d=QDateTime.fromString(pdate.strftime("%Y-%m-%d %H:%M:%S"), "yyyy-MM-dd hh:mm:ss")
                 except Exception as e:
                     QMessageBox.warning(self,"Failed to load", str(e))
-                    logging.warning(e)
+                    logging.exception(e)
             self.input_date.setDateTime(d)
             self.input_id.setText(id)
             self.input_pid.setText(pid)
@@ -377,7 +379,7 @@ class MainWindow(QMainWindow):
             self.label_prescriber.setText(self.prescriber.name)
         except Exception as e:
             QMessageBox.warning(self,"Failed to load", "Failed to load the data into the application.")
-            logging.warning(e)
+            logging.exception(e)
 
     def load_interface_from_instance(self):
         if(self.current_file.has_template()):
@@ -470,7 +472,7 @@ class MainWindow(QMainWindow):
                 self.input_attachment.addItem(new)
         except Exception as e:
             QMessageBox.warning(self,"Attach failed", "Failed to attach file.")
-            logging.warning(e)
+            logging.exception(e)
 
     def remove_attachment(self):
         index=self.input_attachment.currentRow()
@@ -484,7 +486,7 @@ class MainWindow(QMainWindow):
         try:
             shutil.copyfile(self.input_attachment.currentItem().text(), QFileDialog.getSaveFileName(self, "Save Attachment", os.path.join(config["document_directory"], os.path.basename(self.input_attachment.currentItem().text())))[0])
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
 
     def load_attachment(self, attachments):
         for attach in attachments:
@@ -634,7 +636,7 @@ class MainWindow(QMainWindow):
                     action_plugin[-1].triggered.connect(partial(self.plugin.run, i[0], self.prescription))
                     action_plugin[-1].triggered.connect(self.load_interface_from_instance)
             except Exception as e:
-                logging.warning(e)
+                logging.exception(e)
             menu_plugin=menubar.addMenu("Plugin")
             for i in action_plugin:
                 menu_plugin.addAction(i)
@@ -662,7 +664,7 @@ class MainWindow(QMainWindow):
             templates.remove(os.path.basename(config["template"]))
             templates.insert(0, os.path.basename(config["template"]))
         except Exception as e:
-            logging.warning(e)
+            logging.exception(e)
         self.input_template.addItems(templates)
         toolbar.addWidget(self.input_template)
         spacer=QWidget(self)