]> Softwares of Agnibho - medscript.git/commitdiff
Enabled running plugins in the background.
authorAgnibho Mondal <mondal@agnibho.com>
Sat, 28 Oct 2023 22:56:45 +0000 (04:26 +0530)
committerAgnibho Mondal <mondal@agnibho.com>
Sat, 28 Oct 2023 22:56:45 +0000 (04:26 +0530)
README
plugin.py

diff --git a/README b/README
index 7857efd85b87de4ad0d3abcf87cffc5bcd85067d..d75113a5fdaeb808ac23d2a0af62e0c11831e434 100644 (file)
--- a/README
+++ b/README
@@ -374,12 +374,17 @@ following functions:
     def filesave(string path)
         Get file to save. Only called before "run" function.
 
+Plugin may run in the background by defining a global variable `background`
+with the value `True`. This may be useful for long running tasks without
+freezing the program. This is only available for the "run" function.
+
 The plugin may implement one or more functions mentioned above, it is not
 necessary to implement all.
 
 An example plugin, that modifies the name of the patient, may be as follows:
 
     name="Change Name"
+    background=False
     text=""
 
     def run(prescription):
index da9552bc0cbd060d3801ae63f7a04a7c8219aefd..ceba95ab6fe763f43b80feecc21aefd1a410a96d 100644 (file)
--- a/plugin.py
+++ b/plugin.py
@@ -5,7 +5,7 @@
 # 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.
 # You should have received a copy of the GNU General Public License along with MedScript. If not, see <https://www.gnu.org/licenses/>.
 
-import os, importlib
+import os, importlib, threading
 from PyQt6.QtWidgets import QMessageBox, QInputDialog, QFileDialog
 from glob import glob
 from config import config
@@ -37,6 +37,11 @@ class Plugin():
             except Exception as e:
                 return(mod.__name__)
 
+    def background(self, function, prescription):
+        msg=function(prescription)
+        if(msg):
+            QMessageBox.information(None, "Information", msg)
+
     def commands(self):
         cmds=[]
         for i in self.plugins:
@@ -101,9 +106,13 @@ class Plugin():
                     module.fileopen(QFileDialog.getOpenFileName()[0])
                 if(hasattr(module, "filesave") and callable(module.filesave)):
                     module.filesave(QFileDialog.getSaveFileName()[0])
-                msg=module.run(prescription)
-                if(msg):
-                    QMessageBox.information(None, "Information", msg)
+                if(module.background):
+                    QMessageBox.information(None, "Information", "Module "+module.__name__+" will run in background.")
+                    threading.Thread(target=self.background, args=[module.run, prescription]).start()
+                else:
+                    msg=module.run(prescription)
+                    if(msg):
+                        QMessageBox.information(None, "Information", msg)
         except Exception as e:
             print(e)