Repository navigation
Expand file tree
/
Copy pathhelpers.py
More file actions
109 lines (86 loc) · 2.65 KB
/
Copy pathhelpers.py
File metadata and controls
109 lines (86 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from io import BytesIO
from Crypto.Util.Padding import pad
import hashlib
import base64
from Crypto.Cipher import AES
def fill_query_params(query, *args):
return query.format(*args)
def sp_endpoint(path, method='GET'):
def decorator(function):
def wrapper(*args, **kwargs):
kwargs.update({
'path': path,
'method': method
})
return function(*args, **kwargs)
wrapper.__doc__ = function.__doc__
return wrapper
return decorator
def encrypt_aes(file_or_bytes_io, key, iv):
key = base64.b64decode(key)
iv = base64.b64decode(iv)
aes = AES.new(key, AES.MODE_CBC, iv)
try:
if isinstance(file_or_bytes_io, BytesIO):
return aes.encrypt(pad(file_or_bytes_io.read(), 16))
return aes.encrypt(pad(bytes(file_or_bytes_io.read(), encoding='iso-8859-1'), 16))
except UnicodeEncodeError:
file_or_bytes_io.seek(0)
return aes.encrypt(pad(bytes(file_or_bytes_io.read(), encoding='utf-8'), 16))
except TypeError:
file_or_bytes_io.seek(0)
return aes.encrypt(pad(file_or_bytes_io.read(), 16))
def decrypt_aes(content, key, iv):
key = base64.b64decode(key)
iv = base64.b64decode(iv)
decrypter = AES.new(key, AES.MODE_CBC, iv)
decrypted = decrypter.decrypt(content)
padding_bytes = decrypted[-1]
return decrypted[:-padding_bytes]
def create_md5(file):
hash_md5 = hashlib.md5()
if isinstance(file, BytesIO):
for chunk in iter(lambda: file.read(4096), b''):
hash_md5.update(chunk)
file.seek(0)
return hash_md5.hexdigest()
if isinstance(file, str):
with open(file, "rb") as f:
for chunk in iter(lambda: f.read(4096), b''):
hash_md5.update(chunk)
return hash_md5.hexdigest()
for chunk in iter(lambda: file.read(4096), b''):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def nest_dict(flat: dict()):
"""
Convert flat dictionary to nested dictionary.
Input
{
"AmazonOrderId":1,
"ShipFromAddress.Name" : "Seller",
"ShipFromAddress.AddressLine1": "Street",
}
Output
{
"AmazonOrderId":1,
"ShipFromAddress.: {
"Name" : "Seller",
"AddressLine1": "Street",
}
}
Args:
flat:dict():
Returns:
nested:dict():
"""
result = {}
for k, v in flat.items():
_nest_dict_rec(k, v, result)
return result
def _nest_dict_rec(k, v, out):
k, *rest = k.split('.', 1)
if rest:
_nest_dict_rec(rest[0], v, out.setdefault(k, {}))
else:
out[k] = v