-
Notifications
You must be signed in to change notification settings - Fork 26
/
translators.py
148 lines (119 loc) · 4.33 KB
/
translators.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
"""
notebook.py
Provides the translators for various data types to be persistable
"""
import six
import json
import collections
from .exceptions import ScrapbookException
class DataTranslatorRegistry(collections.MutableMapping):
def __init__(self):
self._translators = {}
def __getitem__(self, key):
return self._translators.__getitem__(key)
def __setitem__(self, key, value):
if not (getattr(value, "translate", None) and callable(value.translate)):
raise ScrapbookException(
"Can't register object without 'translate' method."
)
if not (getattr(value, "load", None) and callable(value.translate)):
raise ScrapbookException("Can't register object without 'load' method.")
return self._translators.__setitem__(key, value)
def __delitem__(self, key):
return self._translators.__delitem__(key)
def __iter__(self):
return self._translators.__iter__()
def __len__(self):
return self._translators.__len__()
def register(self, storage_type, translator):
"""
Registers a new storage_type to a particular translator
Parameters
----------
storage_type: str
Name of the mime subtype parsed by the translator.
translator: obj
The object which implements the required functions.
"""
# TODO: Make the translators specify what types they can store?
self[storage_type] = translator
def deregister(self, storage_type):
"""
Removes a particular translator from the registry
Parameters
----------
storage_type: str
Name of the mime subtype parsed by the translator.
"""
del self[storage_type]
def reset(self):
"""
Resets the registry to have no translators.
"""
self._translators = {}
# TODO: Add name
def load_data(self, storage_type, scrap):
"""
Finds the register for the given storage_type and loads the scrap into
a JSON or string object.
Parameters
----------
storage_type: str
Name of the mime subtype parsed by the translator.
scrap: obj
Object to be converted from JSON or string format to the original value.
"""
loader = self._translators.get(storage_type)
if not loader:
raise ScrapbookException(
'No translator found for "{}" data type!'.format(storage_type)
)
return loader.load(scrap)
# TODO: Add name
def translate_data(self, storage_type, scrap):
"""
Finds the register for the given storage_type and translates the scrap into
an object of the translator output type.
Parameters
----------
storage_type: str
Name of the mime subtype parsed by the translator.
scrap: obj
Object to be converted to JSON or string format for storage in an output
"""
translator = self._translators.get(storage_type)
if not translator:
raise ScrapbookException(
'No translator found for "{}" data type!'.format(storage_type)
)
return translator.translate(scrap)
class JsonTranslator(object):
def translate(self, scrap):
if isinstance(scrap, six.string_types):
return json.loads(scrap)
return scrap
def load(self, scrap):
# Just in case we somehow got a valid JSON string pushed
if isinstance(scrap, six.string_types):
return json.loads(scrap)
return scrap
class UnicodeTranslator(object):
def translate(self, scrap):
if not isinstance(scrap, six.string_types):
return str(scrap)
return scrap
def load(self, scrap):
# Just in case we somehow got a non-string saved?!
if not isinstance(scrap, six.string_types):
return str(scrap)
return scrap
class ArrowDataframeTranslator(object):
def translate(self, scrap):
pass # TODO: Implement
def load(self, scrap):
pass # TODO: Implement
registry = DataTranslatorRegistry()
registry.register("unicode", UnicodeTranslator())
registry.register("json", JsonTranslator())
# registry.register('arrow', ArrowDataframeTranslator())