]> Softwares of Agnibho - medscript.git/blob - plugin.py
Update instance before plugin call
[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
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 spec=importlib.util.spec_from_file_location(os.path.basename(i), os.path.join(i, "main.py"))
27 mod=importlib.util.module_from_spec(spec)
28 spec.loader.exec_module(mod)
29 self.plugins.append(mod)
30 except Exception as e:
31 print(i, ":", e)
32
33 def get_name(self, mod):
34 try:
35 return(mod.name)
36 except Exception as e:
37 return(mod.__name__)
38
39 def commands(self):
40 cmds=[]
41 for i in self.plugins:
42 if(hasattr(i, "run") and callable(i.run)):
43 cmds.append([i, self.get_name(i)])
44 return(cmds)
45
46 def new(self, prescription):
47 for i in self.plugins:
48 try:
49 if(hasattr(i, "new") and callable(i.new)):
50 msg=i.new(prescription)
51 if(msg):
52 QMessageBox.information(None, "Information", msg)
53 except Exception as e:
54 print(e)
55
56 def open(self, prescription):
57 for i in self.plugins:
58 try:
59 if(hasattr(i, "open") and callable(i.open)):
60 msg=i.open(prescription)
61 if(msg):
62 QMessageBox.information(None, "Information", msg)
63 except Exception as e:
64 print(e)
65
66 def save(self, prescription):
67 for i in self.plugins:
68 try:
69 if(hasattr(i, "save") and callable(i.save)):
70 msg=i.save(prescription)
71 if(msg):
72 QMessageBox.information(None, "Information", msg)
73 except Exception as e:
74 print(e)
75
76 def refresh(self, prescription):
77 for i in self.plugins:
78 try:
79 if(hasattr(i, "refresh") and callable(i.refresh)):
80 msg=i.refresh(prescription)
81 if(msg):
82 QMessageBox.information(None, "Information", msg)
83 except Exception as e:
84 print(e)
85
86 def run(self, module, prescription):
87 try:
88 if(hasattr(module, "run") and callable(module.run)):
89 msg=module.run(prescription)
90 if(msg):
91 QMessageBox.information(None, "Information", msg)
92 except Exception as e:
93 print(e)