comparison env/lib/python3.7/site-packages/galaxy/util/submodules.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 from __future__ import absolute_import
2
3 import importlib
4 import logging
5 import pkgutil
6
7 log = logging.getLogger(__name__)
8
9
10 def import_submodules(module, ordered=True, recursive=False):
11 """ Import all submodules of a module
12
13 :param module: module (package name or actual module)
14 :type module: str | module
15
16 :param ordered: Whether to order the returned modules. The default is True, and
17 modules are returned in reverse order to allow hierarchical overrides
18 i.e. 000_galaxy_rules.py, 100_site_rules.py, 200_instance_rules.py
19 :type ordered: bool
20
21 :param recursive: Recursively returns all subpackages
22 :type recursive: bool
23
24 :rtype: [module]
25 """
26 sub_modules = __import_submodules_impl(module, recursive)
27 if ordered:
28 return sorted(sub_modules, reverse=True, key=lambda m: m.__name__)
29 else:
30 return sub_modules
31
32
33 def __import_submodules_impl(module, recursive=False):
34 """ Implementation of import only, without sorting.
35
36 :param module: module (package name or actual module)
37 :type module: str | module
38 :rtype: [module]
39 """
40 if isinstance(module, str):
41 module = importlib.import_module(module)
42 submodules = []
43 for _, name, is_pkg in pkgutil.walk_packages(module.__path__):
44 full_name = module.__name__ + '.' + name
45 try:
46 submodule = importlib.import_module(full_name)
47 submodules.append(submodule)
48 if recursive and is_pkg:
49 submodules.update(__import_submodules_impl(submodule, recursive=True))
50 except BaseException:
51 message = "%s dynamic module could not be loaded (traceback follows):" % (full_name)
52 log.exception(message)
53 continue
54 return submodules