forked from 3liz/qgis_plugin_tools
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathresources.py
397 lines (290 loc) · 11.2 KB
/
resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""Tools to work with resource files."""
import configparser
import importlib.resources
import inspect
import sys
from os.path import abspath, dirname, exists, join, pardir
from pathlib import Path
from typing import TYPE_CHECKING, Dict, Iterator, NamedTuple, Optional
from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog, QWidget
if TYPE_CHECKING:
import os
from typing import Union
__copyright__ = (
"Copyright 2019, 3Liz, 2020-2021 Gispo Ltd, "
"2022-2023 National Land Survey of Finland"
)
__license__ = "GPL version 3"
__email__ = "[email protected]"
__revision__ = "$Format:%H$"
from qgis.core import QgsApplication
PLUGIN_NAME: str = ""
SLUG_NAME: str = ""
def is_submodule() -> bool:
"""Detects if qgis_plugin_tools is used as a submodule
Checks if the second level package name is qgis_plugin_tools
The __name__ is:
- "myplugin.qgis_plugin_tools.tools.resources" when used as a submodule
- "myplugin.dependencies.qgis_plugin_tools.tools.resources" when used as embedded
dependency
- "qgis_plugin_tools.tools.resources" when used as pip installed package
"""
try:
_, second_level_package_name, *_ = __name__.split(".")
except ValueError:
return False
return second_level_package_name == "qgis_plugin_tools"
_IS_SUBMODULE_USAGE = is_submodule()
def _plugin_path_submodule() -> str:
# assume qgis_plugin_tools is inside the plugin package,
# use the path to the top level module name
top_level_package_name = __name__.split(".", maxsplit=1)[0]
module_file = sys.modules[top_level_package_name].__file__
if module_file is not None:
path = str(Path(module_file).parent.resolve())
else:
# maybe possible to have __file__ as none? fall back to default
# structure with qgis_plugin_tools directly under plugin package
path = dirname(dirname(__file__))
path = abspath(abspath(join(path, pardir)))
return path
def _iterate_modules(module_name: str) -> Iterator[str]:
"""Iterates modules bottom up
>>> list(_iterate_modules("plugin.tools.resources"))
[
"plugin.tools.resources",
"plugin.tools",
"plugin"
]
"""
modules = module_name.split(".")
for i in range(len(modules), 0, -1):
yield ".".join(modules[:i])
class IsPluginResult(NamedTuple):
is_plugin: bool
plugin_directory: Optional[str] = None
def __bool__(self) -> bool:
return self.is_plugin
def _is_module_qgis_plugin(module_name: str) -> IsPluginResult:
"""Checks if the module is a QGIS plugin
A plugin module has a classFactory method and a metadata.txt at the package root.
Returns a IsPluginResult thats compares to True when the module given is a plugin.
>>> if is_plugin := _is_module_qgis_plugin('myplugin'):
>>> print(f"Plugin is installed ad {is_plugin.plugin_directory}")
"""
module = sys.modules.get(module_name)
if module is None or not inspect.ismodule(module):
return IsPluginResult(False)
class_factory_function = getattr(module, "classFactory", None)
if class_factory_function is None or not inspect.isfunction(class_factory_function):
return IsPluginResult(False)
try:
source_file = inspect.getsourcefile(module)
except TypeError:
# TypeError is thrown for built-in modules. We are only interested in
# custom modules, so it is safe to ignore the error.
return IsPluginResult(False)
if not source_file:
return IsPluginResult(False)
source_directory = dirname(source_file)
if not exists(join(source_directory, "metadata.txt")):
return IsPluginResult(False)
return IsPluginResult(True, source_directory)
def _plugin_path_dependency() -> str:
"""Get the path to the plugin package folder.
Traverses packages of calling modules bottom up and checks if it is a qgis plugin.
"""
for frame_info in inspect.stack():
caller_module_name: Optional[str] = frame_info.frame.f_globals.get("__name__")
if caller_module_name is None or "qgis_plugin_tools" in caller_module_name:
# We are interested only on calls from outside of qgis_plugin_tools
continue
for module_name in _iterate_modules(caller_module_name):
if is_plugin := _is_module_qgis_plugin(module_name):
assert is_plugin.plugin_directory
return is_plugin.plugin_directory
# fall back to default directory tree
return _plugin_path_submodule()
def plugin_path(*args: str) -> str:
"""Get the path to plugin package folder.
:param args List of path elements e.g. ['img', 'logos', 'image.png']
:type args: str
:return: Absolute path to the resource.
:rtype: str
"""
if _IS_SUBMODULE_USAGE:
path = _plugin_path_submodule()
else:
path = _plugin_path_dependency()
for item in args:
path = abspath(join(path, item))
return path
def root_path(*args: str) -> str:
"""Get the path to plugin root folder.
NOTE: the assumed root is the parent of the plugin package folder.
:param args List of path elements e.g. ['img', 'logos', 'image.png']
:type args: str
:return: Absolute path to the resource.
:rtype: str
"""
return plugin_path(pardir, *args)
def profile_path(*args: str) -> str:
"""
Get the path inside profile folder.
return: Absolute path to the resource.
"""
path = QgsApplication.qgisSettingsDirPath()
for item in args:
path = abspath(join(path, item))
return path
def plugin_name() -> str:
"""Return the plugin name according to metadata.txt but stripped of spaces.
This is because lots of the code assumes plugin_name returns a string without
spaces.
:return: The stripped plugin name.
:rtype: basestring
"""
global PLUGIN_NAME, _IS_SUBMODULE_USAGE
if PLUGIN_NAME != "":
return PLUGIN_NAME
try:
metadata = metadata_config()
name: str = metadata["general"]["name"]
name = name.replace(" ", "").strip()
except KeyError:
name = "test_plugin"
# if qgis plugin tools is run as a dependency, global var cannot be set
# since it might confuse multiple plugins in the same env using this fn
if _IS_SUBMODULE_USAGE:
PLUGIN_NAME = name
return name
def plugin_display_name() -> str:
"""Return the plugin name according to metadata.txt, including spaces,
for displaying to the end user.
:return: The original plugin name.
:rtype: basestring
"""
try:
metadata = metadata_config()
return metadata["general"]["name"]
except KeyError:
return "Test plugin"
def slug_name() -> str:
"""Return project slug name in .qgis-plugin.ci"""
global SLUG_NAME, _IS_SUBMODULE_USAGE
if SLUG_NAME != "":
return SLUG_NAME
try:
metadata = metadata_config()
name: str = metadata["general"]["repository"]
slug = name.split("/")[-1]
except KeyError:
slug = plugin_name()
# if qgis plugin tools is run as a dependency, global var cannot be set
# since it might confuse multiple plugins in the same env using this fn
if _IS_SUBMODULE_USAGE:
SLUG_NAME = slug
return slug
def task_logger_name() -> str:
"""
Returns the name for task logger
"""
return f"{plugin_name()}_task"
def metadata_config() -> configparser.ConfigParser:
"""Get the INI config parser for the metadata file.
:return: The config parser object.
:rtype: ConfigParser
"""
path = plugin_path("metadata.txt")
config = configparser.ConfigParser()
config.read(path)
return config
def qgis_plugin_ci_config() -> Optional[Dict]:
"""
Get configuration of the ci config or None
"""
path_str = root_path(".qgis-plugin-ci")
if not Path(path_str).exists():
path_str = plugin_path(".qgis-plugin-ci")
path = Path(path_str)
if path.exists():
with open(path) as f:
config = {}
for line in f:
parts = line.split(":")
config[parts[0]] = ":".join(parts[1:])
return config
return None
def plugin_test_data_path(*args: str) -> str:
"""Get the path to the plugin test data path.
:param args List of path elements e.g. ['img', 'logos', 'image.png']
:type args: str
:return: Absolute path to the resources folder.
:rtype: str
"""
path = abspath(abspath(join(root_path(), "test", "data")))
if not exists(path):
path = abspath(abspath(join(plugin_path(), "test", "data")))
for item in args:
path = abspath(join(path, item))
return path
def resources_path(*args: str) -> str:
"""Get the path to our resources folder.
:param args List of path elements e.g. ['img', 'logos', 'image.png']
:type args: str
:return: Absolute path to the resources folder.
:rtype: str
"""
path = abspath(abspath(join(plugin_path(), "resources")))
for item in args:
path = abspath(join(path, item))
return path
def qgis_plugin_tools_resources(*args: str) -> str:
"""Get the path within the qgis_plugin_tools submodule"""
return str(Path(__file__, "..", "..", "resources", *args).resolve().absolute())
def load_ui(*args: str) -> QWidget:
"""Get compiled UI file.
:param args List of path elements e.g. ['img', 'logos', 'image.png']
:type args: str
:return: Compiled UI file.
"""
ui_file_path = resources_path("ui", *args)
return load_ui_from_file(ui_file_path)
def load_ui_from_file(ui_file_path: "Union[str, os.PathLike]") -> QWidget:
ui_class, _ = uic.loadUiType(ui_file_path)
return ui_class
def ui_file_dialog(*ui_file_name_parts: str): # noqa ANN201
"""DRY helper for building classes from a .ui file"""
class UiFileDialogClass(QDialog, load_ui(*ui_file_name_parts)): # type: ignore
def __init__(
self,
parent: Optional[QWidget],
) -> None:
super().__init__(parent)
self.setupUi(self) # provided by load_ui FORM_CLASS
return UiFileDialogClass
def package_file(package: importlib.resources.Package, file_name: str) -> Path:
"""
Safely access a file in the package hierarchy. This will
ensure the requested file actually exists on the file system
outside the contextmanager, so that the file can be still
accessed with the path on demand later.
Use like importlib.resources, provide a package (module or string)
and file name/path inside the package.
>>> package_file('myplugin.resources', 'image.svg')
C:/Users/me/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins/myplugin/resources/image.svg
>>> import myplugin.resources
>>> package_file(myplugin.resources, 'image.svg')
C:/Users/me/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins/myplugin/resources/image.svg
"""
with importlib.resources.path(package, file_name) as requested_path:
if not requested_path.is_file():
raise FileNotFoundError(
f"requested file {file_name} not found in {package}"
)
if not requested_path.is_file():
raise FileNotFoundError(
"requested file would be available only as a temporary resource"
)
return requested_path