This repository was archived by the owner on Feb 3, 2019. It is now read-only.
forked from smarie/python-mini-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_init.py
More file actions
74 lines (57 loc) · 2.44 KB
/
Copy pathutils_init.py
File metadata and controls
74 lines (57 loc) · 2.44 KB
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
def __is_defined_in_submodule(submodule_name_prefix, x):
"""
Utility method to return True if x is not a module and its module name starts with submodule_name_prefix
e.g. 'autoclass.autohash_'.
:param submodule_name_prefix:
:param x:
:return:
"""
# if _inspect.ismodule(x):
# return False
# else:
from inspect import getmodule
m = getmodule(x)
if m is None:
return True
elif hasattr(m, '__name__') and m.__name__.startswith(submodule_name_prefix):
return True
else:
return False
def __get_submodule_symbols(submodule_full_name, submodule):
from inspect import getmembers, ismodule
all_ = []
for x_name, symbol in getmembers(submodule):
if not x_name.startswith('_'):
if __is_defined_in_submodule(submodule_full_name, symbol):
# print('{} is exported'.format(x_name))
if ismodule(symbol):
all_ = all_ + __get_submodule_symbols(submodule_full_name + '.' + x_name, symbol)
else:
all_.append(x_name)
return all_
def __get_all_submodules_symbols(pkg_name, submodules_to_export):
"""
Generates the list of symbol names that can be used in the `__all__` variable in init.py
The list is created from a list of submodules.
All symbols in these submodules that are not private and that are actually defined in there, get in the list.
The submodules themselves end up in the list.
Note that this function should only be used if you also actually import those symbols in the init.py, so that they
are actually visible at package root level.
:param submodules_to_export: a list of submodule names to export
:return:
"""
from copy import copy
from importlib import import_module
# first create a copy of the submodules list
all_ = copy(submodules_to_export)
# then for each submodule add the symbols that are declared in this submodule
for submodule in submodules_to_export:
submodule_full_name = pkg_name + '.' + submodule
imported_module = import_module(submodule_full_name)
# print(imported_module.__name__)
all_ = all_ + __get_submodule_symbols(submodule_full_name, imported_module)
return all_
def __remove_all_external_symbols(pkg_name, globs):
for x_name in list(globs.keys()):
if not __is_defined_in_submodule(pkg_name, globs[x_name]):
del globs[x_name]