python-module-utils 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- module_utils/__init__.py +42 -0
- module_utils/_importing.py +289 -0
- python_module_utils-1.0.0.dist-info/METADATA +16 -0
- python_module_utils-1.0.0.dist-info/RECORD +7 -0
- python_module_utils-1.0.0.dist-info/WHEEL +5 -0
- python_module_utils-1.0.0.dist-info/licenses/LICENCE +13 -0
- python_module_utils-1.0.0.dist-info/top_level.txt +1 -0
module_utils/__init__.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
__all__ = (
|
|
2
|
+
"ImportResultMode",
|
|
3
|
+
"ModulePlaceholder",
|
|
4
|
+
"extended_import",
|
|
5
|
+
"extended_from_import",
|
|
6
|
+
"optional_import",
|
|
7
|
+
"optional_from_import"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
from ._importing import *
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
#Copyright © 2026 Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
|
|
31
|
+
#
|
|
32
|
+
#
|
|
33
|
+
#This program is free software: you can redistribute it and/or modify it under
|
|
34
|
+
#the terms of the GNU General Public License as published by the Free Software
|
|
35
|
+
#Foundation, either version 3 of the License, or (at your option) any later
|
|
36
|
+
#version.
|
|
37
|
+
#This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
38
|
+
#WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
39
|
+
#PARTICULAR PURPOSE.
|
|
40
|
+
#See the GNU General Public License for more details.
|
|
41
|
+
#You should have received a copy of the GNU General Public License along with
|
|
42
|
+
#this program. If not, see <https://www.gnu.org/licenses/>.
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
__all__ = (
|
|
2
|
+
"ImportResultMode",
|
|
3
|
+
"ModulePlaceholder",
|
|
4
|
+
"extended_import",
|
|
5
|
+
"extended_from_import",
|
|
6
|
+
"optional_import",
|
|
7
|
+
"optional_from_import"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
import sys
|
|
14
|
+
import importlib
|
|
15
|
+
import enum
|
|
16
|
+
import itertools
|
|
17
|
+
from types import SimpleNamespace
|
|
18
|
+
from functools import wraps
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ImportResultMode(enum.StrEnum):
|
|
28
|
+
OBJECTS_STATUSES__FLAT_ROWS_TUPLE = enum.auto()
|
|
29
|
+
OBJECTS_STATUSES__FLAT_ROWS_LIST = enum.auto()
|
|
30
|
+
OBJECTS_STATUSES__FLAT_COLUMNS_TUPLE = enum.auto()
|
|
31
|
+
OBJECTS_STATUSES__FLAT_COLUMNS_LIST = enum.auto()
|
|
32
|
+
OBJECTS_STATUSES__ROWS_TUPLE = enum.auto()
|
|
33
|
+
OBJECTS_STATUSES__ROWS_LIST = enum.auto()
|
|
34
|
+
OBJECTS_STATUSES__COLUMNS_TUPLE = enum.auto()
|
|
35
|
+
OBJECTS_STATUSES__COLUMNS_LIST = enum.auto()
|
|
36
|
+
OBJECTS_ONLY__TUPLE_OR_OBJECT = enum.auto()
|
|
37
|
+
OBJECTS_ONLY__LIST_OR_OBJECT = enum.auto()
|
|
38
|
+
OBJECTS_ONLY__TUPLE = enum.auto()
|
|
39
|
+
OBJECTS_ONLY__LIST = enum.auto()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ModulePlaceholder(SimpleNamespace):
|
|
45
|
+
def __getattr__(self, name):
|
|
46
|
+
#don’t create attributes with system-defined (“dunder”) names
|
|
47
|
+
if len(name) >= 5 and name.startswith("__") and name.endswith("__"):
|
|
48
|
+
raise AttributeError(name, self)
|
|
49
|
+
|
|
50
|
+
new_attribute = ModulePlaceholder()
|
|
51
|
+
setattr(self, name, new_attribute)
|
|
52
|
+
|
|
53
|
+
return new_attribute
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def __bool__(self):
|
|
59
|
+
return False
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def extended_import(modules, *, ignored_exceptions_on_import=None, import_object_on_failure=None, get_top_level_modules=False, exception_on_failure_to_get_top_level_modules=True, result_mode=ImportResultMode.OBJECTS_STATUSES__FLAT_ROWS_TUPLE):
|
|
65
|
+
#prepare parameters
|
|
66
|
+
if isinstance(modules, str):
|
|
67
|
+
modules = (modules, )
|
|
68
|
+
if ignored_exceptions_on_import is None:
|
|
69
|
+
ignored_exceptions_on_import = ()
|
|
70
|
+
elif isinstance(ignored_exceptions_on_import, type) and issubclass(ignored_exceptions_on_import, BaseException):
|
|
71
|
+
ignored_exceptions_on_import = (ignored_exceptions_on_import, )
|
|
72
|
+
elif not isinstance(ignored_exceptions_on_import, tuple):
|
|
73
|
+
ignored_exceptions_on_import = tuple(ignored_exceptions_on_import)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
import_objects = []
|
|
77
|
+
import_statuses = []
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
#import the modules
|
|
81
|
+
for m in modules:
|
|
82
|
+
#disallow relative module names
|
|
83
|
+
if m.startswith("."):
|
|
84
|
+
raise ValueError("Relative module names are not allowed.")
|
|
85
|
+
|
|
86
|
+
if get_top_level_modules:
|
|
87
|
+
try:
|
|
88
|
+
module_object = importlib.__import__(m)
|
|
89
|
+
except ignored_exceptions_on_import:
|
|
90
|
+
top_level_module = m.partition(".")[0]
|
|
91
|
+
|
|
92
|
+
top_level_module_imported = False
|
|
93
|
+
if m != top_level_module:
|
|
94
|
+
#the module is not a top-level module:
|
|
95
|
+
|
|
96
|
+
#explicitly import the top-level module
|
|
97
|
+
try:
|
|
98
|
+
module_object = importlib.__import__(top_level_module)
|
|
99
|
+
except Exception as e:
|
|
100
|
+
if exception_on_failure_to_get_top_level_modules:
|
|
101
|
+
raise ImportError(f"Failed to explicitly import the top-level module `{top_level_module}`, which was tried as a consequence of the “ignored” failure to import the module `{m}`.") from e
|
|
102
|
+
else:
|
|
103
|
+
top_level_module_imported = True
|
|
104
|
+
|
|
105
|
+
if not top_level_module_imported:
|
|
106
|
+
if callable(import_object_on_failure):
|
|
107
|
+
module_object = import_object_on_failure()
|
|
108
|
+
else:
|
|
109
|
+
module_object = import_object_on_failure
|
|
110
|
+
|
|
111
|
+
success = False
|
|
112
|
+
else:
|
|
113
|
+
success = True
|
|
114
|
+
else:
|
|
115
|
+
try:
|
|
116
|
+
module_object = importlib.import_module(m)
|
|
117
|
+
except ignored_exceptions_on_import:
|
|
118
|
+
if callable(import_object_on_failure):
|
|
119
|
+
module_object = import_object_on_failure()
|
|
120
|
+
else:
|
|
121
|
+
module_object = import_object_on_failure
|
|
122
|
+
|
|
123
|
+
success = False
|
|
124
|
+
else:
|
|
125
|
+
success = True
|
|
126
|
+
|
|
127
|
+
import_objects.append(module_object)
|
|
128
|
+
import_statuses.append(success)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
return _render_import_result(import_objects, import_statuses, result_mode=result_mode)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def extended_from_import(module, attributes, *, package=None, ignored_exceptions_on_import=None, ignore_non_existent_attributes_on_import=False, import_object_on_failure=None, result_mode=ImportResultMode.OBJECTS_STATUSES__FLAT_ROWS_TUPLE):
|
|
137
|
+
#prepare parameters
|
|
138
|
+
if isinstance(attributes, str):
|
|
139
|
+
attributes = (attributes, )
|
|
140
|
+
if ignored_exceptions_on_import is None:
|
|
141
|
+
ignored_exceptions_on_import = ()
|
|
142
|
+
elif isinstance(ignored_exceptions_on_import, type) and issubclass(ignored_exceptions_on_import, BaseException):
|
|
143
|
+
ignored_exceptions_on_import = (ignored_exceptions_on_import, )
|
|
144
|
+
elif not isinstance(ignored_exceptions_on_import, tuple):
|
|
145
|
+
ignored_exceptions_on_import = tuple(ignored_exceptions_on_import)
|
|
146
|
+
|
|
147
|
+
#validate parameters
|
|
148
|
+
if len(attributes) == 0:
|
|
149
|
+
raise ValueError("Not at least one attribute name given.")
|
|
150
|
+
if package is None:
|
|
151
|
+
if module.startswith("."):
|
|
152
|
+
raise ValueError("Relative module name but no package name given.")
|
|
153
|
+
else:
|
|
154
|
+
if not module.startswith("."):
|
|
155
|
+
raise ValueError("Absolute module name but a package name given.")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
#resolve a relative to an absolute module name
|
|
159
|
+
module = importlib.util.resolve_name(module, package)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
import_objects = []
|
|
163
|
+
import_statuses = []
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
#import the module
|
|
167
|
+
try:
|
|
168
|
+
module_object = importlib.__import__(module, fromlist=attributes)
|
|
169
|
+
except ignored_exceptions_on_import:
|
|
170
|
+
if callable(import_object_on_failure):
|
|
171
|
+
import_objects.extend([import_object_on_failure() for _ in attributes])
|
|
172
|
+
else:
|
|
173
|
+
import_objects.extend( [import_object_on_failure] * len(attributes) )
|
|
174
|
+
|
|
175
|
+
import_statuses.extend( [False] * len(attributes) )
|
|
176
|
+
else:
|
|
177
|
+
#get the attributes
|
|
178
|
+
for a in attributes:
|
|
179
|
+
#disallow importing the `*`-special-attribute
|
|
180
|
+
if a == "*":
|
|
181
|
+
raise ValueError("Importing the `*`-special-attribute is not allowed.")
|
|
182
|
+
|
|
183
|
+
try:
|
|
184
|
+
attribute_object = getattr(module_object, a)
|
|
185
|
+
except AttributeError:
|
|
186
|
+
if not ignore_non_existent_attributes_on_import:
|
|
187
|
+
raise
|
|
188
|
+
|
|
189
|
+
if callable(import_object_on_failure):
|
|
190
|
+
attribute_object = import_object_on_failure()
|
|
191
|
+
else:
|
|
192
|
+
attribute_object = import_object_on_failure
|
|
193
|
+
|
|
194
|
+
success = False
|
|
195
|
+
else:
|
|
196
|
+
success = True
|
|
197
|
+
|
|
198
|
+
import_objects.append(attribute_object)
|
|
199
|
+
import_statuses.append(success)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
return _render_import_result(import_objects, import_statuses, result_mode=result_mode)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@wraps(extended_import)
|
|
208
|
+
def optional_import(*args, ignored_exceptions_on_import=(ImportError, ), import_object_on_failure=ModulePlaceholder, exception_on_failure_to_get_top_level_modules=False, **kwargs):
|
|
209
|
+
return extended_import(*args, ignored_exceptions_on_import=ignored_exceptions_on_import, import_object_on_failure=import_object_on_failure, exception_on_failure_to_get_top_level_modules=exception_on_failure_to_get_top_level_modules, **kwargs)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@wraps(extended_from_import)
|
|
215
|
+
def optional_from_import(*args, ignored_exceptions_on_import=(ImportError, ), ignore_non_existent_attributes_on_import=True, import_object_on_failure=ModulePlaceholder, **kwargs):
|
|
216
|
+
return extended_from_import(*args, ignored_exceptions_on_import=ignored_exceptions_on_import, ignore_non_existent_attributes_on_import=ignore_non_existent_attributes_on_import, import_object_on_failure=import_object_on_failure, **kwargs)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _render_import_result(import_objects, import_statuses, *, result_mode):
|
|
222
|
+
match result_mode:
|
|
223
|
+
case ImportResultMode.OBJECTS_STATUSES__FLAT_ROWS_TUPLE:
|
|
224
|
+
return tuple(itertools.chain.from_iterable(zip(import_objects, import_statuses, strict=True)))
|
|
225
|
+
case ImportResultMode.OBJECTS_STATUSES__FLAT_ROWS_LIST:
|
|
226
|
+
return list(itertools.chain.from_iterable(zip(import_objects, import_statuses, strict=True)))
|
|
227
|
+
case ImportResultMode.OBJECTS_STATUSES__FLAT_COLUMNS_TUPLE:
|
|
228
|
+
return tuple(import_objects + import_statuses)
|
|
229
|
+
case ImportResultMode.OBJECTS_STATUSES__FLAT_COLUMNS_LIST:
|
|
230
|
+
return import_objects + import_statuses
|
|
231
|
+
case ImportResultMode.OBJECTS_STATUSES__ROWS_TUPLE:
|
|
232
|
+
return tuple(zip(import_objects, import_statuses, strict=True))
|
|
233
|
+
case ImportResultMode.OBJECTS_STATUSES__ROWS_LIST:
|
|
234
|
+
return [ list(row) for row in zip(import_objects, import_statuses, strict=True) ]
|
|
235
|
+
case ImportResultMode.OBJECTS_STATUSES__COLUMNS_TUPLE:
|
|
236
|
+
return ( tuple(import_objects), tuple(import_statuses) )
|
|
237
|
+
case ImportResultMode.OBJECTS_STATUSES__COLUMNS_LIST:
|
|
238
|
+
return [import_objects, import_statuses]
|
|
239
|
+
case ImportResultMode.OBJECTS_ONLY__TUPLE_OR_OBJECT:
|
|
240
|
+
match len(import_objects):
|
|
241
|
+
case 0:
|
|
242
|
+
return None
|
|
243
|
+
case 1:
|
|
244
|
+
return import_objects[0]
|
|
245
|
+
case _:
|
|
246
|
+
return tuple(import_objects)
|
|
247
|
+
case ImportResultMode.OBJECTS_ONLY__LIST_OR_OBJECT:
|
|
248
|
+
match len(import_objects):
|
|
249
|
+
case 0:
|
|
250
|
+
return None
|
|
251
|
+
case 1:
|
|
252
|
+
return import_objects[0]
|
|
253
|
+
case _:
|
|
254
|
+
return import_objects
|
|
255
|
+
case ImportResultMode.OBJECTS_ONLY__TUPLE:
|
|
256
|
+
return tuple(import_objects)
|
|
257
|
+
case ImportResultMode.OBJECTS_ONLY__LIST:
|
|
258
|
+
return import_objects
|
|
259
|
+
case _:
|
|
260
|
+
raise NotImplementedError(f"Unknown return mode `{result_mode.name if isinstance(result_mode, enum.Enum) else result_mode}`.")
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
#Copyright © 2026 Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
|
|
278
|
+
#
|
|
279
|
+
#
|
|
280
|
+
#This program is free software: you can redistribute it and/or modify it under
|
|
281
|
+
#the terms of the GNU General Public License as published by the Free Software
|
|
282
|
+
#Foundation, either version 3 of the License, or (at your option) any later
|
|
283
|
+
#version.
|
|
284
|
+
#This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
285
|
+
#WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
286
|
+
#PARTICULAR PURPOSE.
|
|
287
|
+
#See the GNU General Public License for more details.
|
|
288
|
+
#You should have received a copy of the GNU General Public License along with
|
|
289
|
+
#this program. If not, see <https://www.gnu.org/licenses/>.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-module-utils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Utilities for handling Python modules.
|
|
5
|
+
Author-email: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
|
|
6
|
+
Maintainer-email: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
|
|
7
|
+
Project-URL: website, https://gitlab.com/calestyo/python-module-utils
|
|
8
|
+
Keywords: modules,module,import,importing
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Natural Language :: English
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Requires-Python: >=3.11
|
|
15
|
+
License-File: LICENCE
|
|
16
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
module_utils/__init__.py,sha256=Kvp7-rDjdscPD7mErfeEg1cMa5WNVRkoE9DcIrE_de4,966
|
|
2
|
+
module_utils/_importing.py,sha256=DqPCM4v2IqW84fUQiG6Wsr8Yzeu4cX0rG3LK9ZuOtgw,11000
|
|
3
|
+
python_module_utils-1.0.0.dist-info/licenses/LICENCE,sha256=PU9RjoB0SBo6BuxWcNT0qhUaxxzhDBS9k156jzTDhNE,691
|
|
4
|
+
python_module_utils-1.0.0.dist-info/METADATA,sha256=UelOnWJuSsBrZnYcYxaYhyPfj2EaGVPqlnDGQY14ZHw,695
|
|
5
|
+
python_module_utils-1.0.0.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
|
|
6
|
+
python_module_utils-1.0.0.dist-info/top_level.txt,sha256=_7C3dmxpcDNCi2vJovqg0nbJgyUUhDO7xWyYbx36H84,13
|
|
7
|
+
python_module_utils-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright © 2026 Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
5
|
+
the terms of the GNU General Public License as published by the Free Software
|
|
6
|
+
Foundation, either version 3 of the License, or (at your option) any later
|
|
7
|
+
version.
|
|
8
|
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
9
|
+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
10
|
+
PARTICULAR PURPOSE.
|
|
11
|
+
See the GNU General Public License for more details.
|
|
12
|
+
You should have received a copy of the GNU General Public License along with
|
|
13
|
+
this program. If not, see <https://www.gnu.org/licenses/>.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module_utils
|