]> Softwares of Agnibho - medscript.git/commitdiff
Multiple preset files
authorAgnibho Mondal <mondal@agnibho.com>
Mon, 6 Nov 2023 17:56:54 +0000 (23:26 +0530)
committerAgnibho Mondal <mondal@agnibho.com>
Tue, 7 Nov 2023 20:11:15 +0000 (01:41 +0530)
preset.py
window.py

index 13a90882579acb7350e924a2ec893466edfc073b..253f74565638a8b7180705bee07a39dd27e8365b 100644 (file)
--- a/preset.py
+++ b/preset.py
@@ -5,36 +5,38 @@
 # MedScript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 # You should have received a copy of the GNU General Public License along with MedScript. If not, see <https://www.gnu.org/licenses/>.
 
-import csv
+import os, csv
+from glob import glob
 from config import config
 
 class Preset():
 
-    file=""
+    target=""
     data={}
 
-    def __init__(self, file, skip_first=True, text_as_key=False):
-        self.file=file;
+    def __init__(self, target, skip_first=True, text_as_key=False):
+        self.target=target
         self.data={}
         self.load(skip_first, text_as_key);
 
     def load(self, skip_first=True, text_as_key=False):
         try:
             buf={}
-            with open(self.file, "r") as f:
-                reader=csv.reader(f, delimiter=config["preset_delimiter"])
-                if skip_first:
-                    next(reader)
-                for row in reader:
-                    self.data[row[0]]=row[1]
-                    if text_as_key:
-                        buf[row[1].strip()]=row[1]
+            for file in glob(os.path.join(config["preset_directory"], self.target+"*"+".csv")):
+                with open(file, "r") as f:
+                    reader=csv.reader(f, delimiter=config["preset_delimiter"])
+                    if skip_first:
+                        next(reader)
+                    for row in reader:
+                        self.data[row[0]]=row[1]
+                        if text_as_key:
+                            buf[row[1].strip()]=row[1]
             self.data = buf | self.data
         except FileNotFoundError as e:
-            print(self.file, e)
+            print(e)
         except IndexError as e:
-            print(self.file, e)
+            print(e)
         except StopIteration as e:
-            print(self.file, e, ": Check if file is empty")
+            print(e)
         except Exception as e:
-            print(self.file, e)
+            print(e)
index ea0300cfd3685b4c4e0bc499be6cc484cb0959e1..c4787f8154b8cfbd3a299a0266dc8cbcdf2a4163 100644 (file)
--- a/window.py
+++ b/window.py
@@ -512,13 +512,13 @@ class MainWindow(QMainWindow):
         icon_render=QIcon(os.path.join(config["resource"], "icon_render.svg"))
         icon_refresh=QIcon(os.path.join(config["resource"], "icon_refresh.svg"))
 
-        self.preset_note=Preset(os.path.join(config["preset_directory"], "note.csv"))
-        self.preset_report=Preset(os.path.join(config["preset_directory"], "report.csv"))
-        self.preset_advice=Preset(os.path.join(config["preset_directory"], "advice.csv"))
-        self.preset_investigation=Preset(os.path.join(config["preset_directory"], "investigation.csv"))
-        self.preset_medication=Preset(os.path.join(config["preset_directory"], "medication.csv"), text_as_key=True)
-        self.preset_additional=Preset(os.path.join(config["preset_directory"], "additional.csv"))
-        self.preset_certificate=Preset(os.path.join(config["preset_directory"], "certificate.csv"))
+        self.preset_note=Preset("note")
+        self.preset_report=Preset("report")
+        self.preset_advice=Preset("advice")
+        self.preset_investigation=Preset("investigation")
+        self.preset_medication=Preset("medication", text_as_key=True)
+        self.preset_additional=Preset("additional")
+        self.preset_certificate=Preset("certificate")
 
         action_new=QAction("New", self)
         action_new.setShortcut("Ctrl+N")