Skip to main content

Simple Data URL Model

Project description

durl-py

A lightweight Python library for working with Data URLs (RFC 2397).

Installation

pip install durl-py

Usage

DataURL Class

The durl.DataURL class provides a convenient way to create, parse, and manipulate Data URLs.

Creating a DataURL from raw data:

from durl import DataURL, MIMEType

# From bytes
data_url = DataURL.from_data(MIMEType.TEXT_PLAIN, b"Hello, World!")
print(data_url.url)
# data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==

# From string
data_url = DataURL.from_data(MIMEType.TEXT_PLAIN, "Hello, World!")
print(data_url.url)
# data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==

Parsing a DataURL string:

from durl import DataURL

data_url_string = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="
data_url = DataURL.from_url(data_url_string)

print(data_url.mime_type)
# text/plain

print(data_url.data_decoded)
# Hello, World!

message_contents_from_text function

The durl.message_contents_from_text function allows you to extract Data URLs from a string.

from durl import message_contents_from_text, DataURL

text = "Here is an image: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg== and some text."

contents = message_contents_from_text(text)

print(contents[0])
# Here is an image:

assert isinstance(contents[1], DataURL)
print(contents[1].mime_type)
# image/png

print(contents[2])
# and some text.

Development

To install the development dependencies, run:

pip install -r requirements-all.txt

To run the tests, run:

pytest

License

This project is licensed under the MIT License. See the LICENSE file for details.

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.1.1.tar.gz (7.5 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.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: durl_py-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.13 Darwin/24.5.0

File hashes

Hashes for durl_py-0.1.1.tar.gz
Algorithm Hash digest
SHA256 787fea5956eab7ec120511deb317da95e3bc390553e803197fb4702b1fac3666
MD5 0e6f4ff2a61d8dc78d96be57d1275724
BLAKE2b-256 96c896d8b22c06cc8c0b8fee0625363507fefc175f2e598b6b74ecfc31245737

See more details on using hashes here.

File details

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

File metadata

  • Download URL: durl_py-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.13 Darwin/24.5.0

File hashes

Hashes for durl_py-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e20510a8f79b5eed1169c432c2a6c9e13950416de4c1c577d33d14e76bd2b131
MD5 e4d42b2b5203b8277fc45f35db598952
BLAKE2b-256 95727edf95f24e8e8293547c431f1599295566734a253de50fc51ac0a55367bf

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