From: Agnibho Mondal Date: Mon, 6 Nov 2023 17:56:54 +0000 (+0530) Subject: Multiple preset files X-Git-Tag: v0.5~55 X-Git-Url: https://code.agnibho.com/repo?a=commitdiff_plain;h=5a6e3afc1650a01f84bbacd87523631dfb04d0c9;p=medscript.git Multiple preset files --- diff --git a/preset.py b/preset.py index 13a9088..253f745 100644 --- 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 . -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) diff --git a/window.py b/window.py index ea0300c..c4787f8 100644 --- 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")