本文整理匯總了Python中pkg_resources.resource_isdir方法的典型用法代碼示例。如果您正苦於以下問題:Python pkg_resources.resource_isdir方法的具體用法?Python pkg_resources.resource_isdir怎麽用?Python pkg_resources.resource_isdir使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pkg_resources
的用法示例。
在下文中一共展示了pkg_resources.resource_isdir方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: scan_subpackages
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def scan_subpackages(cls, package: str) -> Sequence[str]:
"""Return a list of sub-packages defined under a named package."""
# FIXME use importlib.resources in python 3.7
if "." in package:
package, sub_pkg = package.split(".", 1)
else:
sub_pkg = "."
if not pkg_resources.resource_isdir(package, sub_pkg):
raise ModuleLoadError(f"Undefined package {package}")
found = []
joiner = "" if sub_pkg == "." else f"{sub_pkg}."
for sub_path in pkg_resources.resource_listdir(package, sub_pkg):
if pkg_resources.resource_exists(
package, f"{sub_pkg}/{sub_path}/__init__.py"
):
found.append(f"{package}.{joiner}{sub_path}")
return found
示例2: _unpack_assets
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def _unpack_assets(output_dir, module, asset_root, execute, current_path):
"""
The internal helper function for unpack_assets(...) recursion.
:param current_path: Records the current
"""
for asset in pkg_resources.resource_listdir(module, current_path):
asset_target = os.path.join(os.path.relpath(current_path, asset_root), asset)
if pkg_resources.resource_isdir(module, os.path.join(current_path, asset)):
safe_mkdir(os.path.join(output_dir, asset_target))
_unpack_assets(output_dir, module, asset_root, execute, os.path.join(current_path, asset))
else:
output_file = os.path.join(output_dir, asset_target)
with open(output_file, 'wb') as fp:
fp.write(pkg_resources.resource_string(
module, os.path.join(asset_root, asset_target)))
execute(output_file)
示例3: image_resources
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def image_resources(package=None, directory='resources'):
"""
Returns all images under the directory relative to a package path. If no directory or package is specified then the
resources module of the calling package will be used. Images are recursively discovered.
:param package: package name in dotted format.
:param directory: path relative to package path of the resources directory.
:return: a list of images under the specified resources path.
"""
if not package:
package = calling_package()
package_dir = '.'.join([package, directory])
images = []
for i in resource_listdir(package, directory):
if i.startswith('__') or i.endswith('.egg-info'):
continue
fname = resource_filename(package_dir, i)
if resource_isdir(package_dir, i):
images.extend(image_resources(package_dir, i))
elif what(fname):
images.append(fname)
return images
# etc
示例4: _is_scan_dir
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def _is_scan_dir(package, src_dir, dst_dir):
"""Check if working on a scan dir.
"""
if os.path.exists(os.path.join(dst_dir, _CONTROL_DIR_NAME)):
return True
package_name = package.__name__
if pkg_resources.resource_isdir(package_name,
os.path.join(src_dir, _CONTROL_DIR_NAME)):
return True
if pkg_resources.resource_exists(package_name,
os.path.join(src_dir, _CONTROL_DIR_FILE)):
return True
return False
示例5: get_plugins
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def get_plugins():
plugins = []
plugin_to_distinfo = dict(pm.list_plugin_distinfo())
for plugin in pm.get_plugins():
static_path = None
templates_path = None
if plugin.__name__ not in DEFAULT_PLUGINS:
try:
if pkg_resources.resource_isdir(plugin.__name__, "static"):
static_path = pkg_resources.resource_filename(
plugin.__name__, "static"
)
if pkg_resources.resource_isdir(plugin.__name__, "templates"):
templates_path = pkg_resources.resource_filename(
plugin.__name__, "templates"
)
except (KeyError, ImportError):
# Caused by --plugins_dir= plugins - KeyError/ImportError thrown in Py3.5
pass
plugin_info = {
"name": plugin.__name__,
"static_path": static_path,
"templates_path": templates_path,
"hooks": [h.name for h in pm.get_hookcallers(plugin)],
}
distinfo = plugin_to_distinfo.get(plugin)
if distinfo:
plugin_info["version"] = distinfo.version
plugin_info["name"] = distinfo.project_name
plugins.append(plugin_info)
return plugins
示例6: find_guest_tools
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def find_guest_tools():
"""Return the path of the guest tools installed with the running virtme.
"""
if pkg_resources.resource_isdir(__name__, 'guest'):
return pkg_resources.resource_filename(__name__, 'guest')
# No luck. This is somewhat surprising.
return None
示例7: copy_resource_dir
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def copy_resource_dir(src, dest):
"""
To copy package data directory to destination
"""
package_name = "assembly"
dest = (dest + "/" + os.path.basename(src)).rstrip("/")
if pkg_resources.resource_isdir(package_name, src):
if not os.path.isdir(dest):
os.makedirs(dest)
for res in pkg_resources.resource_listdir(about.__name__, src):
copy_resource_dir(src + "/" + res, dest)
else:
if not os.path.isfile(dest) and os.path.splitext(src)[1] not in [".pyc"]:
copy_resource_file(src, dest)
示例8: _iter
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def _iter(plugin, module):
'''
Iterate over migrations for a given plugin module
Yield tuples in the form (plugin_name, module_name, filename)
'''
module_name = module if isinstance(module, str) else module.__name__
if not resource_isdir(module_name, 'migrations'):
return
for filename in resource_listdir(module_name, 'migrations'):
if filename.endswith('.py') and not filename.startswith('__'):
yield Migration(plugin, filename, module_name)
示例9: test_pr_resources
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def test_pr_resources(self):
import pkg_resources as pr
# App ZIP
pkg = "android1"
names = ["subdir", "__init__.py", "a.txt", "b.so", "mod1.py"]
self.assertCountEqual(names, pr.resource_listdir(pkg, ""))
for name in names:
with self.subTest(name=name):
self.assertTrue(pr.resource_exists(pkg, name))
self.assertEqual(pr.resource_isdir(pkg, name),
name == "subdir")
self.assertFalse(pr.resource_exists(pkg, "nonexistent"))
self.assertFalse(pr.resource_isdir(pkg, "nonexistent"))
self.assertCountEqual(["c.txt"], pr.resource_listdir(pkg, "subdir"))
self.assertTrue(pr.resource_exists(pkg, "subdir/c.txt"))
self.assertFalse(pr.resource_isdir(pkg, "subdir/c.txt"))
self.assertFalse(pr.resource_exists(pkg, "subdir/nonexistent.txt"))
self.check_pr_resource(APP_ZIP, pkg, "__init__.py", b"# This package is")
self.check_pr_resource(APP_ZIP, pkg, "a.txt", b"alpha\n")
self.check_pr_resource(APP_ZIP, pkg, "b.so", b"bravo\n")
self.check_pr_resource(APP_ZIP, pkg, "subdir/c.txt", b"charlie\n")
# Requirements ZIP
self.reset_package("murmurhash")
self.assertCountEqual(["include", "tests", "__init__.pxd", "__init__.pyc", "about.pyc",
"mrmr.pxd", "mrmr.pyx", "mrmr.so"],
pr.resource_listdir("murmurhash", ""))
self.assertCountEqual(["MurmurHash2.h", "MurmurHash3.h"],
pr.resource_listdir("murmurhash", "include/murmurhash"))
self.check_pr_resource(REQS_COMMON_ZIP, "murmurhash", "__init__.pyc", MAGIC_NUMBER)
self.check_pr_resource(REQS_COMMON_ZIP, "murmurhash", "mrmr.pxd", b"from libc.stdint")
self.check_pr_resource(REQS_ABI_ZIP, "murmurhash", "mrmr.so", b"\x7fELF")
示例10: _files_directory
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def _files_directory(self):
_template_path = 'files/public_cluster'
if pkg_resources.resource_isdir(__name__, _template_path):
return pkg_resources.resource_filename(__name__, _template_path)
else:
raise StandardError(
'Could not find template path ({})'.format(_template_path))
示例11: resources_for_test_config
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def resources_for_test_config(test_config):
resources = {}
for key in [constants.CONFIG_BUNDLES_KEY, 'apps', 'libs', 'services']:
key_path = 'test_configs/{}/{}'.format(test_config, key)
if resource_isdir(__name__, key_path):
resources[key] = {resource_name: resource_string(__name__, '{}/{}'.format(key_path, resource_name))
for resource_name in resource_listdir(__name__, key_path)}
return resources
示例12: _fetch
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def _fetch(name, content_type=None):
"""
Load data or models from the package folder.
Args:
name (str): name of the resource
content_type (str): either "data" or "trained_models"
Returns:
fileset (dict): dictionary containing the file content
"""
package_name = 'neuroner'
resource_path = '/'.join((content_type, name))
# get dirs
root_dir = os.path.dirname(pkg_resources.resource_filename(package_name,
'__init__.py'))
src_dir = os.path.join(root_dir, resource_path)
dest_dir = os.path.join('.', content_type, name)
if pkg_resources.resource_isdir(package_name, resource_path):
# copy from package to dest dir
if os.path.isdir(dest_dir):
msg = "Directory '{}' already exists.".format(dest_dir)
print(msg)
else:
shutil.copytree(src_dir, dest_dir)
msg = "Directory created: '{}'.".format(dest_dir)
print(msg)
else:
msg = "{} not found in {} package.".format(name,package_name)
print(msg)
示例13: __init__
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def __init__(self):
"""Initialize the project manager."""
_logger.info("Initializing project manager")
self._projects = {}
metadata = {} # a dict from internal names to project name, dependencies
logic_resource_pkg = settings.LOGIC_RESOURCE_PKG
# Iterate over all project files
for project in resource_listdir(logic_resource_pkg, '.'):
# Skip empty resource paths (apparently, that can happen!!)
if not project:
continue
# Skip ordinary files
if not resource_isdir(logic_resource_pkg, project):
continue
# Construct project path
project_dir = project
# Find project file
for resource in resource_listdir(logic_resource_pkg, project_dir):
if resource.endswith(".project"):
# Compute path to resource
path_to_resource = path.join(project_dir, resource)
path_to_file = FileManager().mktemp()
# Read contents of project file
with open(path_to_file, 'w') as f:
# Copy contents from resource stream
for byte in resource_stream(logic_resource_pkg, path_to_resource):
f.write(byte)
# Extract metadata from project file
internal_name, deps = self.__extract_metadata(path_to_file)
metadata[internal_name] = Metadata(project, deps)
break
# 2nd pass to create and store projects. This way the internal
# names are entirely hidden from the user.
for (i, (project, deps)) in metadata.iteritems():
p = Project(project, *[metadata[d].project for d in deps])
p.internal_name = i
self._projects[project] = p
setattr(self, project.replace('-', '_'), p)
_logger.info("Found project %s that depends on: %s", project, p.dependencies)
示例14: __enter__
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def __enter__(self):
# Compute resource path
project = self._project
cached_logic_dir = runtime.FileManager().getpath('logic')
cached_proj_dir = path.join(cached_logic_dir, project)
# Check if project has been extracted before
if path.exists(cached_proj_dir):
# Compare signatures
disk_signature = resource_stream(
self._pkg, path.join(project, 'checksum')
).read().strip()
cached_signature = open(
path.join(cached_proj_dir, 'checksum'), 'rb'
).read().strip()
# Project hasn't changed; don't overwrite
if disk_signature == cached_signature:
return cached_proj_dir
# remove stale cached project
shutil.rmtree(cached_proj_dir)
_logger.info("Extracting project %s to %s", project, cached_proj_dir)
resource_dirs = [project]
# Iterate over all project files
while resource_dirs:
# Pop next resource directory
res_dir = resource_dirs.pop(0)
# Process its files
for resource in resource_listdir(self._pkg, res_dir):
# Skip empty resource paths (apparently, that can happen!!)
if not resource:
continue
# Compute path to resource
path_to_resource = path.join(res_dir, resource)
path_to_file = path.join(cached_logic_dir, path_to_resource)
# Process resource directories recursively
if resource_isdir(self._pkg, path_to_resource):
resource_dirs.append(path_to_resource)
continue
_logger.debug("Extracting project file %s", path_to_resource)
# Create parent directory
parent_dir = path.dirname(path_to_file)
if not path.exists(parent_dir):
makedirs(parent_dir)
with open(path_to_file, 'w') as f:
# Copy contents from resource stream
for byte in resource_stream(self._pkg, path_to_resource):
f.write(byte)
return cached_proj_dir
示例15: copy_dir
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_isdir [as 別名]
def copy_dir(source, dest, variables, out_=sys.stdout, i=0):
"""
Copies the ``source`` directory to the ``dest`` directory, where
``source`` is some tuple representing an installed package and a
subdirectory in the package, e.g.,
('pecan', os.path.join('scaffolds', 'base'))
('pecan_extension', os.path.join('scaffolds', 'scaffold_name'))
``variables``: A dictionary of variables to use in any substitutions.
Substitution is performed via ``string.Template``.
``out_``: File object to write to (default is sys.stdout).
"""
def out(msg):
out_.write('%s%s' % (' ' * (i * 2), msg))
out_.write('\n')
out_.flush()
names = sorted(pkg_resources.resource_listdir(source[0], source[1]))
if not os.path.exists(dest):
out('Creating %s' % dest)
makedirs(dest)
else:
out('%s already exists' % dest)
return
for name in names:
full = '/'.join([source[1], name])
dest_full = os.path.join(dest, substitute_filename(name, variables))
sub_file = False
if dest_full.endswith('_tmpl'):
dest_full = dest_full[:-5]
sub_file = True
if pkg_resources.resource_isdir(source[0], full):
out('Recursing into %s' % os.path.basename(full))
copy_dir((source[0], full), dest_full, variables, out_, i + 1)
continue
else:
content = pkg_resources.resource_string(source[0], full)
if sub_file:
content = render_template(content, variables)
if content is None:
continue # pragma: no cover
out('Copying %s to %s' % (full, dest_full))
f = open(dest_full, 'wb')
f.write(content)
f.close()