20
20
class Plugin :
21
21
"""QGIS Plugin Implementation."""
22
22
23
- def __init__ (self , iface ):
23
+ def __init__ (self , iface , plugin_path = None ):
24
24
self .iface = iface
25
25
self ._defered_packages = []
26
26
self .settings = QgsSettings ()
27
27
self .settings .beginGroup ("QPIP" )
28
28
29
- self .plugins_path = os .path .join (
30
- QgsApplication .qgisSettingsDirPath (), "python" , "plugins"
31
- )
29
+ if plugin_path is None :
30
+ self .plugins_path = os .path .join (
31
+ QgsApplication .qgisSettingsDirPath (), "python" , "plugins"
32
+ )
33
+ else :
34
+ self .plugins_path = plugin_path
32
35
self .prefix_path = os .path .join (
33
36
QgsApplication .qgisSettingsDirPath ().replace ("/" , os .path .sep ),
34
37
"python" ,
@@ -70,9 +73,10 @@ def initGui(self):
70
73
def initComplete (self ):
71
74
if self ._defered_packages :
72
75
log (f"Initialization complete. Loading deferred packages" )
73
- self .check_deps_and_prompt_install (
74
- additional_plugins = self ._defered_packages
75
- )
76
+ dialog , run_gui = self .check_deps (additional_plugins = self ._defered_packages )
77
+ if run_gui :
78
+ self .promt_install (dialog )
79
+ self .save_settings (dialog )
76
80
self .start_packages (self ._defered_packages )
77
81
self ._defered_packages = []
78
82
@@ -91,7 +95,9 @@ def unload(self):
91
95
os .environ ["PYTHONPATH" ] = os .environ ["PYTHONPATH" ].replace (
92
96
self .bin_path + os .pathsep , ""
93
97
)
94
- os .environ ["PATH" ] = os .environ ["PATH" ].replace (self .bin_path + os .pathsep , "" )
98
+ os .environ ["PATH" ] = os .environ ["PATH" ].replace (
99
+ self .bin_path + os .pathsep , ""
100
+ )
95
101
96
102
def patched_load_plugin (self , packageName ):
97
103
"""
@@ -118,14 +124,21 @@ def patched_load_plugin(self, packageName):
118
124
return self ._original_loadPlugin (packageName )
119
125
else :
120
126
log (f"Check on install enabled, we check { packageName } ." )
121
- self .check_deps_and_prompt_install (additional_plugins = [packageName ])
127
+ dialog , run_gui = self .check_deps (additional_plugins = [packageName ])
128
+ if run_gui :
129
+ self .promt_install (dialog )
130
+ self .save_settings (dialog )
122
131
self .start_packages ([packageName ])
123
132
return True
124
133
125
- def check_deps_and_prompt_install (self , additional_plugins = [], force_gui = False ) :
134
+ def check_deps (self , additional_plugins = []) -> MainDialog | bool :
126
135
"""
127
136
This checks dependencies for installed plugins and to-be installed plugins. If
128
137
anything is missing, shows a GUI to install them.
138
+
139
+ The function returns:
140
+ - MainDialog, the QDialog object (without opening it)
141
+ - A bool if the dialog needs to be opened or not
129
142
"""
130
143
131
144
plugin_names = [* qgis .utils .active_plugins , * additional_plugins ]
@@ -165,31 +178,34 @@ def check_deps_and_prompt_install(self, additional_plugins=[], force_gui=False):
165
178
req = Req (plugin_name , str (requirement ), error )
166
179
libs [requirement .key ].name = requirement .key
167
180
libs [requirement .key ].required_by .append (req )
181
+ dialog = MainDialog (
182
+ libs .values (), self ._check_on_startup (), self ._check_on_install ()
183
+ )
184
+ return dialog , needs_gui
185
+
186
+ def promt_install (self , dialog : MainDialog ):
187
+ """Promts the install dialog and ask the user what to install"""
188
+ if dialog .exec_ ():
189
+ reqs_to_uninstall = dialog .reqs_to_uninstall
190
+ if reqs_to_uninstall :
191
+ log (f"Will uninstall selected dependencies : { reqs_to_uninstall } " )
192
+ self .pip_uninstall_reqs (reqs_to_uninstall )
193
+
194
+ reqs_to_install = dialog .reqs_to_install
195
+ if reqs_to_install :
196
+ log (f"Will install selected dependencies : { reqs_to_install } " )
197
+ self .pip_install_reqs (reqs_to_install )
198
+
199
+ def save_settings (self , dialog ):
200
+ """Stores the settings values"""
201
+ sys .path_importer_cache .clear ()
168
202
169
- if force_gui or needs_gui :
170
- dialog = MainDialog (
171
- libs .values (), self ._check_on_startup (), self ._check_on_install ()
172
- )
173
- if dialog .exec_ ():
174
- reqs_to_uninstall = dialog .reqs_to_uninstall
175
- if reqs_to_uninstall :
176
- log (f"Will uninstall selected dependencies : { reqs_to_uninstall } " )
177
- self .pip_uninstall_reqs (reqs_to_uninstall )
178
-
179
- reqs_to_install = dialog .reqs_to_install
180
- if reqs_to_install :
181
- log (f"Will install selected dependencies : { reqs_to_install } " )
182
- self .pip_install_reqs (reqs_to_install )
183
-
184
- sys .path_importer_cache .clear ()
185
-
186
- # Save these even if the dialog was closed
187
- self .settings .setValue (
188
- "check_on_startup" , "yes" if dialog .check_on_startup else "no"
189
- )
190
- self .settings .setValue (
191
- "check_on_install" , "yes" if dialog .check_on_install else "no"
192
- )
203
+ self .settings .setValue (
204
+ "check_on_startup" , "yes" if dialog .check_on_startup else "no"
205
+ )
206
+ self .settings .setValue (
207
+ "check_on_install" , "yes" if dialog .check_on_install else "no"
208
+ )
193
209
194
210
def start_packages (self , packageNames ):
195
211
"""
@@ -249,7 +265,9 @@ def pip_install_reqs(self, reqs_to_install):
249
265
)
250
266
251
267
def check (self ):
252
- self .check_deps_and_prompt_install (force_gui = True )
268
+ dialog , _ = self .check_deps ()
269
+ self .promt_install (dialog )
270
+ self .save_settings (dialog )
253
271
254
272
def show_folder (self ):
255
273
if platform .system () == "Windows" :
0 commit comments