]>
Softwares of Agnibho - medscript.git/blob - filehandler.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
, glob
, tempfile
, json
9 from zipfile
import ZipFile
10 from config
import config
11 from signature
import Signature
15 meta
={ "type" : "MedScript" , "version" : "0.3" }
20 def __init__ ( self
, file = "" ):
23 def reset ( self
, file = "" ):
25 self
. directory
= tempfile
. TemporaryDirectory ()
27 def set_file ( self
, file ):
30 def copy ( self
, file , category
= "attachment" ):
31 dirname
= os
. path
. join ( self
. directory
. name
, category
)
32 os
. makedirs ( dirname
, exist_ok
= True )
34 shutil
. copyfile ( file , os
. path
. join ( dirname
, os
. path
. basename ( file )))
35 except shutil
. SameFileError
as e
:
38 def list ( self
, category
= "attachment" ):
40 dirname
= os
. path
. join ( self
. directory
. name
, category
)
41 for f
in glob
. glob ( os
. path
. join ( dirname
, "*" ), recursive
= True ):
45 def save ( self
, file = None , change_template
= True ):
48 with
open ( os
. path
. join ( self
. directory
. name
, "meta.json" ), "w" ) as f
:
49 f
. write ( json
. dumps ( self
. meta
))
50 template
= os
. path
. join ( self
. directory
. name
, "template" )
51 os
. makedirs ( template
, exist_ok
= True )
53 shutil
. copytree ( config
[ "template" ], template
, dirs_exist_ok
= True )
56 os
. remove ( os
. path
. join ( template
, "output.html" ))
60 with
ZipFile ( self
. file , "w" , strict_timestamps
= False ) as target
:
61 for f
in glob
. glob ( os
. path
. join ( self
. directory
. name
, "**" , "*" ), recursive
= True ):
62 target
. write ( f
, os
. path
. relpath ( f
, self
. directory
. name
))
64 def open ( self
, file = None ):
67 with
ZipFile ( self
. file , "r" , strict_timestamps
= False ) as source
:
68 source
. extractall ( self
. directory
. name
)
70 def sign ( self
, password
= "" ):
71 with
open ( os
. path
. join ( self
. directory
. name
, "prescription.json" ), "r" ) as file :
73 signature
= Signature
. sign ( data
, certificate
= config
[ "certificate" ], privkey
= config
[ "private_key" ], password
= password
)
74 with
open ( os
. path
. join ( self
. directory
. name
, "signature" ), "wb" ) as file :
76 shutil
. copyfile ( config
[ "certificate" ], os
. path
. join ( self
. directory
. name
, "certificate.pem" ))
79 with
open ( os
. path
. join ( self
. directory
. name
, "prescription.json" ), "r" ) as file :
82 with
open ( os
. path
. join ( self
. directory
. name
, "certificate.pem" ), "r" ) as file :
83 certificate
= file . read ()
84 with
open ( os
. path
. join ( self
. directory
. name
, "signature" ), "rb" ) as file :
86 return Signature
. verify ( data
, certificate
= os
. path
. join ( self
. directory
. name
, "certificate.pem" ), signature
= signature
)
87 except FileNotFoundError
as e
:
90 def delete_attachment ( self
, item
):
92 os
. unlink ( os
. path
. join ( self
. directory
. name
, "attachment" , os
. path
. basename ( item
)))
93 except Exception as e
:
96 def delete_sign ( self
):
98 os
. unlink ( os
. path
. join ( self
. directory
. name
, "certificate.pem" ))
99 os
. unlink ( os
. path
. join ( self
. directory
. name
, "signature" ))
100 except Exception as e
:
103 def has_template ( self
):
104 return ( os
. path
. exists ( os
. path
. join ( self
. directory
. name
, "template" , "index.html" )))
107 return ( os
. path
. exists ( os
. path
. join ( self
. directory
. name
, "certificate.pem" )) and ( os
. path
. exists ( os
. path
. join ( self
. directory
. name
, "signature.p7m" ))))