]> Softwares of Agnibho - medscript.git/blob - plugin.py
Bugfix: prescriber editor dialog open
[medscript.git] / plugin.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, importlib, copy
9 from PyQt6.QtWidgets import QMessageBox, QInputDialog, QFileDialog
10 from PyQt6.QtCore import QThread, pyqtSignal
11 from glob import glob
12 from config import config
13
14 class Plugin():
15
16 plugins=[]
17 names=[]
18 workers=[]
19
20 def __init__(self):
21 if(config["enable_plugin"]):
22 self.load()
23
24 def load(self):
25 plugin_list=glob(os.path.join(config["plugin_directory"], "*"))
26 for i in plugin_list:
27 try:
28 if(os.path.isdir(i)):
29 spec=importlib.util.spec_from_file_location(os.path.basename(i), os.path.join(i, "main.py"))
30 mod=importlib.util.module_from_spec(spec)
31 spec.loader.exec_module(mod)
32 self.plugins.append(mod)
33 except Exception as e:
34 logging.warning(i, ":", e)
35
36 def get_name(self, mod):
37 try:
38 return(mod.name)
39 except Exception as e:
40 return(mod.__name__)
41
42 def commands(self):
43 cmds=[]
44 for i in self.plugins:
45 if(hasattr(i, "run") and callable(i.run)):
46 cmds.append([i, self.get_name(i)])
47 return(cmds)
48
49 def new(self, prescription):
50 for i in self.plugins:
51 try:
52 if(hasattr(i, "new") and callable(i.new)):
53 if(hasattr(i, "input") and callable(i.input)):
54 i.input(self.input())
55 message=i.new(prescription)
56 if(message):
57 self.showMessage(message)
58 except Exception as e:
59 logging.exception(e)
60
61 def open(self, prescription):
62 for i in self.plugins:
63 try:
64 if(hasattr(i, "open") and callable(i.open)):
65 if(hasattr(i, "input") and callable(i.input)):
66 i.input(self.input())
67 message=i.open(prescription)
68 if(message):
69 self.showMessage(message)
70 except Exception as e:
71 logging.exception(e)
72
73 def save(self, prescription):
74 for i in self.plugins:
75 try:
76 if(hasattr(i, "save") and callable(i.save)):
77 if(hasattr(i, "input") and callable(i.input)):
78 i.input(self.input())
79 message=i.save(prescription)
80 if(message):
81 self.showMessage(message)
82 except Exception as e:
83 logging.exception(e)
84
85 def refresh(self, prescription):
86 for i in self.plugins:
87 try:
88 if(hasattr(i, "refresh") and callable(i.refresh)):
89 if(hasattr(i, "input") and callable(i.input)):
90 i.input(self.input())
91 message=i.refresh(prescription)
92 if(message):
93 self.showMessage(message)
94 except Exception as e:
95 logging.exception(e)
96
97 def run(self, module, prescription):
98 try:
99 if(hasattr(module, "run") and callable(module.run)):
100 if(hasattr(module, "confirm") and module.confirm):
101 if(QMessageBox.StandardButton.Yes!=QMessageBox.question(None,"Confirm", module.confirm)):
102 return
103 if(hasattr(module, "input") and callable(module.input)):
104 module.input(self.input())
105 if(hasattr(module, "fileopen") and callable(module.fileopen)):
106 module.fileopen(QFileDialog.getOpenFileName()[0])
107 if(hasattr(module, "filesave") and callable(module.filesave)):
108 module.filesave(QFileDialog.getSaveFileName()[0])
109 if(hasattr(module, "background") and module.background):
110 self.showMessage("Module "+module.__name__+" will run in background.")
111 self.workers.append(Worker(module.run, prescription))
112 index=len(self.workers)-1
113 self.workers[index].setIndex(index)
114 self.workers[index].pluginComplete.connect(self.showMessage)
115 self.workers[index].start()
116 else:
117 message=module.run(prescription)
118 if(message):
119 self.showMessage(message)
120 except Exception as e:
121 logging.exception(e)
122
123 def input(self):
124 try:
125 text, ok=QInputDialog.getText(None, "User input", "Enter text:")
126 if text and ok:
127 return text
128 else:
129 return ""
130 except Exception as e:
131 logging.exception(e)
132
133 def showMessage(self, message, index=None):
134 QMessageBox.information(None, "Information", message)
135 if index is not None:
136 self.workers[index]=None
137
138 class Worker(QThread):
139
140 pluginComplete=pyqtSignal(str, int)
141 function=None
142 prescription=None
143 index=None
144
145 def __init__(self, function, prescription):
146 super().__init__()
147 self.function=function
148 self.prescription=prescription
149
150 def setIndex(self, index):
151 self.index=index
152
153 def run(self):
154 try:
155 prescription_copy=copy.deepcopy(self.prescription)
156 message=self.function(prescription_copy)
157 self.pluginComplete.emit(message, self.index)
158 except Exception as e:
159 logging.exception(e)