本文整理匯總了Python中pkg_resources.resource_stream方法的典型用法代碼示例。如果您正苦於以下問題:Python pkg_resources.resource_stream方法的具體用法?Python pkg_resources.resource_stream怎麽用?Python pkg_resources.resource_stream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pkg_resources
的用法示例。
在下文中一共展示了pkg_resources.resource_stream方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: open_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def open_resource(name):
"""Open a resource from the zoneinfo subdir for reading.
Uses the pkg_resources module if available and no standard file
found at the calculated location.
"""
name_parts = name.lstrip('/').split('/')
for part in name_parts:
if part == os.path.pardir or os.path.sep in part:
raise ValueError('Bad path segment: %r' % part)
filename = os.path.join(os.path.dirname(__file__),
'zoneinfo', *name_parts)
if not os.path.exists(filename) and resource_stream is not None:
# http://bugs.launchpad.net/bugs/383171 - we avoid using this
# unless absolutely necessary to help when a broken version of
# pkg_resources is installed.
return resource_stream(__name__, 'zoneinfo/' + name)
return open(filename, 'rb')
示例2: test_post_upload_list
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def test_post_upload_list(db, client, token):
"""
Test uploading a report.
"""
count_1 = db.session.query(models.Upload).count()
rv = client.post(
"/rest_api/v1/uploads",
data={"report": resource_stream("tests", "multiqc_data.json")},
headers={
"access_token": token,
"Content-Type": "multipart/form-data",
"Accept": "application/json",
},
)
# Check the request was successful
assert rv.status_code == 201, rv.json
# Validate the response
schemas.UploadSchema().validate(rv.json)
# Check that there is a new Upload
count_2 = db.session.query(models.Upload).count()
assert count_2 == count_1 + 1
示例3: open_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def open_resource(name):
"""Open a resource from the zoneinfo subdir for reading.
Uses the pkg_resources module if available and no standard file
found at the calculated location.
"""
name_parts = name.lstrip('/').split('/')
for part in name_parts:
if part == os.path.pardir or os.path.sep in part:
raise ValueError('Bad path segment: %r' % part)
filename = os.path.join(os.path.dirname(__file__),
'zoneinfo', *name_parts)
if not os.path.exists(filename):
# http://bugs.launchpad.net/bugs/383171 - we avoid using this
# unless absolutely necessary to help when a broken version of
# pkg_resources is installed.
try:
from pkg_resources import resource_stream
except ImportError:
resource_stream = None
if resource_stream is not None:
return resource_stream(__name__, 'zoneinfo/' + name)
return open(filename, 'rb')
示例4: _cache_tlds
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def _cache_tlds(self, tlds):
'''Logs a diff of the new TLDs and caches them on disk, according to
settings passed to __init__.'''
if LOG.isEnabledFor(logging.DEBUG):
import difflib
snapshot_stream = pkg_resources.resource_stream(__name__, '.tld_set_snapshot')
with closing(snapshot_stream) as snapshot_file:
snapshot = sorted(
json.loads(snapshot_file.read().decode('utf-8'))
)
new = sorted(tlds)
LOG.debug('computed TLD diff:\n' + '\n'.join(difflib.unified_diff(
snapshot,
new,
fromfile=".tld_set_snapshot",
tofile=self.cache_file
)))
if self.cache_file:
try:
with open(self.cache_file, 'w') as cache_file:
json.dump(tlds, cache_file)
except IOError as ioe:
LOG.warn("unable to cache TLDs in file %s: %s", self.cache_file, ioe)
示例5: test_no_schema_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def test_no_schema_resource(self, tmpdir, caplog):
class FakeProvider(object):
def get_resource_stream(self, pkg, rsc):
raise IOError
# pkg_resources.resource_stream() cannot be mocked directly
# Instead mock the module-level function it calls.
(flexmock(pkg_resources)
.should_receive('get_provider')
.and_return(FakeProvider()))
filename = os.path.join(str(tmpdir), 'config.yaml')
with open(filename, 'w'):
pass
tasker, workflow = self.prepare()
plugin = ReactorConfigPlugin(tasker, workflow, config_path=str(tmpdir))
with caplog.at_level(logging.ERROR), pytest.raises(Exception):
plugin.run()
captured_errs = [x.message for x in caplog.records]
assert "unable to extract JSON schema, cannot validate" in captured_errs
示例6: test_invalid_schema_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def test_invalid_schema_resource(self, tmpdir, caplog, schema):
class FakeProvider(object):
def get_resource_stream(self, pkg, rsc):
return io.BufferedReader(io.BytesIO(schema))
# pkg_resources.resource_stream() cannot be mocked directly
# Instead mock the module-level function it calls.
(flexmock(pkg_resources)
.should_receive('get_provider')
.and_return(FakeProvider()))
filename = os.path.join(str(tmpdir), 'config.yaml')
with open(filename, 'w'):
pass
tasker, workflow = self.prepare()
plugin = ReactorConfigPlugin(tasker, workflow, config_path=str(tmpdir))
with caplog.at_level(logging.ERROR), pytest.raises(Exception):
plugin.run()
captured_errs = [x.message for x in caplog.records]
assert any("cannot validate" in x for x in captured_errs)
示例7: __open_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def __open_resource(name):
"""Open a resource from the zoneinfo subdir for reading.
Uses the pkg_resources module if available and no standard file
found at the calculated location.
"""
name_parts = name.lstrip('/').split('/')
for part in name_parts:
if part == os.path.pardir or os.path.sep in part:
raise ValueError('Bad path segment: %r' % part)
filename = os.path.join(os.path.dirname(__file__),
'zoneinfo', *name_parts)
if not os.path.exists(filename) and resource_stream is not None:
# http://bugs.launchpad.net/bugs/383171 - we avoid using this
# unless absolutely necessary to help when a broken version of
# pkg_resources is installed.
return resource_stream(__name__, 'zoneinfo/' + name)
return open(filename, 'rb')
示例8: test_read_yaml_file_bad_extract
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def test_read_yaml_file_bad_extract(tmpdir, caplog):
class FakeProvider(object):
def get_resource_stream(self, pkg, rsc):
raise IOError
# pkg_resources.resource_stream() cannot be mocked directly
# Instead mock the module-level function it calls.
(flexmock(pkg_resources)
.should_receive('get_provider')
.and_return(FakeProvider()))
config_path = os.path.join(str(tmpdir), 'config.yaml')
with open(config_path, 'w'):
pass
with pytest.raises(IOError):
read_yaml_from_file_path(config_path, 'schemas/container.json')
assert "unable to extract JSON schema, cannot validate" in caplog.text
示例9: schematron
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def schematron(cls, schema):
transforms = [
"xml/schematron/iso_dsdl_include.xsl",
"xml/schematron/iso_abstract_expand.xsl",
"xml/schematron/iso_svrl_for_xslt1.xsl",
]
if isinstance(schema, file):
compiled = etree.parse(schema)
else:
compiled = schema
for filename in transforms:
with resource_stream(
__name__, filename) as stream:
xform_xml = etree.parse(stream)
xform = etree.XSLT(xform_xml)
compiled = xform(compiled)
return etree.XSLT(compiled)
示例10: load_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def load_resource(path: str, encoding: str = None) -> TextIO:
"""
Open a resource file located in a python package or the local filesystem.
Args:
path: The resource path in the form of `dir/file` or `package:dir/file`
Returns:
A file-like object representing the resource
"""
components = path.rsplit(":", 1)
try:
if len(components) == 1:
return open(components[0], encoding=encoding)
else:
bstream = pkg_resources.resource_stream(components[0], components[1])
if encoding:
return TextIOWrapper(bstream, encoding=encoding)
return bstream
except IOError:
pass
示例11: __init__
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def __init__(self):
resource_package = __name__
yelp_acc_path = 'acc_yelp.bin'
yelp_ppl_path = 'ppl_yelp.binary'
yelp_ref0_path = 'yelp.refs.0'
yelp_ref1_path = 'yelp.refs.1'
yelp_acc_file = pkg_resources.resource_stream(resource_package, yelp_acc_path)
yelp_ppl_file = pkg_resources.resource_stream(resource_package, yelp_ppl_path)
yelp_ref0_file = pkg_resources.resource_stream(resource_package, yelp_ref0_path)
yelp_ref1_file = pkg_resources.resource_stream(resource_package, yelp_ref1_path)
self.yelp_ref = []
with open(yelp_ref0_file.name, 'r') as fin:
self.yelp_ref.append(fin.readlines())
with open(yelp_ref1_file.name, 'r') as fin:
self.yelp_ref.append(fin.readlines())
self.classifier_yelp = fasttext.load_model(yelp_acc_file.name)
self.yelp_ppl_model = kenlm.Model(yelp_ppl_file.name)
示例12: run
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def run(self):
# XXX: do this once only
fd = pkg_resources.resource_stream ('crocoite', 'data/click.yaml')
config = list (yaml.safe_load_all (fd))
l = nodes.definition_list ()
for site in config:
urls = set ()
v = nodes.definition ()
vl = nodes.bullet_list ()
v += vl
for s in site['selector']:
i = nodes.list_item ()
i += nodes.paragraph (text=s['description'])
vl += i
urls.update (map (lambda x: URL(x).with_path ('/'), s.get ('urls', [])))
item = nodes.definition_list_item ()
term = ', '.join (map (lambda x: x.host, urls)) if urls else site['match']
k = nodes.term (text=term)
item += k
item += v
l += item
return [l]
示例13: ensure_builder_units
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def ensure_builder_units():
gallery_builder_service = 'tljh-voila-gallery-builder.service'
with resource_stream(__name__, f'./systemd-units/{gallery_builder_service}') as f:
builder_unit_template = f.read().decode('utf-8')
gallery_builder_timer = 'tljh-voila-gallery-builder.timer'
with resource_stream(__name__, f'./systemd-units/{gallery_builder_timer}') as f:
builder_timer_template = f.read().decode('utf-8')
unit_params = dict(
python_interpreter_path=sys.executable,
)
systemd.install_unit(gallery_builder_service, builder_unit_template.format(**unit_params))
systemd.install_unit(gallery_builder_timer, builder_timer_template.format(**unit_params))
for unit in [gallery_builder_service, gallery_builder_timer]:
systemd.restart_service(unit)
systemd.enable_service(unit)
systemd.reload_daemon()
示例14: load
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def load() -> None:
global spdx_list
if spdx_list is not None:
return
with pkg_resources.resource_stream("maubot.cli", "res/spdx.json.zip") as disk_file:
with zipfile.ZipFile(disk_file) as zip_file:
with zip_file.open("spdx.json") as file:
spdx_list = json.load(file)
示例15: open_resource
# 需要導入模塊: import pkg_resources [as 別名]
# 或者: from pkg_resources import resource_stream [as 別名]
def open_resource(name):
"""Open a resource from the zoneinfo subdir for reading.
Uses the pkg_resources module if available and no standard file
found at the calculated location.
It is possible to specify different location for zoneinfo
subdir by using the PYTZ_TZDATADIR environment variable.
"""
name_parts = name.lstrip('/').split('/')
for part in name_parts:
if part == os.path.pardir or os.path.sep in part:
raise ValueError('Bad path segment: %r' % part)
zoneinfo_dir = os.environ.get('PYTZ_TZDATADIR', None)
if zoneinfo_dir is not None:
filename = os.path.join(zoneinfo_dir, *name_parts)
else:
filename = os.path.join(os.path.dirname(__file__),
'zoneinfo', *name_parts)
if not os.path.exists(filename):
# http://bugs.launchpad.net/bugs/383171 - we avoid using this
# unless absolutely necessary to help when a broken version of
# pkg_resources is installed.
try:
from pkg_resources import resource_stream
except ImportError:
resource_stream = None
if resource_stream is not None:
return resource_stream(__name__, 'zoneinfo/' + name)
return open(filename, 'rb')