]> Softwares of Agnibho - medscript.git/blob - installer.py
Updated index view, copy
[medscript.git] / installer.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 from PyQt6.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QHBoxLayout, QPushButton, QListWidget, QMessageBox, QFileDialog
9 from PyQt6.QtGui import QIcon, QStandardItemModel, QStandardItem
10 from glob import glob
11 from zipfile import ZipFile
12 from config import config
13 import logging, os, tempfile, shutil
14
15 class Installer(QMainWindow):
16
17 preset={"name":[], "path":[]}
18 template={"name":[], "path":[]}
19 form={"name":[], "path":[]}
20 plugin={"name":[], "path":[]}
21
22 protected=["note", "report", "advice", "investigation", "medication", "additional", "certificate", "default", "medcert"]
23 directory=None
24
25 def cmd_install(self):
26 try:
27 file=QFileDialog.getOpenFileName(self, "Open Package", config["data_directory"], "Zip (*.zip);; All Files (*)")[0]
28 self.directory=tempfile.TemporaryDirectory()
29 with ZipFile(file, "r", strict_timestamps=False) as package:
30 package.extractall(self.directory.name)
31 for i in glob(os.path.join(self.directory.name, "preset", "*.csv")):
32 name=os.path.splitext(os.path.basename(i))[0]
33 if name not in self.protected:
34 if name not in self.preset["name"]:
35 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm install", "Installing PRESET <strong>"+name+"</strong>. Continue?")):
36 self.copy(i, config["preset_directory"])
37 else:
38 QMessageBox.information(self, "File exists", "PRESET <strong>"+name+"</strong> is already installed.")
39 for i in glob(os.path.join(self.directory.name, "template", "*")):
40 if os.path.exists(os.path.join(i, "index.html")):
41 name=os.path.basename(i)
42 if name not in self.protected:
43 if name not in self.template["name"]:
44 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm install", "Installing TEMPLATE <strong>"+name+"</strong>. Continue?")):
45 self.copy(i, os.path.join(config["template_directory"], name))
46 else:
47 QMessageBox.information(self, "File exists", "TEMPLATE <strong>"+name+"</strong> is already installed.")
48 for i in glob(os.path.join(self.directory.name, "form", "*.json")):
49 name=os.path.splitext(os.path.basename(i))[0]
50 if name not in self.protected:
51 if name not in self.form["name"]:
52 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm install", "Installing FORM <strong>"+name+"</strong>. Continue?")):
53 self.copy(i, config["form_directory"])
54 else:
55 QMessageBox.information(self, "File exists", "FORM <strong>"+name+"</strong> is already installed.")
56 for i in glob(os.path.join(self.directory.name, "plugin", "*")):
57 if os.path.exists(os.path.join(i, "main.py")):
58 name=os.path.basename(i)
59 if name not in self.protected:
60 if name not in self.plugin["name"]:
61 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm install", "Installing PLUGIN <strong>"+name+"</strong>. Continue?")):
62 self.copy(i, os.path.join(config["plugin_directory"], name))
63 else:
64 QMessageBox.information(self, "File exists", "PLUGIN <strong>"+name+"</strong> is already installed.")
65 QMessageBox.information(self, "Restart", "Please restart MedScript for the changes to take effect.")
66 except Exception as e:
67 logging.warning(e)
68
69 def cmd_uninstall(self):
70 txt=self.installed.currentItem().text().split("\t")
71 name=txt[1]
72 group=txt[0].replace("[", "").replace("]", "")
73 if name not in self.protected:
74 if(group=="preset"):
75 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm uninstall", "Uninstalling PRESET <strong>"+name+"</strong>. Continue?")):
76 idx=self.preset["name"].index(name)
77 path=self.preset["path"][idx]
78 self.delete(path)
79 elif(group=="template"):
80 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm uninstall", "Uninstalling TEMPLATE <strong>"+name+"</strong>. Continue?")):
81 idx=self.template["name"].index(name)
82 path=self.template["path"][idx]
83 self.delete(path)
84 elif(group=="form"):
85 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm uninstall", "Uninstalling FORM <strong>"+name+"</strong>. Continue?")):
86 idx=self.form["name"].index(name)
87 path=self.form["path"][idx]
88 self.delete(path)
89 elif(group=="plugin"):
90 if(QMessageBox.StandardButton.Yes==QMessageBox.question(self, "Confirm uninstall", "Uninstalling PLUGIN <strong>"+name+"</strong>. Continue?")):
91 idx=self.plugin["name"].index(name)
92 path=self.plugin["path"][idx]
93 self.delete(path)
94 QMessageBox.information(self, "Restart", "Please restart MedScript for the changes to take effect.")
95 else:
96 QMessageBox.information(self, "Item protected", "Protected items cannot be deleted.")
97
98 def delete(self, path):
99 try:
100 os.unlink(path)
101 except IsADirectoryError:
102 shutil.rmtree(path)
103 except Exception as e:
104 logging.critical(e)
105 self.load()
106
107 def copy(self, path, destination):
108 try:
109 shutil.copytree(path, destination)
110 except NotADirectoryError:
111 shutil.copy(path, destination)
112 except Exception as e:
113 logging.critical(e)
114 self.load()
115
116 def __init__(self, *args, **kwargs):
117 super().__init__(*args, **kwargs)
118
119 self.setWindowTitle("MedScript")
120 self.setGeometry(200, 200, 600, 400)
121
122 widget=QWidget(self)
123 layout=QVBoxLayout(widget)
124 self.installed=QListWidget()
125 layout.addWidget(self.installed)
126 layout2=QHBoxLayout()
127 button_install=QPushButton("Install")
128 button_install.clicked.connect(self.cmd_install)
129 button_uninstall=QPushButton("Uninstall")
130 button_uninstall.clicked.connect(self.cmd_uninstall)
131 layout2.addWidget(button_install)
132 layout2.addWidget(button_uninstall)
133 layout.addLayout(layout2)
134
135 self.setCentralWidget(widget)
136 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
137
138 self.load()
139
140 def load(self, file=None):
141 self.preset={"name":[], "path":[]}
142 self.template={"name":[], "path":[]}
143 self.form={"name":[], "path":[]}
144 self.plugin={"name":[], "path":[]}
145 self.installed.clear()
146 try:
147 for i in glob(os.path.join(config["preset_directory"], "*.csv")):
148 self.preset["name"].append(os.path.splitext(os.path.basename(i))[0])
149 self.preset["path"].append(i)
150 for i in glob(os.path.join(config["template_directory"], "*")):
151 if(os.path.exists(os.path.join(i, "index.html"))):
152 self.template["name"].append(os.path.basename(i))
153 self.template["path"].append(i)
154 for i in glob(os.path.join(config["form_directory"], "*.json")):
155 self.form["name"].append(os.path.splitext(os.path.basename(i))[0])
156 self.form["path"].append(i)
157 for i in glob(os.path.join(config["plugin_directory"], "*")):
158 if(os.path.exists(os.path.join(i, "main.py"))):
159 self.plugin["name"].append(os.path.basename(i))
160 self.plugin["path"].append(i)
161
162 for i in self.preset["name"]:
163 self.installed.addItem("[preset]\t"+i)
164 for i in self.template["name"]:
165 self.installed.addItem("[template]\t"+i)
166 for i in self.form["name"]:
167 self.installed.addItem("[form]\t"+i)
168 for i in self.plugin["name"]:
169 self.installed.addItem("[plugin]\t"+i)
170 except Exception as e:
171 logging.warning(e)