Skip to main content

media-type-version - extract the format version from a media type string

[Home | GitLab | PyPI | Python API | ReadTheDocs]

Overview

The media-type-version library is designed to be used as the first step in parsing structured data, e.g. configuration files, serialized classes, etc. The caller extracts the media type string (e.g. a JSON "mediaType": "..." key) and passes it in for parsing. The caller then decides what to do with the extracted version information - is this version supported, what fields are expected to be there, should any extraneous fields produce errors, and so on.

The media type string is expected to be in a <prefix>.vX.Y<suffix> format, with a fixed prefix and suffix. The prefix will usually be a vendor-specific media type. The version part consists of two unsigned integer numbers. The suffix, if used, may correspond to the file format. A sample media type string identifying a TOML configuration file for a text-processing program could be vnd.ringlet.textproc.publync.config/publync.v0.2+toml

The library

The media-type-version library provides a single function, extract(), that parses a media type string, strips the specified prefix and suffix, and looks for a .vX.Y version string left. It then returns the (X, Y) version tuple.

Python example:

mtv_cfg: Final = media_type_version.Config(
    log=logging.Logger(...),
    prefix="vnd.acme/thing",
    suffix="+toml",
)

ver_major, ver_minor = media_type_version.extract(mtv_cfg, "vnd.acme/thing.v3.12+toml")

Rust example:

use media_type_version::{Config as MTVConfig, Error as MTVError, Version as MTVersion};

let cfg = MTVConfig::builder()
    .prefix("vnd.acme/thing")
    .suffix("+toml")
    .build()
    .map_err(MTVError::into_owned_error)?;
assert_eq!(
    media_type_version::extract(&cfg, "vnd.acme/thing.v3.12+toml").as_tuple(),
    (3, 12)
);

The mtv-extract tool

The media-type-version library also provides a command-line tool called mtv-extract that will read a series of strings from the specified files, parse them as media-type strings with the specified prefix and suffix, and output a line consisting of two tab-separated numbers for each parsed string:

$ { echo vnd.acme/thing.v3.47; echo vnd.acme/thing.v42.616; } | mtv-extract -q -p vnd.acme/thing -- -
3       47
42      616
$

The mtv-extract tool supports the following command-line options:

  • -p prefix (required): the prefix to strip from the media type string
  • -q: quiet operation; only display warnings and error messages
  • -s suffix: the optional suffix to strip from the media type string
  • -v: verbose operation; display diagnostic output

Contact

The media-type-version library was written by Peter Pentchev. It is developed in a GitLab repository. This documentation is hosted at Ringlet with a copy at ReadTheDocs.

Metadata

Release files for media-type-version 0.1.3

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

Source distribution (sdist)

Source distribution for media-type-version 0.1.3
File Size Uploaded
media_type_version-0.1.3.tar.gz 55.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for media-type-version 0.1.3
File Interpreter ABI Platform
media_type_version-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 63.7 kB

Release files / media_type_version-0.1.3.tar.gz

Download URL media_type_version-0.1.3.tar.gz
Size 55.1 kB
Tags Source
SHA-256 checksum
How to use checksums
85acc52be8a48656e6a5de41f460a0e3a2ad94d866dc01572919c94372afbcf3
BLAKE2b-256 checksum
How to use checksums
711161a4e73fd47c607de260d7167a73a403ca272a8a6bcf9f312c8e8c641fe8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / media_type_version-0.1.3-py3-none-any.whl

Download URL media_type_version-0.1.3-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d98e21080ef25cc1977fb2222bc34252e134cdb1541d773c80154091b0770081
BLAKE2b-256 checksum
How to use checksums
79a442660d878d974ac73002aa0597fb55b5701cc3efb9d55edc580cb4806e44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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