Skip to main content

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.

Metadata

Release files for durl-py 0.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 durl-py 0.3.0
File Size Uploaded
durl_py-0.3.0.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for durl-py 0.3.0
File Interpreter ABI Platform
durl_py-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / durl_py-0.3.0.tar.gz

Download URL durl_py-0.3.0.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c9992989334d0053843ca7e3fc5a85b072fee53f82ad79f0a5e9013f225a9cea
BLAKE2b-256 checksum
How to use checksums
bac0c97fca7714b001fbe11d2276060201b88e4f746bad20a623da8a150cb4e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.11.13 Darwin/25.3.0

Release files / durl_py-0.3.0-py3-none-any.whl

Download URL durl_py-0.3.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b8d4d9bf9ba8cec87846acdb5f1b579d7db35bbb19714297e320eb49e424623
BLAKE2b-256 checksum
How to use checksums
fdccea12b6ba3e1199a762c313b3ea9c8580044687d47ab4826ccab460d21dc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.11.13 Darwin/25.3.0

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

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