-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.py
72 lines (57 loc) · 2.09 KB
/
serialization.py
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
import logging
import os.path
import requests
def validate_bin(path: str):
if os.path.isdir(path):
return False
elif not os.path.exists(path):
return False
elif not os.path.getsize(path):
return False
else:
with open(path, "rb") as f:
header = f.read(8)
return header == b"\x00\x01\x00\x00\x00\xff\xff\xff"
def validate_json(path: str):
if os.path.isdir(path):
return False
elif not os.path.exists(path):
return False
elif not os.path.getsize(path):
return False
else:
with open(path, "rb") as f:
return f.read(1) == b"{"
def validate_csv(path: str):
if os.path.isdir(path):
return False
elif not os.path.exists(path):
return False
elif not os.path.getsize(path):
return False
else:
with open(path, "rb") as f:
headers = f.readline()
return headers == "code\tko\tja\ten\tc\tsc"
def serialize_bin(file, output_path: str):
with open(file, 'rb') as f:
file_content = f.read()
url = "https://binser.azurewebsites.net/api/SerBin?code=15Nt3c9v-mJKqymOryauu-mUTiW7w3inDuybikcg8eZ8AzFuDe7VVA=="
headers = {"Content-Type": "application/octet-stream"}
response = requests.post(url, headers=headers, data=file_content)
basename = os.path.basename(file)
name_without_ext = os.path.splitext(basename)[0]
path = os.path.join(output_path, f'{name_without_ext}.json')
with open(path, 'wb') as f:
f.write(response.content)
def deserialize_bin(file, output_path: str):
with open(file, 'rb') as f:
file_content = f.read()
url = "https://binser.azurewebsites.net/api/DeserBin?code=HqjKFtCn-SwOrgt2Uk11S_gwhdjHT7ciE53A4ikf5q-cAzFuBeiWvg=="
headers = {"Content-Type": "application/json"}
response = requests.post(url, headers=headers, data=file_content)
basename = os.path.basename(file)
name_without_ext = os.path.splitext(basename)[0]
path = os.path.join(output_path, f'{name_without_ext}.bin')
with open(path, 'wb') as f:
f.write(response.content)