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