]> Softwares of Agnibho - medscript.git/blob - plugin.py
da9552bc0cbd060d3801ae63f7a04a7c8219aefd
[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 os, importlib
9 from PyQt6.QtWidgets import QMessageBox, QInputDialog, QFileDialog
10 from glob import glob
11 from config import config
12
13 class Plugin():
14
15 plugins=[]
16 names=[]
17
18 def __init__(self):
19 if(config["enable_plugin"]):
20 self.load()
21
22 def load(self):
23 plugin_list=glob(os.path.join(config["plugin_directory"], "*"))
24 for i in plugin_list:
25 try:
26 if(os.path.isdir(i)):
27 spec=importlib.util.spec_from_file_location(os.path.basename(i), os.path.join(i, "main.py"))
28 mod=importlib.util.module_from_spec(spec)
29 spec.loader.exec_module(mod)
30 self.plugins.append(mod)
31 except Exception as e:
32 print(i, ":", e)
33
34 def get_name(self, mod):
35 try:
36 return(mod.name)
37 except Exception as e:
38 return(mod.__name__)
39
40 def commands(self):
41 cmds=[]
42 for i in self.plugins:
43 if(hasattr(i, "run") and callable(i.run)):
44 cmds.append([i, self.get_name(i)])
45 return(cmds)
46
47 def new(self, prescription):
48 for i in self.plugins:
49 try:
50 if(hasattr(i, "new") and callable(i.new)):
51 if(hasattr(i, "input") and callable(i.input)):
52 i.input(self.input())
53 msg=i.new(prescription)
54 if(msg):
55 QMessageBox.information(None, "Information", msg)
56 except Exception as e:
57 print(e)
58
59 def open(self, prescription):
60 for i in self.plugins:
61 try:
62 if(hasattr(i, "open") and callable(i.open)):
63 if(hasattr(i, "input") and callable(i.input)):
64 i.input(self.input())
65 msg=i.open(prescription)
66 if(msg):
67 QMessageBox.information(None, "Information", msg)
68 except Exception as e:
69 print(e)
70
71 def save(self, prescription):
72 for i in self.plugins:
73 try:
74 if(hasattr(i, "save") and callable(i.save)):
75 if(hasattr(i, "input") and callable(i.input)):
76 i.input(self.input())
77 msg=i.save(prescription)
78 if(msg):
79 QMessageBox.information(None, "Information", msg)
80 except Exception as e:
81 print(e)
82
83 def refresh(self, prescription):
84 for i in self.plugins:
85 try:
86 if(hasattr(i, "refresh") and callable(i.refresh)):
87 if(hasattr(i, "input") and callable(i.input)):
88 i.input(self.input())
89 msg=i.refresh(prescription)
90 if(msg):
91 QMessageBox.information(None, "Information", msg)
92 except Exception as e:
93 print(e)
94
95 def run(self, module, prescription):
96 try:
97 if(hasattr(module, "run") and callable(module.run)):
98 if(hasattr(module, "input") and callable(module.input)):
99 module.input(self.input())
100 if(hasattr(module, "fileopen") and callable(module.fileopen)):
101 module.fileopen(QFileDialog.getOpenFileName()[0])
102 if(hasattr(module, "filesave") and callable(module.filesave)):
103 module.filesave(QFileDialog.getSaveFileName()[0])
104 msg=module.run(prescription)
105 if(msg):
106 QMessageBox.information(None, "Information", msg)
107 except Exception as e:
108 print(e)
109
110 def input(self):
111 try:
112 text, ok=QInputDialog.getText(None, "User input", "Enter text:")
113 if text and ok:
114 return text
115 else:
116 return ""
117 except Exception as e:
118 print(e)