@@ -30,6 +30,8 @@ class TinyDB(object):
30
30
and getting tables.
31
31
"""
32
32
33
+ DEFAULT_STORAGE = JSONStorage
34
+
33
35
def __init__ (self , * args , ** kwargs ):
34
36
"""
35
37
Create a new instance of TinyDB.
@@ -40,10 +42,9 @@ def __init__(self, *args, **kwargs):
40
42
:param storage: The class of the storage to use. Will be initialized
41
43
with ``args`` and ``kwargs``.
42
44
"""
43
- storage = kwargs .pop ('storage' , JSONStorage )
45
+ storage = kwargs .pop ('storage' , TinyDB . DEFAULT_STORAGE )
44
46
#: :type: Storage
45
47
self ._storage = storage (* args , ** kwargs )
46
- self ._serializers = {}
47
48
48
49
self ._table_cache = {}
49
50
self ._table = self .table ('_default' )
@@ -102,24 +103,6 @@ def purge_tables(self):
102
103
self ._write ({})
103
104
self ._table_cache .clear ()
104
105
105
- def register_serializer (self , serializer , name ):
106
- """
107
- Register a new Serializer.
108
-
109
- When reading from/writing to the underlying storage, TinyDB
110
- will run all objects through the list of registered serializers
111
- allowing each one to handle objects it recognizes.
112
-
113
- .. note:: The name has to be unique among this database instance.
114
- Re-using the same name will overwrite the old serializer.
115
- Also, registering a serializer will be reflected in all
116
- tables when reading/writing them.
117
-
118
- :param serializer: an instance of the serializer
119
- :type serializer: tinydb.serialize.Serializer
120
- """
121
- self ._serializers [name ] = serializer
122
-
123
106
def _read (self ):
124
107
"""
125
108
Reading access to the backend.
@@ -142,32 +125,10 @@ def _read_table(self, table):
142
125
"""
143
126
144
127
try :
145
- return self ._deserialize ( self . _read ()[table ])
128
+ return self ._read ()[table ]
146
129
except (KeyError , TypeError ):
147
130
return {}
148
131
149
- def _deserialize (self , data ):
150
- """
151
- Deserialize the data passed in with all registered serializers
152
-
153
- :param data: the data set to deserialize
154
- :return: the data set with deserialized values as needed
155
- """
156
- for serializer_name in self ._serializers :
157
- serializer = self ._serializers [serializer_name ]
158
- tag = '{{{}}}:' .format (serializer_name ) # E.g: '{TinyDate}:'
159
-
160
- for eid in data :
161
- for field in data [eid ]:
162
- try :
163
- if data [eid ][field ].startswith (tag ):
164
- encoded = data [eid ][field ][len (tag ):]
165
- data [eid ][field ] = serializer .decode (encoded )
166
- except AttributeError :
167
- pass # Not a string
168
-
169
- return data
170
-
171
132
def _write (self , tables ):
172
133
"""
173
134
Writing access to the backend.
@@ -193,36 +154,7 @@ def _write_table(self, values, table):
193
154
data = self ._read ()
194
155
data [table ] = values
195
156
196
- # Finally, serialize & store the data
197
- self ._write (self ._serialize (data ))
198
-
199
- def _serialize (self , data ):
200
- """
201
- Serialize the data passed in with all registered serializers
202
-
203
- :param data: the data set to serialize
204
- :return: the data set with serialized values as needed
205
- """
206
-
207
- for serializer_name in self ._serializers :
208
- # If no serializers are registered, this code will just look up
209
- # the serializer list and continue. But if there are serializers,
210
- # the inner loop will run very often.
211
- # For that reason, the lookup of the serialized class is pulled
212
- # out into the outer loop:
213
-
214
- serializer = self ._serializers [serializer_name ]
215
- serializer_class = serializer .OBJ_CLASS
216
-
217
- for eid in data :
218
- for field in data [eid ]:
219
- if isinstance (data [eid ][field ], serializer_class ):
220
- encoded = serializer .encode (data [eid ][field ])
221
- tagged = '{{{}}}:{}' .format (serializer_name , encoded )
222
-
223
- data [eid ][field ] = tagged
224
-
225
- return data
157
+ self ._write (data )
226
158
227
159
# Methods that are executed on the default table
228
160
# Because magic methods are not handlet by __getattr__ we need to forward
0 commit comments