]> Softwares of Agnibho - medscript.git/blob - config.py
Bugfix: Windows uninstall package permission error
[medscript.git] / config.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, argparse, json, os, sys, shutil, copy
9
10 default_config_file=os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "data", "config.json"))
11
12 real_dir=os.path.dirname(os.path.realpath(sys.argv[0]))
13
14 with open(os.path.join(real_dir, "info.json")) as info_file:
15 info=json.loads(info_file.read())
16
17 parser = argparse.ArgumentParser()
18 parser.add_argument("filename", nargs="?")
19 parser.add_argument("-c", "--config")
20 parser.add_argument("-p", "--prescriber")
21 args = parser.parse_args()
22
23 if(args.config is None):
24 config_file=default_config_file
25 else:
26 config_file=args.config
27
28 default = {
29 "data_directory": "data",
30 "document_directory": "document",
31 "prescriber_directory": "prescriber",
32 "prescriber": "prescriber",
33 "template_directory": "template",
34 "template": "default",
35 "preset_directory": "preset",
36 "form_directory": "form",
37 "enable_form": False,
38 "age_default": True,
39 "plugin_directory": "plugin",
40 "enable_plugin": True,
41 "log_directory": "log",
42 "preset_newline": True,
43 "preset_delimiter": ",",
44 "markdown": False,
45 "check_update": False,
46 "smime": False,
47 "root_bundle": "",
48 "private_key": "",
49 "certificate": ""
50 }
51
52 try:
53 with open(config_file) as conf:
54 read = json.loads(conf.read())
55 config = default | read
56 except FileNotFoundError as e:
57 logging.critical(e)
58 config=default
59 except Exception as e:
60 logging.exception(e)
61 config=default
62
63 config_orig=copy.deepcopy(config)
64 config["filename"]=args.filename
65 config["data_directory"]=os.path.abspath(os.path.join(real_dir, os.path.expanduser(config["data_directory"])))
66 config["document_directory"]=os.path.join(config["data_directory"], config["document_directory"])
67 config["preset_directory"]=os.path.join(config["data_directory"], config["preset_directory"])
68 config["form_directory"]=os.path.join(config["data_directory"], config["form_directory"])
69 config["plugin_directory"]=os.path.join(config["data_directory"], config["plugin_directory"])
70 config["template_directory"]=os.path.join(config["data_directory"], config["template_directory"])
71 config["template"]=os.path.join(config["template_directory"], config["template"])
72 config["log_directory"]=os.path.join(config["data_directory"], config["log_directory"])
73 config["resource"]=os.path.abspath(os.path.join(real_dir, "resource"))
74 if(args.prescriber is None):
75 config["prescriber_directory"]=os.path.join(config["data_directory"], config["prescriber_directory"])
76 config["prescriber"]=os.path.join(config["prescriber_directory"], config["prescriber"])
77 if (not config["prescriber"].endswith(".json")): config["prescriber"]=config["prescriber"]+".json"
78 else:
79 if(not os.path.isabs(args.prescriber)):
80 args.prescriber=os.path.join(config["prescriber_directory"], args.prescriber)
81 if(os.path.isfile(args.prescriber)):
82 config["prescriber"]=args.prescriber
83 else:
84 config["prescriber"]=os.path.join(config["prescriber_directory"], config["prescriber"])
85 logging.warning("File "+args.prescriber+" not found.")
86
87 os.makedirs(config["data_directory"], exist_ok=True)
88 os.makedirs(config["document_directory"], exist_ok=True)
89 os.makedirs(config["prescriber_directory"], exist_ok=True)
90 os.makedirs(config["preset_directory"], exist_ok=True)
91 os.makedirs(config["form_directory"], exist_ok=True)
92 os.makedirs(config["plugin_directory"], exist_ok=True)
93 os.makedirs(config["template_directory"], exist_ok=True)
94 os.makedirs(config["log_directory"], exist_ok=True)
95 if not os.path.exists(os.path.join(config["data_directory"], "config.json")):
96 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "config.json")), os.path.join(config["data_directory"], "config.json"))
97 if not os.path.exists(os.path.join(config["prescriber_directory"], "prescriber.json")):
98 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "prescriber", "prescriber.json")), os.path.join(config["prescriber_directory"], "prescriber.json"))
99 if not os.path.exists(os.path.join(config["template_directory"], "default")):
100 shutil.copytree(os.path.abspath(os.path.join(real_dir, "data", "template", "default")), os.path.join(config["template_directory"], "default"))
101 if not os.path.exists(os.path.join(config["template_directory"], "medcert")):
102 shutil.copytree(os.path.abspath(os.path.join(real_dir, "data", "template", "medcert")), os.path.join(config["template_directory"], "medcert"))
103 if not os.path.exists(os.path.join(config["preset_directory"], "certificate.csv")):
104 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "preset", "certificate.csv")), os.path.join(config["preset_directory"], "certificate.csv"))
105 if not os.path.exists(os.path.join(config["preset_directory"], "note.csv")):
106 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "preset", "note.csv")), os.path.join(config["preset_directory"], "note.csv"))
107 if not os.path.exists(os.path.join(config["preset_directory"], "report.csv")):
108 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "preset", "report.csv")), os.path.join(config["preset_directory"], "report.csv"))
109 if not os.path.exists(os.path.join(config["preset_directory"], "investigation.csv")):
110 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "preset", "investigation.csv")), os.path.join(config["preset_directory"], "investigation.csv"))
111 if not os.path.exists(os.path.join(config["preset_directory"], "advice.csv")):
112 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "preset", "advice.csv")), os.path.join(config["preset_directory"], "advice.csv"))
113 if not os.path.exists(os.path.join(config["preset_directory"], "medication.csv")):
114 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "preset", "medication.csv")), os.path.join(config["preset_directory"], "medication.csv"))
115 if not os.path.exists(os.path.join(config["preset_directory"], "additional.csv")):
116 shutil.copyfile(os.path.abspath(os.path.join(real_dir, "data", "preset", "additional.csv")), os.path.join(config["preset_directory"], "additional.csv"))