Skip to main content

Minimal, strict Data URL handling for Python

Project description

durl-py

Minimal, strict Data URL handling for Python.

durl-py is a small RFC 2397 library centered on one core type: DURL. It parses existing data: URLs, builds new ones, preserves round-trip serialization, and fails loudly on malformed input.

Installation

pip install durl-py

API

The public API is intentionally small:

from durl import DURL

Core properties:

  • mime_type: str | None
  • parameters: Mapping[str, str]
  • is_base64: bool
  • raw_data: str
  • parsed_data: str | bytes

Usage

Parse an existing Data URL:

from durl import DURL

durl = DURL("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==")

print(durl.mime_type)
# text/plain

print(durl.is_base64)
# True

print(durl.parsed_data)
# Hello, World!

print(str(durl))
# data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==

Parse a non-base64 Data URL with charset:

from durl import DURL

durl = DURL("data:text/plain;charset=UTF-8,%E4%BD%A0%E5%A5%BD")

print(durl.parameters["charset"])
# UTF-8

print(durl.parsed_data)
# 你好

Build a new Data URL from bytes:

from durl import DURL

durl = DURL.build(mime_type="text/plain", data=b"Hello, World!")

print(durl.raw_data)
# SGVsbG8sIFdvcmxkIQ==

print(str(durl))
# data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==

Build a non-base64 URL explicitly:

from durl import DURL

durl = DURL.build(
    mime_type="text/plain",
    data="你好".encode("utf-8"),
    parameters={"charset": "UTF-8"},
    is_base64=False,
)

print(str(durl))
# data:text/plain;charset=UTF-8,%E4%BD%A0%E5%A5%BD

Use immutable with_*() updates:

from durl import DURL

original = DURL("data:text/plain;base64,SGVsbG8=")
updated = original.with_mime_type("text/markdown").with_parameters(
    {"charset": "UTF-8"}
)

print(str(original))
# data:text/plain;base64,SGVsbG8=

print(str(updated))
# data:text/markdown;charset=UTF-8;base64,SGVsbG8=

Behavior

  • Parsing is strict and RFC 2397-oriented.
  • Both base64 and non-base64 forms are supported.
  • Non-base64 payloads are percent-decoded.
  • charset is respected when present.
  • Text payloads without charset must be ASCII, or decoding raises ValueError.
  • Invalid media types, duplicate parameters, malformed percent-encoding, and invalid base64 all raise ValueError.

Non-core Helpers

Text scanning helpers live outside the DURL core API:

from durl.utils.text import contents_from_text

contents_from_text() scans a larger string and returns a mixed list of plain text segments and parsed DURL objects.

Filesystem helpers are still kept out of the core object and can be added under durl.utils.* later if needed.

Development

Install dependencies:

poetry install --all-extras --all-groups

Run the docs site locally:

make docs-serve

Build the docs site:

make docs-build

Canonical test command:

python -m pytest -q

The repository also provides:

make pytest

The repository documentation site is built with mkdocs-material and deployed to GitHub Pages through GitHub Actions.

License

MIT. See LICENSE.

Project details


Download files

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

Source Distribution

durl_py-0.3.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

durl_py-0.3.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: durl_py-0.3.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.11.13 Darwin/25.3.0

File hashes

Hashes for durl_py-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c9992989334d0053843ca7e3fc5a85b072fee53f82ad79f0a5e9013f225a9cea
MD5 d348c367ec1dd9fb809bd279d856c2f0
BLAKE2b-256 bac0c97fca7714b001fbe11d2276060201b88e4f746bad20a623da8a150cb4e3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: durl_py-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.11.13 Darwin/25.3.0

File hashes

Hashes for durl_py-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b8d4d9bf9ba8cec87846acdb5f1b579d7db35bbb19714297e320eb49e424623
MD5 983c7f234dc8b8b0691883af9fa7a2a0
BLAKE2b-256 fdccea12b6ba3e1199a762c313b3ea9c8580044687d47ab4826ccab460d21dc3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page