本文整理匯總了Python中pkg_resources.declare_namespace方法的典型用法代碼示例。如果您正苦於以下問題:Python pkg_resources.declare_namespace方法的具體用法?Python pkg_resources.declare_namespace怎麽用?Python pkg_resources.declare_namespace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pkg_resources
的用法示例。
在下文中一共展示了pkg_resources.declare_namespace方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _declare_namespace
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import declare_namespace [as 別名]
def _declare_namespace(self, package_name):
'''
Mock for #pkg_resources.declare_namespace() which calls
#pkgutil.extend_path() afterwards as the original implementation doesn't
seem to properly find all available namespace paths.
'''
self.state['declare_namespace'](package_name)
mod = sys.modules[package_name]
mod.__path__ = pkgutil.extend_path(mod.__path__, package_name)
示例2: __enter__
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import declare_namespace [as 別名]
def __enter__(self):
# pkg_resources comes with setuptools.
try:
import pkg_resources
nsdict = copy.deepcopy(pkg_resources._namespace_packages)
declare_namespace = pkg_resources.declare_namespace
pkg_resources.declare_namespace = self._declare_namespace
except ImportError:
nsdict = None
declare_namespace = None
# Save the global importer state.
self.state = {
'nsdict': nsdict,
'declare_namespace': declare_namespace,
'nspaths': {},
'path': sys.path[:],
'meta_path': sys.meta_path[:],
'disables': {},
'pkgutil.extend_path': pkgutil.extend_path,
}
# Update the systems meta path and apply function mocks.
sys.path[:] = self.path
sys.meta_path[:] = self.meta_path + sys.meta_path
pkgutil.extend_path = extend_path
# If this function is called not the first time, we need to
# restore the modules that have been imported with it and
# temporarily disable the ones that would be shadowed.
for key, mod in items(self.modules):
try: self.state['disables'][key] = sys.modules.pop(key)
except KeyError: pass
sys.modules[key] = mod
# Evaluate imports from the .pth files, if any.
for fn, lineno, stmt in self.pth_imports:
exec_pth_import(fn, lineno, stmt)
# Add the original path to sys.path.
sys.path += self.state['path']
# Update the __path__ of all namespace modules.
for key, mod in items(sys.modules):
if mod is None:
# Relative imports could have lead to None-entries in
# sys.modules. Get rid of them so they can be re-evaluated.
prefix = key.rpartition('.')[0]
if hasattr(sys.modules.get(prefix), '__path__'):
del sys.modules[key]
elif hasattr(mod, '__path__'):
self.state['nspaths'][key] = copy.copy(mod.__path__)
mod.__path__ = pkgutil.extend_path(mod.__path__, mod.__name__)
self.in_context = True
if self.do_autodisable:
self.autodisable()
return self