本文整理汇总了Python中importlib.abc方法的典型用法代码示例。如果您正苦于以下问题:Python importlib.abc方法的具体用法?Python importlib.abc怎么用?Python importlib.abc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类importlib
的用法示例。
在下文中一共展示了importlib.abc方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _add_module_finder
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import abc [as 别名]
def _add_module_finder(self) -> None:
"""Add a custom finder to support Nest module import syntax.
"""
module_manager = self
class NamespaceLoader(importlib.abc.Loader):
def create_module(self, spec):
_, namespace = spec.name.split('.')
module = ModuleType(spec.name)
module_manager._update_namespaces()
meta = module_manager.namespaces.get(namespace)
module.__path__ = [meta['module_path']] if meta else []
return module
def exec_module(self, module):
pass
class NestModuleFinder(importlib.abc.MetaPathFinder):
def __init__(self):
super(NestModuleFinder, self).__init__()
self.reserved_namespaces = [
v[:-3] for v in os.listdir(os.path.dirname(os.path.realpath(__file__))) if v.endswith('.py')]
def find_spec(self, fullname, path, target=None):
if fullname.startswith('nest.'):
name = fullname.split('.')
if len(name) == 2:
if not name[1] in self.reserved_namespaces:
return importlib.machinery.ModuleSpec(fullname, NamespaceLoader())
sys.meta_path.insert(0, NestModuleFinder())
示例2: open_module
# 需要导入模块: import importlib [as 别名]
# 或者: from importlib import abc [as 别名]
def open_module(self, event=None):
# XXX Shouldn't this be in IOBinding?
try:
name = self.text.get("sel.first", "sel.last")
except TclError:
name = ""
else:
name = name.strip()
name = tkSimpleDialog.askstring("Module",
"Enter the name of a Python module\n"
"to search on sys.path and open:",
parent=self.text, initialvalue=name)
if name:
name = name.strip()
if not name:
return
# XXX Ought to insert current file's directory in front of path
try:
spec = importlib.util.find_spec(name)
except (ValueError, ImportError) as msg:
tkMessageBox.showerror("Import error", str(msg), parent=self.text)
return
if spec is None:
tkMessageBox.showerror("Import error", "module not found",
parent=self.text)
return
if not isinstance(spec.loader, importlib.abc.SourceLoader):
tkMessageBox.showerror("Import error", "not a source-based module",
parent=self.text)
return
try:
file_path = spec.loader.get_filename(name)
except AttributeError:
tkMessageBox.showerror("Import error",
"loader does not support get_filename",
parent=self.text)
return
if self.flist:
self.flist.open(file_path)
else:
self.io.loadfile(file_path)
return file_path