]>
Softwares of Agnibho - medscript.git/blob - renderer.py
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/>.
8 import logging
, os
, shutil
, tempfile
, json
, datetime
, re
9 from markdown
import markdown
10 from jinja2
import Template
11 from config
import config
17 def render ( self
, data_directory
):
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
)
31 data
[ "date" ]= datetime
. datetime
. strptime ( data
[ "date" ], "%Y-%m- %d %H:%M:%S" )
32 except Exception as e
:
34 output
= template_data
. render ( data
)
35 target_file
. write ( output
)
37 except Exception as e
:
40 def process_diagnosis ( self
, data
):
42 for d
in data
[ "diagnosis" ]. split ( ";" ):
43 diagnosis_list
. append ( d
. strip ())
44 data
[ "diagnosis_list" ]= diagnosis_list
47 def process_medication ( self
, data
):
49 pattern
= re
. compile ( r
".*?\[(.*)\].*" )
50 for line
in data
[ "medication" ]. splitlines ():
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
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" ])