-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
serialize and deserialize functions | ||
""" | ||
from base64 import b64encode, b64decode | ||
from json import dumps, loads | ||
from typing import Tuple, Dict, Union | ||
|
||
|
||
def serialize(img: bytes, metadata: dict = None) -> str: | ||
""" | ||
This function serialize a binary bytes object | ||
:param img: mandatory, the binary bytes object | ||
:type img: bytes | ||
:param metadata: optional, some metadata about the object | ||
:type metadata: dict | ||
:return: a json formatted string containing the image and metadata | ||
:retype: str | ||
.. info :: In the json formatted string, the image is saved in base64 format | ||
""" | ||
b64_img = b64encode(img) | ||
str_img = b64_img.decode('utf-8') | ||
data = {'image': str_img} #type: Dict[str, Union[str, Dict[str, str]]] | ||
if metadata: | ||
data.update({'metadata': metadata}) | ||
json_img = dumps(data) | ||
return json_img | ||
|
||
|
||
def deserialize(data: str) -> Tuple[bytes, dict]: | ||
""" | ||
This function deserialize json formatted string | ||
:param data: the string json formatted string to deserialize | ||
:type data: str | ||
:return: the binary bytes image and metadata | ||
:retype: bytes, dict | ||
.. warning :: The data should look like the following example: \ | ||
``{"image": ..., "metadata": {...}}`` | ||
""" | ||
json_img = loads(data) | ||
str_img = json_img.get('image') | ||
metadata = json_img.get('metadata', {}) | ||
b64_img = str_img.encode('utf-8') | ||
b_img = b64decode(b64_img) | ||
return b_img, metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from unittest import TestCase, main | ||
from json import loads | ||
from base64 import b64decode | ||
|
||
from screamshot.serializer import serialize, deserialize | ||
|
||
|
||
class TestSerializerUnit(TestCase): | ||
def test_serialize_without_metadata(self): | ||
data = b'Hello world' | ||
json_data = serialize(data) | ||
|
||
self.assertIsInstance(json_data, str) | ||
|
||
extracted_data = loads(json_data) | ||
|
||
self.assertTrue('image' in extracted_data) | ||
self.assertFalse('metadata' in extracted_data) | ||
|
||
b64_image = extracted_data.get('image').encode('utf-8') | ||
self.assertNotEqual(data, b64_image) | ||
|
||
b_image = b64decode(b64_image) | ||
self.assertEqual(data, b_image) | ||
|
||
def test_serialize_with_metadata(self): | ||
data = b'Hello world' | ||
name = 'hello_world.png' | ||
author = 'John Doe' | ||
json_data = serialize(data, metadata={'name': name, 'author': author}) | ||
|
||
extracted_data = loads(json_data) | ||
|
||
self.assertTrue('image' in extracted_data) | ||
self.assertTrue('metadata' in extracted_data) | ||
|
||
metadata = extracted_data.get('metadata') | ||
self.assertTrue('name' in metadata) | ||
self.assertTrue('author' in metadata) | ||
self.assertEqual(name, metadata.get('name')) | ||
self.assertEqual(author, metadata.get('author')) | ||
|
||
b64_image = extracted_data.get('image').encode('utf-8') | ||
self.assertNotEqual(data, b64_image) | ||
|
||
b_image = b64decode(b64_image) | ||
self.assertEqual(data, b_image) | ||
|
||
def test_deserialize_without_metadata(self): | ||
data = '{"image": "SGVsbG8gd29ybGQ="}' | ||
b_img, metadata = deserialize(data) | ||
|
||
self.assertIsInstance(b_img, bytes) | ||
self.assertIsInstance(metadata, dict) | ||
|
||
self.assertEqual(b_img, b'Hello world') | ||
self.assertEqual(metadata, {}) | ||
|
||
def test_deserialize_with_metadata(self): | ||
data = '{"image": "SGVsbG8gd29ybGQ=", "metadata": {"name": "hello_world.png", \ | ||
"author": "John Doe"}}' | ||
b_img, metadata = deserialize(data) | ||
|
||
self.assertIsInstance(b_img, bytes) | ||
self.assertIsInstance(metadata, dict) | ||
|
||
self.assertEqual(b_img, b'Hello world') | ||
self.assertEqual(metadata, {'name': 'hello_world.png', 'author': 'John Doe'}) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |