]> Softwares of Agnibho - medscript.git/blob - plugin.py
a45b801c7c95f1bfb1c79ea5d84da4c2eb794615
[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.warning(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.warning(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.warning(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.warning(e)
96
97 def run(self, module, prescription):
98 try:
99 if(hasattr(module, "run") and callable(module.run)):
100 if(hasattr(module, "input") and callable(module.input)):
101 module.input(self.input())
102 if(hasattr(module, "fileopen") and callable(module.fileopen)):
103 module.fileopen(QFileDialog.getOpenFileName()[0])
104 if(hasattr(module, "filesave") and callable(module.filesave)):
105 module.filesave(QFileDialog.getSaveFileName()[0])
106 if(hasattr(module, "background") and module.background):
107 self.showMessage("Module "+module.__name__+" will run in background.")
108 self.workers.append(Worker(module.run, prescription))
109 index=len(self.workers)-1
110 self.workers[index].setIndex(index)
111 self.workers[index].pluginComplete.connect(self.showMessage)
112 self.workers[index].start()
113 else:
114 message=module.run(prescription)
115 if(message):
116 self.showMessage(message)
117 except Exception as e:
118 logging.warning(e)
119
120 def input(self):
121 try:
122 text, ok=QInputDialog.getText(None, "User input", "Enter text:")
123 if text and ok:
124 return text
125 else:
126 return ""
127 except Exception as e:
128 logging.warning(e)
129
130 def showMessage(self, message, index=None):
131 QMessageBox.information(None, "Information", message)
132 if index is not None:
133 self.workers[index]=None
134
135 class Worker(QThread):
136
137 pluginComplete=pyqtSignal(str, int)
138 function=None
139 prescription=None
140 index=None
141
142 def __init__(self, function, prescription):
143 super().__init__()
144 self.function=function
145 self.prescription=prescription
146
147 def setIndex(self, index):
148 self.index=index
149
150 def run(self):
151 prescription_copy=copy.deepcopy(self.prescription)
152 message=self.function(prescription_copy)
153 self.pluginComplete.emit(message, self.index)