我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pkg_resources.resource_stream()。
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)
def __init__(self, cli_args): """Load config from commandline arguments""" self.cli_args = cli_args self.set_logging() # Don't even bother loading the config file if it's just a version query if cli_args.version: print("Dactyl version %s" % __version__) exit(0) self.bypass_errors = cli_args.bypass_errors # Start with the default config, then overwrite later self.config = yaml.load(resource_stream(__name__, "default-config.yml")) self.filters = {} if cli_args.config: self.load_config_from_file(cli_args.config) else: logger.debug("No config file specified, trying ./dactyl-config.yml") self.load_config_from_file(DEFAULT_CONFIG_FILE) self.load_filters()
def get_es_template(self, filename): """Loads an ElasticSearch template (as JSON)""" template_path = os.path.join(self.config["template_path"], filename) try: with open(template_path) as f: es_template = json.load(f) except (FileNotFoundError, json.decoder.JSONDecodeError) as e: if type(e) == FileNotFoundError: logger.debug("Didn't find ES template (%s), falling back to default" % template_path) elif type(e) == json.decoder.JSONDecodeError: recoverable_error(("Error JSON-decoding ES template (%s)" % template_path), self.bypass_errors) with resource_stream(__name__, BUILTIN_ES_TEMPLATE) as f: es_template = json.load(f) return es_template
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')
def load_model(package_name, model_dir, model_name): model_path = model_dir + "/" + model_name model = {} for f in pkg_resources.resource_listdir(package_name, model_path): f = model_path + '/' + f with pkg_resources.resource_stream(package_name, f) as fd: append_model(model, yaml.safe_load(fd)) imports_path = model.get('imports') if imports_path: f = model_dir + '/' + imports_path with pkg_resources.resource_stream(package_name, f) as fd: append_model(model, yaml.safe_load(fd)) extend_base_objects(model) extend_api_objects(model) return model # Singleton generator
def _open_asset_path(path, encoding=None): """ :param asset_path: string containing absolute path to file, or package-relative path using format ``"python.module:relative/file/path"``. :returns: filehandle opened in 'rb' mode (unless encoding explicitly specified) """ if encoding: return codecs.getreader(encoding)(_open_asset_path(path)) if os.path.isabs(path): return open(path, "rb") package, sep, subpath = path.partition(":") if not sep: raise ValueError("asset path must be absolute file path " "or use 'pkg.name:sub/path' format: %r" % (path,)) return pkg_resources.resource_stream(package, subpath) #: type aliases
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')
def prepare_packer_template(config, template_name): """ Prepares a packer template JSON file according to configuration and writes it into a temporary location where packer later expects it. Uses jinja2 template syntax to generate the resulting JSON file. Templates are in templates/ and snippets in templates/snippets/. """ try: template_fd = resource_stream(__name__, 'templates/{}.json'.format(template_name)) except FileNotFoundError: print("Template doesn't exist: {}".format(template_name)) sys.exit(2) filepath = resource_filename(__name__, 'templates/') env = Environment(loader=FileSystemLoader(filepath), autoescape=False, trim_blocks=True, lstrip_blocks=True) template = env.get_template("{}.json".format(template_name)) # write to temporary file f = create_cachefd('{}.json'.format(template_name)) f.write(template.render(config)) # pylint: disable=no-member f.close() return f.name
def open_resource(self, 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')
def schema_from_json(path): """ Create a pyspark schema from the json representation. The json representation must be from a StructType. This can be generated from any StructType using the `.json()` method. The schema for a dataframe can be obtained using the `.schema` accessor. For example, to generate the json from the `topline_summary`, run the following in the pyspark repl: >>> path = 's3a://telemetry-parquet/topline_summary/v1/mode=weekly' >>> json_data = spark.read.parquet(path).schema.json() :path str: Path the the json data """ with pkg_resources.resource_stream(mozetl.topline.__name__, path) as f: data = json.load(f) return StructType.fromJson(data) # Generate module level schemas
def _transform_to_html(self, content, xslt_package=None, xslt_path=None): xslt_package = xslt_package or __name__ xslt_path = xslt_path or \ '../templates/ckanext/spatial/gemini2-html-stylesheet.xsl' # optimise -- read transform only once and compile rather # than at each request with resource_stream(xslt_package, xslt_path) as style: style_xml = etree.parse(style) transformer = etree.XSLT(style_xml) xml = etree.parse(StringIO(content.encode('utf-8'))) html = transformer(xml) response.headers['Content-Type'] = 'text/html; charset=utf-8' response.headers['Content-Length'] = len(content) result = etree.tostring(html, pretty_print=True) return result
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')
def create_blank_config(conda_fp, project_fp, template="default"): if template=="microb120": template_stream = pkg_resources.resource_stream( "sunbeamlib", "data/microb120_config.yml") elif template=="microb191": template_stream = pkg_resources.resource_stream( "sunbeamlib", "data/microb191_config.yml") elif template=="pmacs": template_stream = pkg_resources.resource_stream( "sunbeamlib", "data/pmacs_config.yml") elif template=="respublica": template_stream = pkg_resources.resource_stream( "sunbeamlib", "data/default_config.yml") else: sys.stderr.write("Using default config template...\n") template_stream = pkg_resources.resource_stream( "sunbeamlib", "data/default_config.yml") return template_stream.read().decode().format( CONDA_FP=conda_fp, PROJECT_FP=project_fp)
def read_from_egg(tfile): '''Read a relative path, getting the contents locally or from the installed egg, parsing the contents based on file_type if given, such as yaml Params: tfile: relative package path file_type: file extension such as "json" or "yaml" or None Returns: contents: yaml or json loaded or raw ''' template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), tfile) if not os.path.exists(template_path): path_in_egg = os.path.join("btax", tfile) buf = resource_stream(Requirement.parse("btax"), path_in_egg) _bytes = buf.read() contents = str(_bytes) else: with open(template_path, 'r') as f: contents = f.read() return contents
def test_resource_api(easter_fixture): test_file = NamedTemporaryFile(mode='wb+') dirname, file_name = os.path.split(test_file.name) easter_fixture.stub_egg.location = dirname easter_fixture.stub_egg.activate() assert pkg_resources.resource_exists(easter_fixture.stub_egg.as_requirement(), file_name) assert not pkg_resources.resource_exists(easter_fixture.stub_egg.as_requirement(), 'IDoNotExist') contents = b'asdd ' test_file.write(contents) test_file.flush() as_string = pkg_resources.resource_string(easter_fixture.stub_egg.as_requirement(), file_name) assert as_string == contents as_file = pkg_resources.resource_stream(easter_fixture.stub_egg.as_requirement(), file_name) assert as_file.read() == contents
def setup_output_path(self): """ Called on task startup to copy all static resources into the output path (and to make sure the output path exists as a directory). """ self.logger.info('setting up output path') try: self.output_path.mkdir() except FileExistsError: pass try: (self.output_path / 'simple').mkdir() except FileExistsError: pass for filename in resource_listdir(__name__, 'static'): with (self.output_path / filename).open('wb') as f: source = resource_stream(__name__, 'static/' + filename) f.write(source.read()) source.close()
def _read_data( filename, key_conv=str, value_conv=float, key_col=0, value_col=1): result = {} cols = None for count, line in enumerate( pkg.resource_stream(__name__, filename), start=1): data = _parse_re.match(line.decode('utf-8')).group('data') if data: data = data.split() try: if cols is None: cols = len(data) elif len(data) != cols: raise ValueError( 'Unexpected number of values (expected %d)' % cols) key = key_conv(data[key_col]) value = value_conv(data[value_col]) if key in result: raise ValueError( 'Duplicate definition for group %s' % key) result[key] = value except (IndexError, ValueError) as e: e.args += ('on line %d of %s' % (count, filename),) raise return result
def setUp(self): """ Loads the test xml json and provn data """ self.test_resources = { 'xml': {'package': 'provdbconnector', 'file': '/tests/resources/primer.provx'}, 'json': {'package': 'provdbconnector', 'file': '/tests/resources/primer.json'}, 'provn': {'package': 'provdbconnector', 'file': '/tests/resources/primer.provn'} } self.test_prov_files = dict((key, pkg_resources.resource_stream(val['package'], val['file'])) for key, val in self.test_resources.items()) self.auth_info = {"user_name": NEO4J_USER, "user_password": NEO4J_PASS, "host": NEO4J_HOST + ":" + NEO4J_BOLT_PORT } self.provapi = ProvDb(api_id=1, adapter=Neo4jAdapter, auth_info=self.auth_info)
def __ensure_config_setup(cls): root = cls.__default_settings_root() if not os.path.exists(root): os.makedirs(root) settings_file = os.path.join(root, 'settings.conf') if not os.path.exists(settings_file): with open(settings_file, 'w+b') as fh: content = resource_stream("vpnporthole", "resources/settings.conf").read() fh.write(content) print("* Wrote: %s" % settings_file) root = os.path.join(root, 'profiles') if not os.path.exists(root): os.makedirs(root) profile_file = os.path.join(root, 'example.conf') if not os.path.exists(profile_file): with open(profile_file, 'w+b') as fh: content = resource_stream("vpnporthole", "resources/example.conf").read() fh.write(content) print("* Wrote: %s" % profile_file)
def open_resource(self, resource): """Opens a resource from the application's resource folder. To see how this works, consider the following folder structure:: /myapplication.py /schemal.sql /static /style.css /templates /layout.html /index.html If you want to open the `schema.sql` file you would do the following:: with app.open_resource('schema.sql') as f: contents = f.read() do_something_with(contents) :param resource: the name of the resource. To access resources within subfolders use forward slashes as separator. """ if pkg_resources is None: return open(os.path.join(self.root_path, resource), 'rb') return pkg_resources.resource_stream(self.import_name, resource)
def test_get_parameter_list(self, m): response = StringIO(BytesIO(pkg_resources.resource_string(__name__, 'test_data/bom_parameter_list.request')).read().decode('UTF-8')).read() m.get( 'http://www.bom.gov.au/waterdata/services?station_no=410730&type=QueryServices&service=kisters&format=json&request=getParameterList', text = response ) expected = pd.read_csv( pkg_resources.resource_stream( __name__, 'test_data/bom_parameter_list.csv' ) ) df = self.k.get_parameter_list(station_no = '410730') expected.equals(df)