]> Softwares of Agnibho - medscript.git/blob - renderer.py
Bugfix: Windows uninstall package permission error
[medscript.git] / renderer.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, shutil, tempfile, json, datetime, re
9 from markdown import markdown
10 from jinja2 import Template
11 from config import config
12
13 class Renderer:
14
15 tempdir=None
16
17 def render(self, data_directory):
18 try:
19 source=os.path.join(data_directory, "prescription.json")
20 target=os.path.join(data_directory, "template", "output.html")
21 template=os.path.join(data_directory, "template", "index.html")
22 if not os.path.exists(template):
23 shutil.copytree(config["template"], os.path.join(data_directory, "template"), dirs_exist_ok=True)
24 with open(source, "r") as source_file, open(target, "w") as target_file:
25 with open(template) as template_file:
26 template_data = Template(template_file.read())
27 data=self.process_medication(self.process_diagnosis(json.loads(source_file.read())))
28 if config["markdown"]:
29 data=self.render_markdown(data)
30 try:
31 data["date"]=datetime.datetime.strptime(data["date"], "%Y-%m-%d %H:%M:%S")
32 except Exception as e:
33 logging.exception(e)
34 output=template_data.render(data)
35 target_file.write(output)
36 return(target)
37 except Exception as e:
38 logging.exception(e)
39
40 def process_diagnosis(self, data):
41 diagnosis_list=[]
42 for d in data["diagnosis"].split(";"):
43 diagnosis_list.append(d.strip())
44 data["diagnosis_list"]=diagnosis_list
45 return data
46
47 def process_medication(self, data):
48 medication_list=[]
49 pattern=re.compile(r".*?\[(.*)\].*")
50 for line in data["medication"].splitlines():
51 if(line):
52 try:
53 f2=re.search(pattern, line).group(1)
54 f1=line.replace("["+f2+"]", "")
55 medication_list.append([f1, f2])
56 except AttributeError:
57 medication_list.append([line, ""])
58 data["medication_list"]=medication_list
59 return data
60
61 def render_markdown(self, data):
62 data["extra"]=markdown(data["extra"])
63 data["note"]=markdown(data["note"])
64 data["report"]=markdown(data["report"])
65 data["advice"]=markdown(data["advice"])
66 data["investigation"]=markdown(data["investigation"])
67 data["additional"]=markdown(data["additional"])
68 return data