Skip to content

Commit

Permalink
Base Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anubhav-narayan committed May 11, 2021
1 parent 0895ba9 commit 9c88947
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Specify filepatterns you want git to ignore.

*.dillo
*.json
build/*
dist/*
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021 Anubhav Mattoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# dillO

A small dill wrapper with Metadata and Verification Hash

# Installation

## From Source
```bash
$ python3 setup.py install
```

## From `pip`
```bash
$ pip3 install dillO
```

# In-memory Storage
```python
from dillo import Dillo

file = Dillo('My dillO')
# some object instances as obj
file.store(obj)
```
# On File Storage
```python
from dillo import Dillo

file = Dillo('My dillO')
# some object instances as obj
file.store(obj) # store the object `before` you write
file.write_file('./filename.dillO') # write dillO files
```
# JSON
JSON Files
```python
from dillo import Dillo

file = Dillo('My dillO', type='json')
# some object instances as obj
file.store(obj) # store the object `before` you write
file.write_file('./filename.json') # write JSON files
```
JSON Strings
```python
from dillo import Dillo

file = Dillo('My dillO', type='json')
# some object instances as obj
file.store(obj) # store the object `before` you write
file.json_string() # get JSON string
```
JSON Objects
```python
from dillo import Dillo

file = Dillo('My dillO', type='json')
# some object instances as obj
file.store(obj) # store the object `before` you write
file.json() # get JSON Object
```
# Sample Files
dillO
```plaintext
-----METADATA-----
Name : Array JSON
dillO : pickle
Sign : SHA256
Tags :
Length : 271 Bytes
-----DILLO-----
gASVBAEAAAAAAACMCmRpbGwuX2RpbGyUjA1fY3JlYXRlX2FycmF5lJOUKGgAjAlfZ2V0X2F0dHKUk5R
oAIwOX2ltcG9ydF9tb2R1bGWUk5SMHG51bXB5LmNvcmUuX211bHRpYXJyYXlfdW1hdGiUhZRSlIwMX3
JlY29uc3RydWN0lIaUUpSMBW51bXB5lIwHbmRhcnJheZSTlEsAhZRDAWKUh5QoSwFLBYWUaA2MBWR0e
XBllJOUjAJpOJSJiIeUUpQoSwOMATyUTk5OSv____9K_____0sAdJRiiUMoAQAAAAAAAAACAAAAAAAA
AAMAAAAAAAAABAAAAAAAAAAFAAAAAAAAAJR0lE50lFKULg==
-----SIGN-----
1ac33161cd72c5ce8ec286ea322a02372e1d759a6724f64d33417ac8274d2808
```
JSON
```json
{"py/object": "dillo.dillo.Dillo", "name": "Array JSON", "type": "json", "sign": "SHA256", "protocol": null, "byref": false, "fmode": 2, "recurse": false, "_stream": {"py/b64": "gASVBAEAAAAAAACMCmRpbGwuX2RpbGyUjA1fY3JlYXRlX2FycmF5lJOUKGgAjAlfZ2V0X2F0dHKUk5RoAIwOX2ltcG9ydF9tb2R1bGWUk5SMHG51bXB5LmNvcmUuX211bHRpYXJyYXlfdW1hdGiUhZRSlIwMX3JlY29uc3RydWN0lIaUUpSMBW51bXB5lIwHbmRhcnJheZSTlEsAhZRDAWKUh5QoSwFLBYWUaA2MBWR0eXBllJOUjAJpOJSJiIeUUpQoSwOMATyUTk5OSv////9K/////0sAdJRiiUMoAQAAAAAAAAACAAAAAAAAAAMAAAAAAAAABAAAAAAAAAAFAAAAAAAAAJR0lE50lFKULg=="}, "ignore": false, "tags": {"py/set": []}, "hash": {"py/b64": "MWFjMzMxNjFjZDcyYzVjZThlYzI4NmVhMzIyYTAyMzcyZTFkNzU5YTY3MjRmNjRkMzM0MTdhYzgyNzRkMjgwOA=="}}
```
# License [MIT](https://choosealicense.com/licenses/mit/)
Copyright (c) 2021 Anubhav Mattoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions dillo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .dillo import Dillo
Binary file added dillo/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added dillo/__pycache__/dillo.cpython-39.pyc
Binary file not shown.
Binary file added dillo/__pycache__/signing.cpython-39.pyc
Binary file not shown.
184 changes: 184 additions & 0 deletions dillo/dillo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
'''
Small dill Wrapper with Metadata
MIT License
Copyright (c) 2021 Anubhav Mattoo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
__version__ = '0.0.1'
__author__ = 'Anubhav Mattoo'
__email__ = "anubhavmattoo@outlook.com"
__license__ = "MIT"
__status__ = "Beta"

import dill
from os import PathLike


class Dillo():
"""
dill with Metadata and Signing
"""
def __init__(self, name: str, type: str = 'pickle',
sign: str = 'SHA256', **kwargs):
self.name = name
self.type = type
self.sign = sign
self.protocol = kwargs.pop('protocol', None)
self.byref = kwargs.pop('byref', None)
self.fmode = kwargs.pop('fmode', None)
self.recurse = kwargs.pop('recurse', None)
self._stream = b''
self.ignore = kwargs.pop('ignore', None)
self.tags = set()

def __bytes__(self):
return self._stream

def __repr__(self):
fstr = f'-----METADATA-----\n'\
+ f'Name : {self.name}\n'\
+ f'dillO : {self.type}\n'\
+ f'Sign : {self.sign}\n'\
+ f'Tags : {", ".join([x for x in self.tags])}\n'\
+ f'Length : {len(self._stream)} Bytes\n'
return fstr

def store(self, obj, **kwargs) -> None:
'''
In Memory Strorage
'''
self._stream = dill.dumps(obj, self.protocol, self.byref,
self.fmode, self.recurse, **kwargs)

def read(self, **kwargs):
'''
In Memory Read
'''
return dill.loads(self._stream, self.ignore, **kwargs)

def write_file(self, filepath: PathLike):
'''
dillO to File
'''
data = self.text()
if self.type == 'pickle':
with open(filepath, 'wb') as f:
f.write(self.__repr__().encode())
f.write(data)
f.write(self.get_sign())
elif self.type == 'json':
with open(filepath, 'w') as f:
f.write(self.json_string())

def json_string(self):
'''
JSON String Helper
'''
import jsonpickle
self.hash = self.get_sign()[15:]
return jsonpickle.encode(self)

def json(self):
'''
JSON Object
'''
import json
return json.loads(self.json_string())

def text(self):
'''
Base64 Helper
'''
from base64 import urlsafe_b64encode
data = urlsafe_b64encode(self._stream)
if len(data) >= 80:
data = b"".join(data[i:i+79] + b"\n"
for i in range(0, len(data), 79))
data = data[:-1]
data = f'-----DILLO-----\n'.encode() + data + b'\n'
return data

def get_sign(self) -> bytes:
'''
Data Signing Helper
'''
from .signing import Sign
if self.sign == 'MD5':
return b'-----SIGN-----\n' + Sign.md5(self._stream).encode()
elif self.sign == 'SHA256':
return b'-----SIGN-----\n' + Sign.sha256(self._stream).encode()
elif self.sign == 'SHA3-256':
return b'-----SIGN-----\n' + Sign.sha3_256(self._stream).encode()
else:
raise TypeError(f'Unknown Signing Type \'{self.sign}\'')

def add_tag(self, tag: str) -> None:
self.tags.add(tag)

@classmethod
def read_file(cls, filepath: PathLike):
'''
dillO File Reader
'''
from base64 import urlsafe_b64decode
with open(filepath, 'r') as f:
buffer_data = f.read()
import re
regex = r'^-{5}METADATA-{5}\n'\
+ r'(Name : (?P<name>.*))\n'\
+ r'(dillO : (?P<type>.*))\n'\
+ r'(Sign : (?P<sign>.*))\n'\
+ r'(Tags : (?P<tags>.*))\n'\
+ r'(Length : (?P<length>[0-9]* Bytes))\n'\
+ r'-{5}DILLO-{5}\n'\
+ r'(?P<dillo>[A-Za-z0-9-_=\n]*)'\
+ r'-{5}SIGN-{5}\n(?P<hash>[0-9a-fA-F]*)$'
parsed_buffer = re.match(regex, buffer_data)
if parsed_buffer:
this = cls(parsed_buffer['name'], parsed_buffer['type'],
parsed_buffer['sign'])
this.tags = set(parsed_buffer['tags'].split(', '))
data = ''.join(parsed_buffer['dillo'].splitlines())
data = urlsafe_b64decode(data)
setattr(this, '_stream', data)
if this.get_sign().decode()[15:] == parsed_buffer['hash']:
return this
else:
raise ValueError('Unverified dillO')
else:
try:
import jsonpickle
import json
this = jsonpickle.decode(buffer_data)
urlsafe_b64decode(
json.loads(buffer_data)['hash']['py/b64']).decode()
if this.get_sign().decode()[15:] ==\
urlsafe_b64decode(
json.loads(buffer_data)['hash']['py/b64']
).decode():
return this
else:
raise ValueError('Unverified dillO')
except ValueError:
raise
except Exception:
raise TypeError('Incorrect File Format')
Loading

0 comments on commit 9c88947

Please sign in to comment.