From b7fb7501c8c54edc67ade72bb3685118bd87e6c3 Mon Sep 17 00:00:00 2001 From: Agnibho Mondal Date: Sun, 29 Oct 2023 04:26:45 +0530 Subject: [PATCH] Enabled running plugins in the background. --- README | 5 +++++ plugin.py | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README b/README index 7857efd..d75113a 100644 --- 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): diff --git a/plugin.py b/plugin.py index da9552b..ceba95a 100644 --- 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 . -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) -- 2.39.5