def get_sources(source_list=None):
"""
Get a list of all availale Sources.
Expects that source_list is the direct array of Sources loaded from settings.
"""
import pkgutil
import os
pkg_path = os.path.dirname(sources.__file__)
loaded = []
for _,name,_ in pkgutil.iter_modules([pkg_path]):
if '_source' not in name:
continue
fi = __import__(name, fromlist=[''])
for clazz in _module_classes(fi):
if source_list is not None:
for obj in source_list:
cl = clazz() # build the class.
if cl.from_obj(obj):# if the class accepts this data.
loaded.append(cl)
else:
cl = clazz()
loaded.append(cl)
return loaded
python类iter_modules()的实例源码
def _sub_modules_dict(self):
"""
Lists modules in the directory of this module (if this module is a
package).
"""
path = self._module.path
names = {}
if path is not None and path.endswith(os.path.sep + '__init__.py'):
mods = pkgutil.iter_modules([os.path.dirname(path)])
for module_loader, name, is_pkg in mods:
fake_n = helpers.FakeName(name)
# It's obviously a relative import to the current module.
imp = helpers.FakeImport(fake_n, self, level=1)
fake_n.parent = imp
names[name] = [fake_n]
# TODO add something like this in the future, its cleaner than the
# import hacks.
# ``os.path`` is a hardcoded exception, because it's a
# ``sys.modules`` modification.
#if str(self.name) == 'os':
# names.append(helpers.FakeName('path', parent=self))
return names
def _get_module_names(self, search_path=None):
"""
Get the names of all modules in the search_path. This means file names
and not names defined in the files.
"""
names = []
# add builtin module names
if search_path is None:
names += [self._generate_name(name) for name in sys.builtin_module_names]
if search_path is None:
search_path = self.sys_path_with_modifications()
for module_loader, name, is_pkg in pkgutil.iter_modules(search_path):
names.append(self._generate_name(name))
return names
def getunpackers():
"""Scans the unpackers dir, finds unpackers and add them to UNPACKERS list.
An unpacker will be loaded only if it is a valid python module (name must
adhere to naming conventions) and it is not blacklisted (i.e. inserted
into BLACKLIST."""
path = __path__
prefix = __name__ + '.'
unpackers = []
interface = ['unpack', 'detect', 'PRIORITY']
for _importer, modname, _ispkg in pkgutil.iter_modules(path, prefix):
if 'tests' not in modname and modname not in BLACKLIST:
try:
module = __import__(modname, fromlist=interface)
except ImportError:
raise UnpackingError('Bad unpacker: %s' % modname)
else:
unpackers.append(module)
return sorted(unpackers, key = lambda mod: mod.PRIORITY)
def walk_modules(path):
"""Loads a module and all its submodules from the given module path and
returns them. If *any* module throws an exception while importing, that
exception is thrown back.
For example: walk_modules('scrapy.utils')
"""
mods = []
mod = import_module(path)
mods.append(mod)
if hasattr(mod, '__path__'):
for _, subpath, ispkg in iter_modules(mod.__path__):
fullpath = path + '.' + subpath
if ispkg:
mods += walk_modules(fullpath)
else:
submod = import_module(fullpath)
mods.append(submod)
return mods
def get_submodules(mod):
modules = []
for loader, module_name, is_pkg in pkgutil.iter_modules(mod.__path__):
if module_name.startswith("test_"):
continue
mod_name = mod.__name__ + "." + module_name
# print("Found module ", mod_name)
module = pkgutil.importlib.import_module(mod_name)
modules.append(module)
results = []
for mod in modules:
try:
intro = mod.__doc__.split("\n")[0]
except:
sys.exit("Module missing a docstring: {}".format(mod))
results.append((mod.__name__, intro))
return results
def install_modules(self):
installed_command_modules = []
for cmd in self.command_table:
try:
self.command_table[cmd].load_arguments()
except (ImportError, ValueError):
pass
mods_ns_pkg = import_module('azure.cli.command_modules')
for _, modname, _ in pkgutil.iter_modules(mods_ns_pkg.__path__):
if modname not in BLACKLISTED_MODS:
installed_command_modules.append(modname)
for mod in installed_command_modules:
try:
mod = import_module('azure.cli.command_modules.' + mod)
mod.load_params(mod)
mod.load_commands()
except Exception: # pylint: disable=broad-except
print("Error loading: {}".format(mod))
_update_command_definitions(self.command_table)
pluginDynamicMutatePolicies.py 文件源码
项目:Basic-Expression-Lexicon-Variation-Algorithms-BELVA
作者: kenb123
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def dynamic_load_return_mods():
#---------------------------------------
#---- we want to return mod names so we can control output
# path = os.path.join(os.path.dirname(__file__), "applicationSignatures")
# path = os.path.join(os.getcwd(), "plugins/policies/mutate")
path = os.path.join(os.path.dirname(__file__), "plugins/policies/mutate")
path = path.replace("src/pluginSystem/", "")
path = path.replace("src\pluginSystem\\", "")
modules = pkgutil.iter_modules(path=[path])
mode_names = []
for loader, mod_name, ispkg in modules:
mode_names.append(mod_name)
return mode_names
#---------------------------------------
pluginDynamicSelectPolicies.py 文件源码
项目:Basic-Expression-Lexicon-Variation-Algorithms-BELVA
作者: kenb123
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def dynamic_load_return_mods():
#---------------------------------------
# path = os.path.join(os.getcwd(), "plugins/policies/select")
path = os.path.join(os.path.dirname(__file__), "plugins/policies/select")
path = path.replace("src/pluginSystem/", "")
path = path.replace("src\pluginSystem\\", "")
modules = pkgutil.iter_modules(path=[path])
# print ""
mode_names = []
for loader, mod_name, ispkg in modules:
mode_names.append(mod_name)
return mode_names
#---------------------------------------
pluginDynamicSubstitutions.py 文件源码
项目:Basic-Expression-Lexicon-Variation-Algorithms-BELVA
作者: kenb123
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def dynamic_load_return_mods():
#---------------------------------------
# path = os.path.join(os.getcwd(), "plugins/substitutions")
path = os.path.join(os.path.dirname(__file__), "plugins/substitutions")
path = path.replace("src/pluginSystem/", "")
path = path.replace("src\pluginSystem\\", "")
modules = pkgutil.iter_modules(path=[path])
mode_names = []
for loader, mod_name, ispkg in modules:
mode_names.append(mod_name)
return mode_names
#---------------------------------------
def loop_modules(runner, cli_pkg, mods, path):
for mod in mods:
try:
cli = importlib.import_module(path + '.' + mod).init()
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
print("Module: " + path + '.' + mod)
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
print("::\n")
click_echo(output(cli))
except:
failures.append(path + '.' + mod)
loop_groups(cli)
submods = sorted([name for _, name, _ in pkgutil.iter_modules([cli_pkg + '/' + mod])])
if submods:
loop_modules(runner, cli_pkg + '/' + mod, submods, path + '.' + mod)
def scan_package(import_path, module=None):
if module is None:
module = __import__(import_path)
basename = module.__name__ + '.'
path = getattr(module, '__path__', None)
yield module
if path is None:
return
for importer, modname, ispkg in pkgutil.iter_modules(path):
fullname = basename + modname
__import__(fullname)
submodule = getattr(module, modname)
yield submodule
if ispkg:
for m in scan_package(fullname, submodule):
yield m
def setup_addon_modules(path, package_name):
"""
Imports and reloads all modules in this addon.
path -- __path__ from __init__.py
package_name -- __name__ from __init__.py
"""
def get_submodule_names(path = path[0], root = ""):
module_names = []
for importer, module_name, is_package in pkgutil.iter_modules([path]):
if is_package:
sub_path = os.path.join(path, module_name)
sub_root = root + module_name + "."
module_names.extend(get_submodule_names(sub_path, sub_root))
else:
module_names.append(root + module_name)
return module_names
def import_submodules(names):
modules = []
for name in names:
modules.append(importlib.import_module("." + name, package_name))
return modules
def reload_modules(modules):
for module in modules:
importlib.reload(module)
names = get_submodule_names()
modules = import_submodules(names)
if reload_event:
reload_modules(modules)
return modules
def setup_addon_modules(path, package_name):
"""
Imports and reloads all modules in this addon.
path -- __path__ from __init__.py
package_name -- __name__ from __init__.py
"""
def get_submodule_names(path=path[0], root=""):
module_names = []
for importer, module_name, is_package in pkgutil.iter_modules([path]):
if is_package:
sub_path = path + "\\" + module_name
sub_root = root + module_name + "."
module_names.extend(get_submodule_names(sub_path, sub_root))
else:
module_names.append(root + module_name)
return module_names
def import_submodules(names):
modules = []
for name in names:
modules.append(importlib.import_module("." + name, package_name))
return modules
def reload_modules(modules):
for module in modules:
importlib.reload(module)
names = get_submodule_names()
modules = import_submodules(names)
if reload_event:
reload_modules(modules)
return modules
def find_commands(management_dir):
command_dir = os.path.join(management_dir, 'commands')
return [name for _, name, is_pkg in pkgutil.iter_modules([command_dir])]
def index(self, dir, shadowed=None):
"""Generate an HTML index for a directory of modules."""
modpkgs = []
if shadowed is None: shadowed = {}
for importer, name, ispkg in pkgutil.iter_modules([dir]):
modpkgs.append((name, '', ispkg, name in shadowed))
shadowed[name] = 1
modpkgs.sort()
contents = self.multicolumn(modpkgs, self.modpkglink)
return self.bigsection(dir, '#ffffff', '#ee77aa', contents)
# -------------------------------------------- text documentation generator
def list_policies():
import pkgutil
import Chain
policies = []
for _, name, ispkg in pkgutil.iter_modules(path=[os.path.dirname(Chain.__file__)]):
if not ispkg:
policies.append(name)
return policies
def list_policies():
import pkgutil
import Chain
policies = []
for _, name, ispkg in pkgutil.iter_modules(path=[os.path.dirname(Chain.__file__)]):
if not ispkg:
policies.append(name)
return policies
def find_modules(import_path, include_packages=False, recursive=False):
"""Finds all the modules below a package. This can be useful to
automatically import all views / controllers so that their metaclasses /
function decorators have a chance to register themselves on the
application.
Packages are not returned unless `include_packages` is `True`. This can
also recursively list modules but in that case it will import all the
packages to get the correct load path of that module.
:param import_name: the dotted name for the package to find child modules.
:param include_packages: set to `True` if packages should be returned, too.
:param recursive: set to `True` if recursion should happen.
:return: generator
"""
module = import_string(import_path)
path = getattr(module, '__path__', None)
if path is None:
raise ValueError('%r is not a package' % import_path)
basename = module.__name__ + '.'
for importer, modname, ispkg in pkgutil.iter_modules(path):
modname = basename + modname
if ispkg:
if include_packages:
yield modname
if recursive:
for item in find_modules(modname, include_packages, True):
yield item
else:
yield modname
def discover_components(package, directory, base_class):
"""Discover implementations of a base class in a package.
Parameters
----------
package : str
Package name
directory : str
Directory of the package to which is inspected.
base_class : object
Base class of objects to discover
Returns
-------
list : all subclasses of `base_class` inside `directory`
"""
components = list()
for module_loader, module_name, ispkg in pkgutil.iter_modules(
[directory]):
full_module_name = "%s.%s" % (package, module_name)
if full_module_name not in sys.modules and not ispkg:
module = importlib.import_module(full_module_name)
for member_name, obj in inspect.getmembers(module):
if inspect.isclass(obj) and issubclass(base_class, obj):
classifier = obj
components.append(classifier)
return components