本文整理汇总了Python中pkg_resources.ResourceManager方法的典型用法代码示例。如果您正苦于以下问题:Python pkg_resources.ResourceManager方法的具体用法?Python pkg_resources.ResourceManager怎么用?Python pkg_resources.ResourceManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pkg_resources
的用法示例。
在下文中一共展示了pkg_resources.ResourceManager方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_resource_filename_rewrites_on_change
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def test_resource_filename_rewrites_on_change(self):
"""
If a previous call to get_resource_filename has saved the file, but
the file has been subsequently mutated with different file of the
same size and modification time, it should not be overwritten on a
subsequent call to get_resource_filename.
"""
import mod
manager = pkg_resources.ResourceManager()
zp = pkg_resources.ZipProvider(mod)
filename = zp.get_resource_filename(manager, 'data.dat')
actual = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
assert actual == self.ref_time
f = open(filename, 'w')
f.write('hello, world?')
f.close()
ts = timestamp(self.ref_time)
os.utime(filename, (ts, ts))
filename = zp.get_resource_filename(manager, 'data.dat')
with open(filename) as f:
assert f.read() == 'hello, world!'
manager.cleanup_resources()
示例2: test_resource_filename_rewrites_on_change
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def test_resource_filename_rewrites_on_change(self):
"""
If a previous call to get_resource_filename has saved the file, but
the file has been subsequently mutated with different file of the
same size and modification time, it should not be overwritten on a
subsequent call to get_resource_filename.
"""
import mod
manager = pkg_resources.ResourceManager()
zp = pkg_resources.ZipProvider(mod)
filename = zp.get_resource_filename(manager, 'data.dat')
actual = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
assert actual == self.ref_time
f = open(filename, 'w')
f.write('hello, world?')
f.close()
ts = timestamp(self.ref_time)
os.utime(filename, (ts, ts))
filename = zp.get_resource_filename(manager, 'data.dat')
f = open(filename)
assert f.read() == 'hello, world!'
manager.cleanup_resources()
示例3: get_package_loader
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def get_package_loader(self, package, package_path):
from pkg_resources import DefaultProvider, ResourceManager, get_provider
loadtime = datetime.utcnow()
provider = get_provider(package)
manager = ResourceManager()
filesystem_bound = isinstance(provider, DefaultProvider)
def loader(path):
if path is None:
return None, None
path = posixpath.join(package_path, path)
if not provider.has_resource(path):
return None, None
basename = posixpath.basename(path)
if filesystem_bound:
return (
basename,
self._opener(provider.get_resource_filename(manager, path)),
)
s = provider.get_resource_string(manager, path)
return basename, lambda: (BytesIO(s), loadtime, len(s))
return loader
示例4: __init__
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def __init__(self, egg_or_spec, resource_name, manager=None, root_resource=None):
if pkg_resources is None:
raise NotImplementedError("This class requires pkg_resources.")
if isinstance(egg_or_spec, (str, unicode)):
self.egg = pkg_resources.get_distribution(egg_or_spec)
else:
self.egg = egg_or_spec
self.resource_name = resource_name
if manager is None:
manager = pkg_resources.ResourceManager()
self.manager = manager
if root_resource is None:
root_resource = resource_name
self.root_resource = os.path.normpath(root_resource)
示例5: test_get_cache_path
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def test_get_cache_path(self):
mgr = pkg_resources.ResourceManager()
path = mgr.get_cache_path('foo')
type_ = str(type(path))
message = "Unexpected type from get_cache_path: " + type_
assert isinstance(path, (unicode, str)), message
示例6: __init__
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def __init__(self, package_name, package_path="templates", encoding="utf-8"):
from pkg_resources import DefaultProvider
from pkg_resources import get_provider
from pkg_resources import ResourceManager
provider = get_provider(package_name)
self.encoding = encoding
self.manager = ResourceManager()
self.filesystem_bound = isinstance(provider, DefaultProvider)
self.provider = provider
self.package_path = package_path
示例7: installed_location
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def installed_location(filename):
"""
Returns the full path for the given installed file or None if not found.
:param filename: The filename to search for.
:returns: The absolute filepath for the file or None if not installed.
"""
try:
return ResourceManager().resource_filename(Requirement.parse("websnort"), filename)
except DistributionNotFound:
return None
示例8: test_get_cache_path
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def test_get_cache_path(self):
mgr = pkg_resources.ResourceManager()
path = mgr.get_cache_path('foo')
type_ = str(type(path))
message = "Unexpected type from get_cache_path: " + type_
assert isinstance(path, string_types), message
示例9: test_get_cache_path_race
# 需要导入模块: import pkg_resources [as 别名]
# 或者: from pkg_resources import ResourceManager [as 别名]
def test_get_cache_path_race(self, tmpdir):
# Patch to os.path.isdir to create a race condition
def patched_isdir(dirname, unpatched_isdir=pkg_resources.isdir):
patched_isdir.dirnames.append(dirname)
was_dir = unpatched_isdir(dirname)
if not was_dir:
os.makedirs(dirname)
return was_dir
patched_isdir.dirnames = []
# Get a cache path with a "race condition"
mgr = pkg_resources.ResourceManager()
mgr.set_extraction_path(str(tmpdir))
archive_name = os.sep.join(('foo', 'bar', 'baz'))
with mock.patch.object(pkg_resources, 'isdir', new=patched_isdir):
mgr.get_cache_path(archive_name)
# Because this test relies on the implementation details of this
# function, these assertions are a sentinel to ensure that the
# test suite will not fail silently if the implementation changes.
called_dirnames = patched_isdir.dirnames
assert len(called_dirnames) == 2
assert called_dirnames[0].split(os.sep)[-2:] == ['foo', 'bar']
assert called_dirnames[1].split(os.sep)[-1:] == ['foo']