]> Softwares of Agnibho - medscript.git/blob - preset.py
Bugfix: Windows uninstall package permission error
[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 filelist=glob(os.path.join(config["preset_directory"], self.target+"*"+".csv"))
26 filelist.append(filelist.pop(filelist.index(os.path.join(config["preset_directory"], self.target+".csv"))))
27 for file in filelist:
28 with open(file, "r") as f:
29 reader=csv.reader(f, delimiter=config["preset_delimiter"])
30 if skip_first:
31 next(reader)
32 for row in reader:
33 try:
34 self.data[row[0]]=row[1]
35 if text_as_key:
36 buf[row[1].strip()]=row[1]
37 except IndexError as e:
38 logging.warning(e)
39 self.data = buf | self.data
40 self.data=dict(sorted(self.data.items()))
41 except FileNotFoundError as e:
42 logging.warning(e)
43 except IndexError as e:
44 logging.warning(e)
45 except StopIteration as e:
46 logging.warning(e)
47 except Exception as e:
48 logging.exception(e)