Skip to main content

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

[Home | sourcehut | issues | PyPI | crates.io | 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 can be used to extract format versions from various sources.

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

  • -q: quiet operation; only display warnings and error messages
  • -v: verbose operation; display diagnostic output

The "features" subcommand

The features subcommand will display a single line of output starting with the prefix "Features: " and containing a space-separated list of "name=version" pairs. This output format is intended to be machine-readable, so that other programs may examine it using e.g. the feature-check library and command-line tool.

The "lines" subcommand

The lines subcommand 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 lines -p vnd.acme/thing -- -
3       47
42      616
$

The lines subcommand supports the following command-line options:

  • -p prefix (required): the prefix to strip from the media type string
  • -s suffix: the optional suffix to strip from the media type string

Supported features

The features subcommand of the mtv-extract tool, as well as the FEATURES constant in both the Python and Rust implementations, may currently list the following features:

media-type-version

The version of the media-type-version library itself.

cmd-features

0.1

The command-line tool supports the features subcommand with the output format described above.

cmd-lines

0.1

The command-line tool supports the lines subcommand with the mandatory prefix option and the optional suffix one. It requires at least one file name to read from, and it supports - for reading from the standard input stream.

extract

0.1

The library supports the extract() function. It accepts a Config parameter containing the prefix and suffix strings, and a string parameter to parse. The string must contain a ".vX.Y" version specification between the prefix and the suffix.

Contact

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

media_type_version-0.3.0.tar.gz (151.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

media_type_version-0.3.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file media_type_version-0.3.0.tar.gz.

File metadata

  • Download URL: media_type_version-0.3.0.tar.gz
  • Upload date:
  • Size: 151.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for media_type_version-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2f20f6d586ed4e8544c9e2892239d5079847c532492ede627c7c84fbe28c4acd
MD5 d45ddce5c5f0642eee404ca7a6db7c57
BLAKE2b-256 9b80883834eaf7f1e9e8a6159e8114e4e5525f88b599aef03bd4ac6549b527d5

See more details on using hashes here.

File details

Details for the file media_type_version-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for media_type_version-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06f91f37ebc0df31a2fe71f8ae3499953dc8293688de082e415f2606148e26e2
MD5 61908c67e5f052a44990d6a07d227c5a
BLAKE2b-256 d2a5b495a313b366feacabfa40fd65283fcfdef55727cda2f009dcd06e495bf8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 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