forked from deepfakes/faceswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializer.py
83 lines (65 loc) · 1.91 KB
/
Serializer.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
73
74
75
76
77
78
79
80
81
82
83
import json, pickle
class Serializer(object):
ext = "wtf"
woptions = "????"
roptions = "????"
@classmethod
def marshal(cls, input_data):
raise NotImplementedError()
@classmethod
def unmarshal(cls, input_string):
raise NotImplementedError()
class YAMLSerializer(Serializer):
ext = "yml"
woptions = "w"
roptions = "r"
@classmethod
def marshal(cls, input_data):
try:
import yaml
except ImportError:
print("You must have PyYAML installed to use YAMLSerializer")
return yaml.dump(input_data, default_flow_style=False)
@classmethod
def unmarshal(cls, input_string):
try:
import yaml
except ImportError:
print("You must have PyYAML installed to use YAMLSerializer")
return yaml.load(input_string)
class JSONSerializer(Serializer):
ext = "json"
woptions = "w"
roptions = "r"
@classmethod
def marshal(cls, input_data):
return json.dumps(input_data, indent=2)
@classmethod
def unmarshal(cls, input_string):
return json.loads(input_string)
class PickleSerializer(Serializer):
ext = "p"
woptions = "wb"
roptions = "rb"
@classmethod
def marshal(cls, input_data):
return pickle.dumps(input_data)
@classmethod
def unmarshal(cls, input_bytes):
return pickle.loads(input_bytes)
def get_serializer(serializer):
if serializer == "yaml":
return YAMLSerializer
if serializer == "json":
return JSONSerializer
if serializer == "pickle":
return PickleSerializer
raise NotImplementedError()
def get_serializer_fromext(ext):
if ext in (".yaml", ".yml"):
return YAMLSerializer
if ext == ".json":
return JSONSerializer
if ext == ".p":
return PickleSerializer
raise NotImplementedError("No serializer matching that file type.")