]>
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 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.1" }
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 ):
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 )
52 shutil
. copytree ( config
[ "template" ], template
, dirs_exist_ok
= True )
54 with
ZipFile ( self
. file , "w" , strict_timestamps
= False ) as target
:
55 for f
in glob
. glob ( os
. path
. join ( self
. directory
. name
, "**" , "*" ), recursive
= True ):
56 target
. write ( f
, os
. path
. relpath ( f
, self
. directory
. name
))
58 def open ( self
, file = None ):
61 with
ZipFile ( self
. file , "r" , strict_timestamps
= False ) as source
:
62 source
. extractall ( self
. directory
. name
)
64 def sign ( self
, password
= "" ):
65 with
open ( os
. path
. join ( self
. directory
. name
, "prescription.json" ), "r" ) as file :
67 signature
= Signature
. sign ( data
, certificate
= config
[ "certificate" ], privkey
= config
[ "private_key" ], password
= password
)
68 with
open ( os
. path
. join ( self
. directory
. name
, "signature.p7m" ), "w" ) as file :
70 shutil
. copyfile ( config
[ "certificate" ], os
. path
. join ( self
. directory
. name
, "certificate.pem" ))
73 with
open ( os
. path
. join ( self
. directory
. name
, "prescription.json" ), "r" ) as file :
76 with
open ( os
. path
. join ( self
. directory
. name
, "certificate.pem" )) as file :
77 certificate
= file . read ()
78 with
open ( os
. path
. join ( self
. directory
. name
, "signature.p7m" )) as file :
80 return Signature
. verify ( data
, certificate
= os
. path
. join ( self
. directory
. name
, "certificate.pem" ), signature
= os
. path
. join ( self
. directory
. name
, "signature.p7m" ))
81 except FileNotFoundError
as e
:
84 def delete_attachment ( self
, item
):
86 os
. unlink ( os
. path
. join ( self
. directory
. name
, "attachment" , os
. path
. basename ( item
)))
87 except Exception as e
:
90 def delete_sign ( self
):
92 os
. unlink ( os
. path
. join ( self
. directory
. name
, "certificate.pem" ))
93 os
. unlink ( os
. path
. join ( self
. directory
. name
, "signature.p7m" ))
94 except Exception as e
:
98 return ( os
. path
. exists ( os
. path
. join ( self
. directory
. name
, "certificate.pem" )) and ( os
. path
. exists ( os
. path
. join ( self
. directory
. name
, "signature.p7m" ))))