]> Softwares of Agnibho - medscript.git/blob - filehandler.py
Bugfix: Windows uninstall package permission error
[medscript.git] / filehandler.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, glob, tempfile, json
9 from zipfile import ZipFile
10 from config import config
11 from signature import Signature
12
13 class FileHandler():
14
15 meta={"type":"MedScript", "version":"0.3"}
16
17 file=""
18 directory=""
19
20 def __init__(self, file=""):
21 self.reset(file)
22
23 def reset(self, file=""):
24 self.file=file
25 self.directory=tempfile.TemporaryDirectory()
26
27 def set_file(self, file):
28 self.file=file
29
30 def copy(self, file, category="attachment"):
31 dirname=os.path.join(self.directory.name, category)
32 os.makedirs(dirname, exist_ok=True)
33 try:
34 shutil.copyfile(file, os.path.join(dirname, os.path.basename(file)))
35 except shutil.SameFileError as e:
36 logging.warning(e)
37
38 def list(self, category="attachment"):
39 items=[]
40 dirname=os.path.join(self.directory.name, category)
41 for f in glob.glob(os.path.join(dirname,"*"), recursive=True):
42 items.append(f)
43 return(items)
44
45 def save(self, file=None, change_template=True):
46 if file is not None:
47 self.file=file
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 if(change_template):
53 shutil.copytree(config["template"], template, dirs_exist_ok=True)
54 else:
55 try:
56 os.remove(os.path.join(template, "output.html"))
57 except:
58 pass
59
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))
63
64 def open(self, file=None):
65 if file is not None:
66 self.file=file
67 with ZipFile(self.file, "r", strict_timestamps=False) as source:
68 source.extractall(self.directory.name)
69
70 def sign(self, password=""):
71 with open(os.path.join(self.directory.name, "prescription.json"), "r") as file:
72 data=file.read()
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:
75 file.write(signature)
76 shutil.copyfile(config["certificate"], os.path.join(self.directory.name, "certificate.pem"))
77
78 def verify(self):
79 with open(os.path.join(self.directory.name, "prescription.json"), "r") as file:
80 data=file.read()
81 try:
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:
85 signature=file.read()
86 return Signature.verify(data, certificate=os.path.join(self.directory.name, "certificate.pem"), signature=signature)
87 except FileNotFoundError as e:
88 logging.warning(e)
89
90 def delete_attachment(self, item):
91 try:
92 os.unlink(os.path.join(self.directory.name, "attachment", os.path.basename(item)))
93 except Exception as e:
94 logging.exception(e)
95
96 def delete_sign(self):
97 try:
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:
101 logging.exception(e)
102
103 def has_template(self):
104 return(os.path.exists(os.path.join(self.directory.name, "template", "index.html")))
105
106 def is_signed(self):
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"))))