]>
Softwares of Agnibho - medscript.git/blob - plugin.py
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
, json
9 from PyQt6
. QtWidgets
import QMessageBox
, QMainWindow
, QInputDialog
, QFileDialog
, QVBoxLayout
10 from PyQt6
. QtCore
import QObject
, QThread
, QUrl
, pyqtSignal
, pyqtSlot
11 from PyQt6
. QtWebChannel
import QWebChannel
12 from PyQt6
. QtGui
import QIcon
13 from PyQt6
. QtWebEngineWidgets
import QWebEngineView
15 from config
import config
17 class Plugin ( QObject
):
27 if ( config
[ "enable_plugin" ]):
31 plugin_list
= glob ( os
. path
. join ( config
[ "plugin_directory" ], "*" ))
35 spec
= importlib
. util
. spec_from_file_location ( os
. path
. basename ( i
), os
. path
. join ( i
, "main.py" ))
36 mod
= importlib
. util
. module_from_spec ( spec
)
37 spec
. loader
. exec_module ( mod
)
38 self
. plugins
. append ( mod
)
39 except Exception as e
:
40 logging
. warning ( i
+ ":" + str ( e
))
42 def get_name ( self
, mod
):
45 except Exception as e
:
50 for i
in self
. plugins
:
51 if ( hasattr ( i
, "run" ) and callable ( i
. run
)):
52 cmds
. append ([ i
, self
. get_name ( i
)])
55 def new ( self
, prescription
):
56 for i
in self
. plugins
:
58 if ( hasattr ( i
, "new" ) and callable ( i
. new
)):
59 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
61 prescription_copy
= copy
. deepcopy ( prescription
)
62 message
= i
. new ( prescription_copy
)
63 prescription
. set_data_from_copy ( prescription_copy
)
65 self
. showMessage ( message
)
66 except Exception as e
:
69 def open ( self
, prescription
):
70 for i
in self
. plugins
:
72 if ( hasattr ( i
, "open" ) and callable ( i
. open )):
73 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
75 prescription_copy
= copy
. deepcopy ( prescription
)
76 message
= i
. open ( prescription_copy
)
77 prescription
. set_data_from_copy ( prescription_copy
)
79 self
. showMessage ( message
)
80 except Exception as e
:
83 def save ( self
, prescription
):
84 for i
in self
. plugins
:
86 if ( hasattr ( i
, "save" ) and callable ( i
. save
)):
87 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
89 prescription_copy
= copy
. deepcopy ( prescription
)
90 message
= i
. save ( prescription_copy
)
91 prescription
. set_data_from_copy ( prescription_copy
)
93 self
. showMessage ( message
)
94 except Exception as e
:
97 def refresh ( self
, prescription
):
98 for i
in self
. plugins
:
100 if ( hasattr ( i
, "refresh" ) and callable ( i
. refresh
)):
101 if ( hasattr ( i
, "input" ) and callable ( i
. input )):
102 i
. input ( self
. input ())
103 prescription_copy
= copy
. deepcopy ( prescription
)
104 message
= i
. refresh ( prescription_copy
)
105 prescription
. set_data_from_copy ( prescription_copy
)
107 self
. showMessage ( message
)
108 except Exception as e
:
111 def run ( self
, module
, prescription
):
113 if ( hasattr ( module
, "web" ) and callable ( module
. web
)):
115 self
. webapp
. done
. connect ( lambda : prescription
. set_data_from_copy ( prescription_copy
))
116 self
. webapp
. done
. connect ( lambda : self
. update
. emit ())
117 prescription_copy
= copy
. deepcopy ( prescription
)
118 url
, data
= module
. web ( prescription_copy
)
119 self
. webapp
. load ( module
, QUrl ( url
), prescription_copy
, data
)
121 elif ( hasattr ( module
, "run" ) and callable ( module
. run
)):
122 if ( hasattr ( module
, "confirm" ) and module
. confirm
):
123 if ( QMessageBox
. StandardButton
. Yes
!= QMessageBox
. question ( None , "Confirm" , module
. confirm
)):
125 if ( hasattr ( module
, "input" ) and callable ( module
. input )):
126 module
. input ( self
. input ())
127 if ( hasattr ( module
, "fileopen" ) and callable ( module
. fileopen
)):
128 module
. fileopen ( QFileDialog
. getOpenFileName ()[ 0 ])
129 if ( hasattr ( module
, "filesave" ) and callable ( module
. filesave
)):
130 module
. filesave ( QFileDialog
. getSaveFileName ()[ 0 ])
131 if ( hasattr ( module
, "background" ) and module
. background
):
132 self
. showMessage ( "Module " + module
.__ name
__ + " will run in background." )
133 self
. workers
. append ( Worker ( module
. run
, prescription
))
134 index
= len ( self
. workers
)- 1
135 self
. workers
[ index
]. setIndex ( index
)
136 self
. workers
[ index
]. pluginComplete
. connect ( self
. showMessage
)
137 self
. workers
[ index
]. start ()
139 prescription_copy
= copy
. deepcopy ( prescription
)
140 message
= module
. run ( prescription_copy
)
141 prescription
. set_data_from_copy ( prescription_copy
)
143 self
. showMessage ( message
)
144 except Exception as e
:
149 text
, ok
= QInputDialog
. getText ( None , "User input" , "Enter text:" )
154 except Exception as e
:
157 def showMessage ( self
, message
, index
= None ):
158 QMessageBox
. information ( None , "Information" , message
)
159 if index
is not None :
160 self
. workers
[ index
]= None
162 class WebApp ( QMainWindow
):
166 def __init__ ( self
, * args
, ** kwargs
):
167 super () .__ init
__ (* args
, ** kwargs
)
169 self
. setWindowTitle ( "WebApp Plugin" )
170 self
. setGeometry ( 100 , 100 , 400 , 400 )
171 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
173 self
. webview
= QWebEngineView ()
174 self
. setCentralWidget ( self
. webview
)
176 def load ( self
, module
, url
, prescription
, data
):
178 self
. webview
. load ( url
)
179 self
. channel
= QWebChannel ()
180 self
. js
= JS ( module
, prescription
, data
)
181 self
. channel
. registerObject ( "js" , self
. js
)
182 self
. webview
. page (). setWebChannel ( self
. channel
)
183 self
. js
. done
. connect ( lambda : self
. done
. emit ())
184 self
. js
. hide
. connect ( lambda : self
. close ())
191 def __init__ ( self
, module
, prescription
, data
):
194 self
. prescription
= prescription
198 def run ( self
, result
):
200 prescription_copy
= copy
. deepcopy ( self
. prescription
)
201 message
= self
. module
. run ( prescription_copy
, result
)
202 self
. prescription
. set_data_from_copy ( prescription_copy
)
204 QMessageBox
. information ( None , "Information" , message
)
206 except Exception as e
:
207 logging
. error ( self
. module
)
210 @pyqtSlot ( result
= str )
218 class Worker ( QThread
):
220 pluginComplete
= pyqtSignal ( str , int )
225 def __init__ ( self
, function
, prescription
):
227 self
. function
= function
228 self
. prescription
= prescription
230 def setIndex ( self
, index
):
235 prescription_copy
= copy
. deepcopy ( self
. prescription
)
236 message
= self
. function ( prescription_copy
)
237 self
. pluginComplete
. emit ( message
, self
. index
)
238 except Exception as e
: