]>
Softwares of Agnibho - medscript.git/blob - plugin.py
a45b801c7c95f1bfb1c79ea5d84da4c2eb794615
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/>.
8 import logging
, os
, importlib
, copy
9 from PyQt6
. QtWidgets
import QMessageBox
, QInputDialog
, QFileDialog
10 from PyQt6
. QtCore
import QThread
, pyqtSignal
12 from config
import config
21 if ( config
[ "enable_plugin" ]):
25 plugin_list
= glob ( os
. path
. join ( config
[ "plugin_directory" ], "*" ))
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
)
36 def get_name ( self
, mod
):
39 except Exception as e
:
44 for i
in self
. plugins
:
45 if ( hasattr ( i
, "run" ) and callable ( i
. run
)):
46 cmds
. append ([ i
, self
. get_name ( i
)])
49 def new ( self
, prescription
):
50 for i
in self
. plugins
:
52 if ( hasattr ( i
, "new" ) and callable ( i
. new
)):
53 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
55 message
= i
. new ( prescription
)
57 self
. showMessage ( message
)
58 except Exception as e
:
61 def open ( self
, prescription
):
62 for i
in self
. plugins
:
64 if ( hasattr ( i
, "open" ) and callable ( i
. open )):
65 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
67 message
= i
. open ( prescription
)
69 self
. showMessage ( message
)
70 except Exception as e
:
73 def save ( self
, prescription
):
74 for i
in self
. plugins
:
76 if ( hasattr ( i
, "save" ) and callable ( i
. save
)):
77 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
79 message
= i
. save ( prescription
)
81 self
. showMessage ( message
)
82 except Exception as e
:
85 def refresh ( self
, prescription
):
86 for i
in self
. plugins
:
88 if ( hasattr ( i
, "refresh" ) and callable ( i
. refresh
)):
89 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
91 message
= i
. refresh ( prescription
)
93 self
. showMessage ( message
)
94 except Exception as e
:
97 def run ( self
, module
, prescription
):
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 ()
114 message
= module
. run ( prescription
)
116 self
. showMessage ( message
)
117 except Exception as e
:
122 text
, ok
= QInputDialog
. getText ( None , "User input" , "Enter text:" )
127 except Exception as e
:
130 def showMessage ( self
, message
, index
= None ):
131 QMessageBox
. information ( None , "Information" , message
)
132 if index
is not None :
133 self
. workers
[ index
]= None
135 class Worker ( QThread
):
137 pluginComplete
= pyqtSignal ( str , int )
142 def __init__ ( self
, function
, prescription
):
144 self
. function
= function
145 self
. prescription
= prescription
147 def setIndex ( self
, index
):
151 prescription_copy
= copy
. deepcopy ( self
. prescription
)
152 message
= self
. function ( prescription_copy
)
153 self
. pluginComplete
. emit ( message
, self
. index
)