Skip to content

About

📦 A package for parsing URI Data

Resources

Stars

1 star

Watchers

5 watching

Forks

Latest commit

 

History

62 Commits

Folders and files

Repository files navigation

data_uri_parser

Supported Python versions PyPI version Licence

Data URI manipulation made easy.

This isn't very robust, and will reject a number of valid data URIs. However, it meets the most useful case: a mimetype, a charset, and the base64 flag.

How to install

Requires Python 3.10 or newer.

pip install data_uri_parser

Parsing

>>> from data_uri_parser import DataURI
>>> uri = DataURI('data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu')
>>> uri.mimetype
'text/plain'
>>> uri.charset
'utf-8'
>>> uri.is_base64
True
>>> uri.data
b'The quick brown fox jumped over the lazy dog.'

For base64 data URIs, DataURI.data returns bytes; it won't decode them into a string based on the charset. For other data URIs it returns the percent-decoded str.

Creating from bytes

>>> made = DataURI.make('text/plain', charset='us-ascii', base64=True, data=b'This is a message.')
>>> made
'data:text/plain;charset=us-ascii;base64,VGhpcyBpcyBhIG1lc3NhZ2Uu'
>>> made.data
b'This is a message.'

With base64=False, the data is percent-encoded instead:

>>> DataURI.make('text/plain', base64=False, data=b'Hello, world')
'data:text/plain,Hello%2C%20world'

Creating from a file

This is really just a convenience method.

>>> png_uri = DataURI.from_file('somefile.png')
>>> png_uri.mimetype
'image/png'
>>> png_uri.data
b'\x89PNG\r\n...'

Development

poetry install
poetry run pytest
poetry run black --check .
poetry run isort --check-only .

CI runs the tests on Python 3.10 to 3.14.

Releasing

Merging to main publishes to PyPI, so every PR must bump version in pyproject.toml. CI fails a PR whose version is already on PyPI.

Notes

Originally from: https://gist.github.com/zacharyvoase/5538178

About

📦 A package for parsing URI Data

Resources

Stars

1 star

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages