Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions data_url/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import base64
from urllib.parse import unquote, quote

DATA_URL_RE = re.compile(
r"""
Expand Down Expand Up @@ -39,7 +40,7 @@ def construct_data_url(mime_type, base64_encoded, data):
return data_url.url

class DataURL:
URL_FORMAT = "data:{mime_type}{encoded},{data}"
URL_FORMAT = "data:{mime_type}{parameters}{encoded},{data}"
ENCODING_STRING = ";base64"

@classmethod
Expand Down Expand Up @@ -119,6 +120,14 @@ def __parse_url(self):
if match:
self._is_base64_encoded = match.group('encoded') is not None
self._mime_type = match.group("MIME") or ""
params = match.group("parameters")
if params:
self._parameters = {}
for pair in params.split(";"):
if pair:
name, value = pair.split("=", 1)
self._parameters[name] = unquote(value)

raw_data = match.group('data')
if self._is_base64_encoded:
self._data = base64.b64decode(raw_data)
Expand All @@ -130,8 +139,9 @@ def __parse_url(self):
def __construct_url(self):
"""Constructs an actual data URL string from class attributes."""
return self.URL_FORMAT.format(
mime_type=self._mime_type,
encoded=self.ENCODING_STRING if self._is_base64_encoded else "",
mime_type=self.mime_type,
parameters=";" + ";".join([f"{name}={quote(value)}" for name, value in self.parameters.items()]) if self.parameters else "",
encoded=self.ENCODING_STRING if self.is_base64_encoded else "",
data=self.encoded_data
)

Expand Down Expand Up @@ -164,3 +174,10 @@ def encoded_data(self):
if self._is_base64_encoded:
return base64.b64encode(self._data).decode('utf-8')
return self._data

@property
def parameters(self):
"""Attribute / Value parameters."""
if not hasattr(self, '_parameters'):
self._parameters = {}
return self._parameters
23 changes: 23 additions & 0 deletions test/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from data_url import *

class TestUrlCreation(unittest.TestCase):
example_url = "data:image/png;hello=world;name=two%20words;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
example_data = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="

# TODO tests with characters that need percent encoding
def test_construct_data_url_string(self):
mime_type = "text/plain"
Expand Down Expand Up @@ -64,6 +67,19 @@ def test_non_compliant_url(self):
url = DataURL.from_url("not a url")
assert url is None

def test_url_with_parameters(self):
url = DataURL.from_url(self.example_url)
self.assertEqual(url.mime_type, "image/png")
self.assertEqual(url.is_base64_encoded, True)
self.assertDictEqual(url.parameters, {"hello": "world", "name": "two words"})

def test_url_assembly_with_parameters(self):
url = DataURL.from_data("image/png", True, self.example_data)
url.parameters["hello"] = "world"
url.parameters["name"] = "two words"
self.assertEqual(str(url), self.example_url)


class TestFromData(unittest.TestCase):
def test_typing(self):
with self.assertRaises(Exception) as context:
Expand Down Expand Up @@ -172,3 +188,10 @@ def run_assertions(self):

self.assertEqual(self.url.is_base64_encoded, self.base64_encoded)
self.assertEqual(self.expected_url, self.url.url)

class TestFromUrl(unittest.TestCase):
def test_from_urls(self):
test_str = "data:image/png;charset=USASCII;name=file.png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
url = DataURL.from_url(test_str)
self.assertEqual(len(url.parameters), 2)
self.assertDictEqual(url.parameters, {"charset": "USASCII", "name": "file.png"})