omnio
Python library for opening URIs as streaming file-like objects
This library provides the omnio.open() function with an interface very
similar to that of the built in Python open function. The difference is
that while Python's open operates only on local filesystem paths, omnio
accepts URIs as well.
It currently supports file, http, and s3 URIs, though it may be
expanded to support additional schemes in the future.
In addition, it supports compression and decompression of streams with gzip or bz2.
Features
- API is a superset of Python 3's built-in open() function
- Based on Python 3
iomodule - Gzip and bzip2 support both for reading and writing
- Local file support using standard library
- HTTP support using
requests - S3 support using
boto3
Examples
Read a local binary file:
>>> with omnio.open('example.bin', 'r') as f:
>>> data = f.read()
Read a local text file:
>>> with omnio.open('example.txt', 'rt') as f:
>>> text = f.read()
Read a text file from the web:
>>> with omnio.open('https://example.com/example.txt', 'rt') as f:
>>> text = f.read()
Read a gzipped text file from the web, uncompressing on the fly:
>>> with omnio.open('https://example.com/example.txt.gz', 'rtz') as f:
>>> text = f.read()
Read a text file from s3:
>>> with omnio.open('s3://my-bucket/my-key', 'rt') as f:
>>> text = f.read()
Write a megabyte of random data to s3, compressing with bzip2:
>>> import os
>>> data = os.urandom(1024**2)
>>> with omnio.open('s3://my-bucket/my-key', 'wbj') as f:
>>> f.write(data)
Read a bzip2 compressed csv file into a list of data rows:
>>> import csv
>>> with omnio.open('data/example_data.csv.bz2', 'rtj') as f:
>>> reader = csv.reader(f)
>>> data = list(reader)
API
omnio.open()
The top-level API provides of a single function intended to be
referenced as omnio.open(). This function API is designed to mimic
Python 3's built-in open() as much as possible, and should normally
be able to be used as a drop-in replacement.
Signature:
omnio.open(uri, mode='rb', encoding=None, errors=None, newline=None)
Returns:
A file-like object whose type depends on the scheme and the mode.
Parameters:
-
uri -- URI or local path. Supported URI schemes are
file,http, ands3. Local paths may be specified by as ordinary path strings. -
mode -- Optional string that specifies the mode in which the file is opened. It defaults to 'rb' which means open for reading in binary mode. Supported modes are documented below.
Modes:
| Character | Meaning |
|---|---|
| 'r' | open for reading (default) |
| 'w' | open for writing, truncating the file first |
| 'b' | binary mode (default) |
| 't' | text mode |
| 'z' | use gzip compression |
| 'j' | use bzip2 compression |
These characters can be composed into valid modes. Binary mode is always the default, so some mode specifications are equivalent. The complete list of supported modes are as follows:
| Mode | Meaning |
|---|---|
| 'r', 'rb' | read bytes |
| 'rt' | read and decode to unicode |
| 'rz', 'rbz' | read, decompressing gzip to bytes |
| 'rj', 'rbj' | read, decompressing bzip2 to bytes |
| 'rtz' | read, decompress gzip to bytes, decode to unicode |
| 'rtj' | read, decompress bzip2 to bytes, decode to unicode |
| 'w', 'wb' | write bytes |
| 'wt' | write unicode, encoding to bytes |
| 'wz', 'wbz' | write bytes, compress with gzip |
| 'wj', 'wbj' | write bytes, compress with bzip2 |
| 'wtz' | write unicode, encode to bytes, compress with gzip |
| 'wtj' | write unicode, encode to bytes, compress with bzip2 |
Some keyword arguments may be applicable to only certain modes. For
example, encoding only applies to 't' (text) modes.
Some schemes may not support some modes. For example, the http scheme currently does not support any 'w' (write) modes.
omnio.glob
The glob submodule is intended to be a drop-in replacement for the
standard library glob module. It provides the functions escape(),
glob(), and iglob() with the signature of each matching that of
the original.
The difference is that glob() and iglob() optionally accept URIs.
Currently, local paths, file://, and s3:// URIs are supported.
Configuration
The omnio.open function accepts an optional config parameter. This
allows for specifying scheme-specific configuration.
The default_config() method returns a config dictionary with all supported
keys defined along with their default values.
>>> import omnio
>>> omnio.default_config()
{'file': {}, 'http': {'content_iter_chunk_size': 512}, 's3': {'upload_part_size': 5242880, 'boto_client_config_args': [], 'boto_client_config_kwargs': {}}}
To specify alternate values for these parameters, instantiate a default
config, update the dict with the desired values and pass it as a keyword arg
to the omnio.open() function.
>>> config = omnio.default_config()
>>> config["s3"]["boto_client_config_kwargs"] = {"read_timeout": 600}
>>> with omnio.open("s3://my-bucket/my-key", "rt", config=config) as fd:
fd.read()
Metadata
Release files for omnio 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| omnio-1.3.0.tar.gz | 10.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| omnio-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.2 kB
Release files / omnio-1.3.0.tar.gz
| Download URL | omnio-1.3.0.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8cb59123f3ed9c86179e26d63d3c5c33918a8e3498516ff7821336a64fdcf10b
|
|
BLAKE2b-256 checksum How to use checksums |
ef3ba9e49748ae526750e278a8c494acb3458aac05dde67d2044d9063c25752c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.5 Darwin/21.5.0
|
Release files / omnio-1.3.0-py3-none-any.whl
| Download URL | omnio-1.3.0-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3251a572a43437077f2b3a4003b9da5e031dd57e185c7f187467de80ffa6032b
|
|
BLAKE2b-256 checksum How to use checksums |
e8a98e495e002669563596d44b8f6fcaeeb414a4e87ce7980dea98e87d4edcde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.5 Darwin/21.5.0
|
Security for omnio 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.