Skip to main content

microversion_parse

A small set of functions to manage OpenStack microversion headers that can be used in middleware, application handlers and decorators to effectively manage microversions.

Also included, in the middleware module, is a MicroversionMiddleware that will process incoming microversion headers.

Version

A namedtuple with integer major and minor fields. Versions can be compared with each other or with (major, minor) tuples, and str(version) returns the 'major.minor' representation:

version = microversion_parse.Version(2, 10)
assert version.major == 2
assert version > (2, 9)
assert str(version) == '2.10'
assert version.matches(min_version=(2, 1), max_version=(2, 20))

matches checks an inclusive range. Omitted bounds use the instance’s min_version and max_version attributes, which extract_version sets from the supported versions list. Otherwise, both attributes default to (-1, 0).

VersionRange

An inclusive range of concrete Version objects, useful for matching schema or handler version bounds:

bounds = microversion_parse.VersionRange(
    minimum=microversion_parse.parse_version_string('2.9'),
    maximum=microversion_parse.parse_version_string('2.10'),
)
microversion_parse.Version(2, 9) in bounds  # True
microversion_parse.Version(2, 10) in bounds  # True
microversion_parse.Version(2, 11) in bounds  # False

Either bound can be omitted or set to None for an unbounded end within that major API version. For example, a range with minimum Version(2, 0) and no maximum includes any 2.x version, but not Version(3, 0). Explicit bounds must use the same major API version.

At least one bound must be provided; omitting both raises ValueError. Unlike Version.matches(), ranges never use the min_version or max_version service bounds stored on version objects. Existing Version.matches() behavior is unchanged.

The overlaps(other) method checks whether two ranges share any version. Sharing an inclusive endpoint counts as overlap:

newer = microversion_parse.VersionRange(
    minimum=microversion_parse.parse_version_string('2.10'),
)
bounds.overlaps(newer)  # True: both contain 2.10

Ranges with different major API versions are always disjoint: overlaps() returns False and intersection (&) returns None.

get_version

A simple parser for OpenStack microversion headers:

import microversion_parse

# headers is a dict of headers with folded (comma-separated
# values) or a list of header, value tuples
version = microversion_parse.get_version(
    headers, service_type='compute',
    legacy_headers=['x-openstack-nova-api-version']
)

# If headers are not already available, a dict of headers
# can be extracted from the WSGI environ
headers = microversion_parse.headers_from_wsgi_environ(environ)
version = microversion_parse.get_version(
    headers, service_type='placement'
)

It processes microversion headers with the standard form:

OpenStack-API-Version: compute 2.1

In that case, the response will be ‘2.1’.

If provided with a legacy_headers argument, this is treated as a list of additional headers to check for microversions. Some examples of headers include:

OpenStack-telemetry-api-version: 2.1
OpenStack-nova-api-version: 2.1
X-OpenStack-nova-api-version: 2.1

If a version string cannot be found, None will be returned. If the input is incorrect usual Python exceptions (ValueError, TypeError) are allowed to raise to the caller.

parse_version_string

A function to turn a version string into a Version, a comparable namedtuple:

version_tuple = microversion_parse.parse_version_string('2.1')

If the provided string is not a valid microversion string, TypeError is raised.

extract_version

Combines get_version and parse_version_string to find and validate a microversion for a given service type in a collection of headers:

version_tuple = microversion_parse.extract_version(
    # a representation of headers, as accepted by get_version
    headers,
    # service type identify to match in headers
    service_type,
    # an ordered list of strings of version numbers that are the valid
    # versions presented by this service
    versions_list,
)

latest will be translated to whatever the max version is in versions_list.

If the found version is not in versions_list a ValueError is raised.

Note that extract_version does not support legacy_headers.

MicroversionMiddleware

A WSGI middleware that can wrap an application that needs to be microversion aware. The application will get a WSGI environ with a SERVICE_TYPE.microversion key that has a value of the microversion found at an openstack-api-version header that matches SERVICE_TYPE. If no header is found, the minimum microversion will be set. If the special keyword 'latest' is used, the maximum microversion will be set.

If the requested microversion is not available a HTTP 406 response is returned.

If there is an error parsing a provided header, a HTTP 400 response is returned.

Otherwise the application is called.

The middleware is configured when it is created. Three parameters are required:

app

The next WSGI middleware or application in the stack.

service_type

The service type of the application, used to identify microversion headers.

versions_list

An ordered list of legitimate microversions (as strings) for the application. It’s assumed that any application that is using microversions will have such a list for its own housekeeping and documentation.

One named parameter is optional:

json_error_formatter

A WebOb error formatter that can be used to structure the response when JSON is expected.

For example:

def app():
    app = middleware.MicroversionMiddleware(
        MyWSGIApp(), 'cats', ['1.0', '1.1', '1.2']
    )
    return app

Metadata

Release files for microversion-parse 2.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for microversion-parse 2.3.0
File Size Uploaded Provenance Actions
microversion_parse-2.3.0.tar.gz 25.5 kB Without provenance Details for microversion_parse-2.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for microversion-parse 2.3.0
File Interpreter ABI Platform Actions
microversion_parse-2.3.0-py3-none-any.whl Python 3 none any Details for microversion_parse-2.3.0-py3-none-any.whl

Total release size: 48.9 kB

Release files / microversion_parse-2.3.0.tar.gz

Download URL microversion_parse-2.3.0.tar.gz
Size 25.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c8c77ab68821aa084281b5aaf78692ce3fce82438fa79f15f764d56083191e12
BLAKE2b-256 checksum
How to use checksums
9145c7b4c4938672c14248aaf4dc238f8530281124647096b1609b93fe3e69df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / microversion_parse-2.3.0-py3-none-any.whl

Download URL microversion_parse-2.3.0-py3-none-any.whl
Size 23.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f091ced2f4d025daab2121fd78ffc2b6758ef4ce59ed09442923d29b21d9a388
BLAKE2b-256 checksum
How to use checksums
dd07f1b552843dcd4e977f0a37a9d963b4436ccaafde83188b3d021029e1608f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for microversion-parse 2.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page