def get_package_libraries(pkg):
"""
Recursively yield template tag libraries defined in submodules of a
package.
"""
for entry in walk_packages(pkg.__path__, pkg.__name__ + '.'):
try:
module = import_module(entry[1])
except ImportError as e:
raise InvalidTemplateLibrary(
"Invalid template library specified. ImportError raised when "
"trying to load '%s': %s" % (entry[1], e)
)
if hasattr(module, 'register'):
yield entry[1]
python类walk_packages()的实例源码
def get_package_libraries(pkg):
"""
Recursively yield template tag libraries defined in submodules of a
package.
"""
for entry in walk_packages(pkg.__path__, pkg.__name__ + '.'):
try:
module = import_module(entry[1])
except ImportError as e:
raise InvalidTemplateLibrary(
"Invalid template library specified. ImportError raised when "
"trying to load '%s': %s" % (entry[1], e)
)
if hasattr(module, 'register'):
yield entry[1]
def _get_module(module_name):
"""Returns a tuple with all objects that could be instantiated
without arguments.
Args:
module_name: A string object. It represents the name of the top-level module.
Returns:
A ModuleType object with all sub-modules imported from the given module.
"""
top_level_module = importlib.import_module(module_name)
prefix = modulename(top_level_module) + "."
try:
for module_loader, name, is_pkg in pkgutil.walk_packages(path=top_level_module.__path__, prefix=prefix):
importlib.import_module(name, top_level_module)
except (ImportError, ImportWarning) as e:
LOG.debug(str(e))
return top_level_module
def get_all_classes_of_type(to_find=None, path="."):
"""
Get all classes currently imported by the Python environment found
in the given path.
:param to_find: The class to find all instances of.
:param path: The import path to walk.
:return: A list of all classes that are subclasses of to_find as found
in the given path.
"""
to_return = []
for importer, name, is_package in pkgutil.walk_packages(path=[path]):
current_package_name = name if path is "." else ".".join([path.replace("/", "."), name])
to_return.extend(IntrospectionHelper.get_classes_from_module_name(
module_name=current_package_name,
parent_class=to_find,
))
return to_return
def list_cls_under_mod(mod, cls, uq_attr):
"""
List classes including derived under one module (imported).
Parameters:
mod: imported module
cls: Base class
uq_attr: name of the class member that each class own a unique value
Return:
A dict that represents a class.
"""
r = dict()
for loader, name, ispkg in pkgutil.walk_packages(
getattr(mod, '__path__', None), prefix=mod.__name__ + '.'):
if ispkg is False:
mod = importlib.import_module(name)
for name, c in inspect.getmembers(mod, inspect.isclass):
if issubclass(c, cls):
v = getattr(c, uq_attr)
if v is not None:
r[v] = c
return r
def fetch_list_datasets():
"""Get all datasets into a dictionary.
Returns
-------
dict
A dictionary where keys are names of datasets and values are
a dictionary containing information like urls or keywords of
a dataset.
"""
db_list = {}
for _, modname, ispkg in pkgutil.walk_packages(path=datasets.__path__,
prefix=datasets.__name__ + '.',
onerror=lambda x: None):
if ispkg:
paths = modname.split('.')
db = get_dataset_attributes(modname)
if db:
dbname = paths[-1]
db_list.update({dbname: db})
return db_list
def writedocs(dir, pkgpath='', done=None):
"""Write out HTML documentation for all modules in a directory tree."""
if done is None: done = {}
for importer, modname, ispkg in pkgutil.walk_packages([dir], pkgpath):
writedoc(modname)
return
def run(self, callback, key=None, completer=None, onerror=None):
if key: key = lower(key)
self.quit = False
seen = {}
for modname in sys.builtin_module_names:
if modname != '__main__':
seen[modname] = 1
if key is None:
callback(None, modname, '')
else:
desc = split(__import__(modname).__doc__ or '', '\n')[0]
if find(lower(modname + ' - ' + desc), key) >= 0:
callback(None, modname, desc)
for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror):
if self.quit:
break
if key is None:
callback(None, modname, '')
else:
loader = importer.find_module(modname)
if hasattr(loader,'get_source'):
import StringIO
desc = source_synopsis(
StringIO.StringIO(loader.get_source(modname))
) or ''
if hasattr(loader,'get_filename'):
path = loader.get_filename(modname)
else:
path = None
else:
module = loader.load_module(modname)
desc = (module.__doc__ or '').splitlines()[0]
path = getattr(module,'__file__',None)
if find(lower(modname + ' - ' + desc), key) >= 0:
callback(path, modname, desc)
if completer:
completer()
def import_submodules(name, submodules=None):
"""Import all submodules for a package/module name"""
sys.path.insert(0, name)
if submodules:
for submodule in submodules:
import_string('{0}.{1}'.format(name, submodule))
else:
for item in pkgutil.walk_packages([name]):
import_string('{0}.{1}'.format(name, item[1]))
def find_packages(path):
# This method returns packages and subpackages as well.
return [name for _, name, is_pkg in walk_packages([path]) if is_pkg]
def load_classes(self, package, suffix):
classes = {}
folder = list(import_module(package).__path__)[0]
for loader, mod_name, is_pkg in pkgutil.walk_packages([folder]):
if not mod_name.endswith(suffix):
continue
try:
module = loader.find_module(mod_name).load_module(mod_name)
except:
log.exception('Loading', mod_name)
continue
cls_name = to_camel(mod_name)
mod_cls = getattr(module, cls_name, '')
if not isclass(mod_cls):
log.warning('Could not find', cls_name, 'in', mod_name)
continue
try:
plugin_path = object.__getattribute__(self, 'plugin_path') + '.'
except AttributeError:
plugin_path = ''
mod_cls.attr_name = self.make_name(mod_cls)
mod_cls.plugin_path = plugin_path + mod_cls.attr_name
classes[mod_cls.attr_name] = mod_cls
return classes
def import_submodules(cls, entity):
"""Import packages and/or modules."""
entity = importlib.import_module(entity)
try:
for _, name, is_pkg in pkgutil.walk_packages(entity.__path__):
if not name.startswith('_'):
full_name = entity.__name__ + '.' + name
importlib.import_module(full_name)
except AttributeError:
pass
def makeParserLoadSubmodules(self, parser):
for _, modname, ispkg in walk_packages(path=mongodb_consistent_backup.__path__, prefix=mongodb_consistent_backup.__name__ + '.'):
if not ispkg:
continue
try:
components = modname.split('.')
mod = __import__(components[0])
for comp in components[1:]:
mod = getattr(mod, comp)
parser = mod.config(parser)
except AttributeError:
continue
return parser
def writedocs(dir, pkgpath='', done=None):
"""Write out HTML documentation for all modules in a directory tree."""
if done is None: done = {}
for importer, modname, ispkg in pkgutil.walk_packages([dir], pkgpath):
writedoc(modname)
return
def run(self, callback, key=None, completer=None, onerror=None):
if key: key = lower(key)
self.quit = False
seen = {}
for modname in sys.builtin_module_names:
if modname != '__main__':
seen[modname] = 1
if key is None:
callback(None, modname, '')
else:
desc = split(__import__(modname).__doc__ or '', '\n')[0]
if find(lower(modname + ' - ' + desc), key) >= 0:
callback(None, modname, desc)
for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror):
if self.quit:
break
if key is None:
callback(None, modname, '')
else:
loader = importer.find_module(modname)
if hasattr(loader,'get_source'):
import StringIO
desc = source_synopsis(
StringIO.StringIO(loader.get_source(modname))
) or ''
if hasattr(loader,'get_filename'):
path = loader.get_filename(modname)
else:
path = None
else:
module = loader.load_module(modname)
desc = (module.__doc__ or '').splitlines()[0]
path = getattr(module,'__file__',None)
if find(lower(modname + ' - ' + desc), key) >= 0:
callback(path, modname, desc)
if completer:
completer()
def import_submodules(package, recursive=True):
""" Import all submodules of a module, recursively, including subpackages
:param package: package (name or actual module)
:type package: str | module
:rtype: dict[str, types.ModuleType]
"""
if isinstance(package, str):
package = importlib.import_module(package)
results = {}
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
full_name = package.__name__ + '.' + name
results[full_name] = importlib.import_module(full_name)
if recursive and is_pkg:
results.update(import_submodules(full_name))
return results
def find_packages(path):
# This method returns packages and subpackages as well.
return [name for _, name, is_pkg in walk_packages([path]) if is_pkg]
def get_all_modules(path, level=""):
modules = []
for loader, name, ispkg in walk_packages([path]):
modules.append(level + "." + name if level else name)
if ispkg:
modules.extend(get_all_modules(os.path.join(path, name), modules[-1]))
if level == package_name:
modules.append(os.path.basename(path))
return modules
def list_modules_in_package_path(package_path, prefix):
"""Get the list of all modules in required package_path recursively
Args:
package_path: The fully qualified linux path of the python package
to traverse recursively
prefix: The root python package name for the plugins
Returns: List of modules in the given package path.
"""
modules = []
path_to_walk = [(package_path, prefix)]
while len(path_to_walk) > 0:
curr_path, curr_prefix = path_to_walk.pop()
for importer, name, ispkg in pkgutil.walk_packages(
path=[curr_path]
):
if ispkg:
path_to_walk.append(
(
'%s/%s/' % (curr_path, name),
'%s.%s' % (curr_prefix, name)
)
)
else:
modules.append((name, '%s.%s' % (curr_prefix, name)))
return modules
def build_awg_translator_map():
translators_map = {}
translators = [_[1] for _ in pkgutil.walk_packages(drivers.__path__)]
for translator in translators:
module = import_module('QGL.drivers.' + translator)
ext = module.get_seq_file_extension()
if ext in translators_map:
translators_map[ext].append(module)
else:
translators_map[ext] = [module]
return translators_map
# static translator map