本文整理匯總了Python中imp.PKG_DIRECTORY屬性的典型用法代碼示例。如果您正苦於以下問題:Python imp.PKG_DIRECTORY屬性的具體用法?Python imp.PKG_DIRECTORY怎麽用?Python imp.PKG_DIRECTORY使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類imp
的用法示例。
在下文中一共展示了imp.PKG_DIRECTORY屬性的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: find_module
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def find_module(self, fullname, path=None):
logger.debug('Running find_module: {0}...'.format(fullname))
if '.' in fullname:
parent, child = fullname.rsplit('.', 1)
if path is None:
loader = self.find_module(parent, path)
mod = loader.load_module(parent)
path = mod.__path__
fullname = child
# Perhaps we should try using the new importlib functionality in Python
# 3.3: something like this?
# thing = importlib.machinery.PathFinder.find_module(fullname, path)
try:
self.found = imp.find_module(fullname, path)
except Exception as e:
logger.debug('Py2Fixer could not find {0}')
logger.debug('Exception was: {0})'.format(fullname, e))
return None
self.kind = self.found[-1][-1]
if self.kind == imp.PKG_DIRECTORY:
self.pathname = os.path.join(self.found[1], '__init__.py')
elif self.kind == imp.PY_SOURCE:
self.pathname = self.found[1]
return self
示例2: is_package
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def is_package(self, fullname):
"""Returns whether the module specified by fullname refers to a package.
This implements part of the extensions to the PEP 302 importer protocol.
Args:
fullname: The fullname of the module.
Returns:
True if fullname refers to a package.
"""
submodule, search_path = self._get_parent_search_path(fullname)
_, _, description, loader = self._find_module_or_loader(
submodule, fullname, search_path)
if loader:
return loader.is_package(fullname)
_, _, file_type = description
if file_type == imp.PKG_DIRECTORY:
return True
return False
示例3: SetAllowedModule
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def SetAllowedModule(name):
"""Allow the use of a module based on where it is located.
Meant to be used by use_library() so that it has a link back into the
trusted part of the interpreter.
Args:
name: Name of the module to allow.
"""
stream, pathname, description = imp.find_module(name)
pathname = os.path.normcase(os.path.abspath(pathname))
if stream:
stream.close()
FakeFile.ALLOWED_FILES.add(pathname)
FakeFile.ALLOWED_FILES.add(os.path.realpath(pathname))
else:
assert description[2] == imp.PKG_DIRECTORY
if pathname.startswith(SITE_PACKAGES):
FakeFile.ALLOWED_SITE_PACKAGE_DIRS.add(pathname)
FakeFile.ALLOWED_SITE_PACKAGE_DIRS.add(os.path.realpath(pathname))
else:
FakeFile.ALLOWED_DIRS.add(pathname)
FakeFile.ALLOWED_DIRS.add(os.path.realpath(pathname))
示例4: GetModuleInfo
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def GetModuleInfo(self, fullname):
"""Determines the path on disk and the search path of a module or package.
Args:
fullname: Full name of the module to look up (e.g., foo.bar).
Returns:
Tuple (pathname, search_path, submodule) where:
pathname: String containing the full path of the module on disk,
or None if the module wasn't loaded from disk (e.g. from a zipfile).
search_path: List of paths that belong to the found package's search
path or None if found module is not a package.
submodule: The relative name of the submodule that's being imported.
"""
submodule, search_path = self.GetParentSearchPath(fullname)
source_file, pathname, description = self.FindModuleRestricted(submodule, fullname, search_path)
suffix, mode, file_type = description
module_search_path = None
if file_type == self._imp.PKG_DIRECTORY:
module_search_path = [pathname]
pathname = os.path.join(pathname, '__init__%spy' % os.extsep)
return pathname, module_search_path, submodule
示例5: find_module
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def find_module(module, paths=None):
"""Just like 'imp.find_module()', but with package support"""
parts = module.split('.')
while parts:
part = parts.pop(0)
f, path, (suffix,mode,kind) = info = imp.find_module(part, paths)
if kind==PKG_DIRECTORY:
parts = parts or ['__init__']
paths = [path]
elif parts:
raise ImportError("Can't find %r in %s" % (parts,module))
return info
示例6: find_module
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def find_module(module, paths=None):
"""Just like 'imp.find_module()', but with package support"""
parts = module.split('.')
while parts:
part = parts.pop(0)
f, path, (suffix, mode, kind) = info = imp.find_module(part, paths)
if kind == PKG_DIRECTORY:
parts = parts or ['__init__']
paths = [path]
elif parts:
raise ImportError("Can't find %r in %s" % (parts, module))
return info
示例7: get_code
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def get_code(self, fullname=None):
fullname = self._fix_name(fullname)
if self.code is None:
mod_type = self.etc[2]
if mod_type==imp.PY_SOURCE:
source = self.get_source(fullname)
self.code = compile(source, self.filename, 'exec')
elif mod_type==imp.PY_COMPILED:
self._reopen()
try:
self.code = read_code(self.file)
finally:
self.file.close()
elif mod_type==imp.PKG_DIRECTORY:
self.code = self._get_delegate().get_code()
return self.code
示例8: get_source
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def get_source(self, fullname=None):
fullname = self._fix_name(fullname)
if self.source is None:
mod_type = self.etc[2]
if mod_type==imp.PY_SOURCE:
self._reopen()
try:
self.source = self.file.read()
finally:
self.file.close()
elif mod_type==imp.PY_COMPILED:
if os.path.exists(self.filename[:-1]):
f = open(self.filename[:-1], 'rU')
self.source = f.read()
f.close()
elif mod_type==imp.PKG_DIRECTORY:
self.source = self._get_delegate().get_source()
return self.source
示例9: find_module_in_dir
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def find_module_in_dir(self, name, dir, allow_packages=1):
if dir is None:
return self.find_builtin_module(name)
if allow_packages:
fullname = self.hooks.path_join(dir, name)
if self.hooks.path_isdir(fullname):
stuff = self.find_module_in_dir("__init__", fullname, 0)
if stuff:
file = stuff[0]
if file: file.close()
return None, fullname, ('', '', PKG_DIRECTORY)
for info in self.hooks.get_suffixes():
suff, mode, type = info
fullname = self.hooks.path_join(dir, name+suff)
try:
fp = self.hooks.openfile(fullname, mode)
return fp, fullname, info
except self.hooks.openfile_error:
pass
return None
示例10: load_module
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def load_module(self, name, stuff):
file, filename, info = stuff
(suff, mode, type) = info
try:
if type == BUILTIN_MODULE:
return self.hooks.init_builtin(name)
if type == FROZEN_MODULE:
return self.hooks.init_frozen(name)
if type == C_EXTENSION:
m = self.hooks.load_dynamic(name, filename, file)
elif type == PY_SOURCE:
m = self.hooks.load_source(name, filename, file)
elif type == PY_COMPILED:
m = self.hooks.load_compiled(name, filename, file)
elif type == PKG_DIRECTORY:
m = self.hooks.load_package(name, filename, file)
else:
raise ImportError, "Unrecognized module type (%r) for %s" % \
(type, name)
finally:
if file: file.close()
m.__file__ = filename
return m
示例11: test_imp_package
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def test_imp_package(self):
self.write_to_file(self._f_init, "my_name = 'imp package test'")
pf, pp, (px, pm, pt) = imp.find_module(self._testdir, [self.test_dir])
self.assertEqual(pt, imp.PKG_DIRECTORY)
self.assertEqual(pf, None)
self.assertEqual(px, "")
self.assertEqual(pm, "")
module = imp.load_module(self._testdir, pf, pp, (px, pm, pt))
self.assertTrue(self._testdir in sys.modules)
self.assertEqual(module.my_name, 'imp package test')
with path_modifier(self.test_dir):
fm = imp.find_module(self._testdir)
# unpack the result obtained above
pf, pp, (px, pm, pt) = fm
self.assertEqual(pt, imp.PKG_DIRECTORY)
self.assertEqual(pf, None)
self.assertEqual(px, "")
self.assertEqual(pm, "")
module = imp.load_module(self._testdir, pf, pp, (px, pm, pt))
self.assertEqual(module.my_name, 'imp package test')
示例12: findPackageContents
# 需要導入模塊: import imp [as 別名]
# 或者: from imp import PKG_DIRECTORY [as 別名]
def findPackageContents(name, searchpath=None):
head = name.split(".")[-1]
if identifierRE.match(head) is None:
return {}
try:
fp, path, (ext, mode, tp) = imp.find_module(head, searchpath)
except ImportError:
return {}
modules = {name: None}
if tp == imp.PKG_DIRECTORY and path:
files = os.listdir(path)
for sub in files:
sub, ext = os.path.splitext(sub)
fullname = name + "." + sub
if sub != "__init__" and fullname not in modules:
modules.update(findPackageContents(fullname, [path]))
return modules