]> Softwares of Agnibho - medscript.git/blob - preset.py
c36b1c92978732c50490d3e6ac358b79227ae9d8
[medscript.git] / preset.py
1 # MedScript
2 # Copyright (C) 2023 Dr. Agnibho Mondal
3 # This file is part of MedScript.
4 # MedScript is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5 # 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.
6 # You should have received a copy of the GNU General Public License along with MedScript. If not, see <https://www.gnu.org/licenses/>.
7
8 import logging, os, csv
9 from glob import glob
10 from config import config
11
12 class Preset():
13
14 target=""
15 data={}
16
17 def __init__(self, target, skip_first=True, text_as_key=False):
18 self.target=target
19 self.data={}
20 self.load(skip_first, text_as_key);
21
22 def load(self, skip_first=True, text_as_key=False):
23 try:
24 buf={}
25 for file in glob(os.path.join(config["preset_directory"], self.target+"*"+".csv")):
26 with open(file, "r") as f:
27 reader=csv.reader(f, delimiter=config["preset_delimiter"])
28 if skip_first:
29 next(reader)
30 for row in reader:
31 try:
32 self.data[row[0]]=row[1]
33 if text_as_key:
34 buf[row[1].strip()]=row[1]
35 except IndexError as e:
36 logging.warning(e)
37 self.data = buf | self.data
38 except FileNotFoundError as e:
39 logging.warning(e)
40 except IndexError as e:
41 logging.warning(e)
42 except StopIteration as e:
43 logging.warning(e)
44 except Exception as e:
45 logging.exception(e)