本文整理匯總了Python中pkg_resources.resource_exists方法的典型用法代碼示例。如果您正苦於以下問題:Python pkg_resources.resource_exists方法的具體用法?Python pkg_resources.resource_exists怎麽用?Python pkg_resources.resource_exists使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pkg_resources
的用法示例。
在下文中一共展示了pkg_resources.resource_exists方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: scan_subpackages
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [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: default
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def default(cls) -> 'PrecalculatedTextMeasurer':
"""Returns a reasonable default PrecalculatedTextMeasurer."""
if cls._default_cache is not None:
return cls._default_cache
if pkg_resources.resource_exists(__name__, 'default-widths.json.xz'):
import lzma
with pkg_resources.resource_stream(__name__,
'default-widths.json.xz') as f:
with lzma.open(f, "rt") as g:
cls._default_cache = PrecalculatedTextMeasurer.from_json(
cast(TextIO, g))
return cls._default_cache
elif pkg_resources.resource_exists(__name__, 'default-widths.json'):
with pkg_resources.resource_stream(__name__,
'default-widths.json') as f:
cls._default_cache = PrecalculatedTextMeasurer.from_json(
io.TextIOWrapper(f, encoding='utf-8'))
return cls._default_cache
else:
raise ValueError('could not load default-widths.json')
示例3: resource_to_data
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def resource_to_data(path_to_data):
"""
This is an auxiliar function to read data from a given resource.
"""
resource_package = 'investpy'
resource_path = '/'.join(('resources', path_to_data))
if pkg_resources.resource_exists(resource_package, resource_path):
data = pd.read_csv(pkg_resources.resource_filename(resource_package, resource_path))
else:
raise FileNotFoundError("ERR#0115: data file not found or errored.")
if data is None:
raise IOError("ERR#0115: data file not found or errored.")
return data
示例4: _is_scan_dir
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [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: __init__
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def __init__(self, mats, definition_file):
self._mats = mats
self.block_map = {}
self.blockstates = {}
for b in self._mats:
if b.ID == 0:
b.stringID = "air"
self.block_map[b.ID] = "minecraft:" + b.stringID
# When running from a bundled app on Linux (and possibly on OSX) pkg_resource can't find the needed files.
if pkg_resources.resource_exists(__name__, definition_file):
# We're running from source or on Windows using the executable (<<== Not sure...)
with pkg_resources.resource_stream(__name__, definition_file) as def_file:
self.blockstates = json.load(def_file)
else:
# In all other cases, retrieve the file directly from the file system.
with open(os.path.join("pymclevel", definition_file)) as def_file:
self.blockstates = json.load(def_file)
self.material_map[self._mats] = self
示例6: find_packaged_regressor
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def find_packaged_regressor(name):
""" Find location of a regression method packaged with YATSM
See :data:`packaged_regressions` for a list of
available pre-packaged regressors
Args:
name (str): name of packaged regression object
Returns:
str: path to packaged regression method
Raises:
KeyError: raise KeyError if user specifies unknown regressor
IOError: raise IOError if the packaged regressor cannot be found
"""
if name not in packaged_regressions:
raise KeyError('Cannot load unknown packaged regressor %s' % name)
path = pkg_resources.resource_filename(__name__, 'pickles')
logger.debug('Checking data files in %s for packaged regressors' % path)
if not pkg_resources.resource_exists(__name__, 'pickles'):
raise IOError('Cannot find packaged regressors in %s. Did you install '
'YATSM via setuptools?' % path)
resource = os.path.join('pickles', name + '.pkl')
if not pkg_resources.resource_exists(__name__, resource):
raise IOError('Cannot find packaged regression method %s, but package '
'directory exists. Check the contents of %s if possible'
% (resource, path))
return pkg_resources.resource_filename(__name__, resource)
示例7: resource_string
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def resource_string(*args):
if len(args) == 0:
raise ValueError()
resource_path = os.path.join('resources', *args)
if not pkg_resources.resource_exists('designate', resource_path):
raise exceptions.ResourceNotFound('Could not find the requested '
'resource: %s' % resource_path)
return pkg_resources.resource_string('designate', resource_path)
示例8: test_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def test_resource(parent, basename, relative_to_module_name=__name__):
resource_test = lambda file_path: resource_exists(relative_to_module_name, file_path)
return _test_filelike(parent, basename, resource_test)
示例9: _get_version
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def _get_version():
import pkg_resources, codecs
if not pkg_resources.resource_exists(__name__, '_version'):
return '0.0.0'
with pkg_resources.resource_stream(__name__, '_version') as fp:
return codecs.getreader('utf-8')(fp).read().strip()
示例10: has_manifest
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def has_manifest(app, filename='manifest.json'):
'''Verify the existance of a JSON assets manifest'''
try:
return pkg_resources.resource_exists(app, filename)
except (ImportError, NotImplementedError):
return os.path.isabs(filename) and os.path.exists(filename)
示例11: all
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def all(cls):
def is_runtime(name):
return resource_exists("runtimes/{}/runtime.yml".format(name))
names = resource_listdir(__name__, "runtimes")
names = [is_runtime(name) for name in names]
return [cls.load(name) for name in names]
示例12: test_pr_resources
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [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")
示例13: _make_player
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def _make_player(file_name):
try:
return game.Player(file_name=file_name)
except IOError as msg:
if pkg_resources.resource_exists('rgkit', file_name):
bot_filename = pkg_resources.resource_filename('rgkit',
file_name)
return game.Player(file_name=bot_filename)
raise IOError(msg)
示例14: _readFromBinary
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def _readFromBinary(framework, frameworks_dir, platform, dst):
script_path = os.path.join("specifications/frameworks",
framework, platform, "build.sh")
if not pkg_resources.resource_exists("aibench", script_path):
raise Exception(
"cannot find the build script in the binary under {}.".format(script_path))
raw_build_script = pkg_resources.resource_string("aibench", script_path)
if not os.path.exists(os.path.dirname(dst)):
os.makedirs(os.path.dirname(dst))
with open(os.path.join(os.path.dirname(dst), "build.sh"), "w") as f:
f.write(raw_build_script.decode("utf-8"))
build_script = f.name
return build_script
示例15: getMeta
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_exists [as 別名]
def getMeta(args, platform):
meta = None
if not args.frameworks_dir:
meta_file = os.path.join(
"specifications/frameworks", args.framework, platform, "meta.json"
)
if "aibench" in sys.modules and pkg_resources.resource_exists(
"aibench", meta_file
):
meta = json.loads(pkg_resources.resource_string("aibench", meta_file))
return meta
else:
# look for files in the old default place
old_default = str(
os.path.dirname(os.path.realpath(__file__))
+ "/../../specifications/frameworks"
)
meta_file = os.path.join(old_default, args.framework, platform, "meta.json")
else:
meta_file = os.path.join(
args.frameworks_dir, args.framework, platform, "meta.json"
)
if os.path.isfile(meta_file):
with open(meta_file, "r") as f:
meta = json.load(f)
return meta