2
2
import contextlib
3
3
import json
4
4
import pprint
5
- import sys
6
5
import threading
7
- try :
8
- import configparser
9
- import io
10
- except ImportError :
11
- import ConfigParser as configparser
12
- import StringIO as io
6
+ import configparser
7
+ import io
13
8
14
9
import marshmallow
15
10
from marshmallow import fields
18
13
except ImportError :
19
14
pass
20
15
21
- # Checking Marshmallow version
22
- MM2 = marshmallow .__version__ .startswith ('2' )
23
- PY2 = int (sys .version_info [0 ]) == 2
24
-
25
16
26
17
@marshmallow .post_load
27
18
def __make_object__ (self , data , ** kwargs ):
@@ -51,7 +42,7 @@ def __new__(mcs, name, parents, dct):
51
42
schema_fields [method_name ] = dct [method_name ]
52
43
53
44
elif hasattr (
54
- value , '__marshmallow_tags__' if MM2 else
45
+ value ,
55
46
'__marshmallow_hook__' ) or key in ('Meta' , 'on_bind_field' ,
56
47
'handle_error' ):
57
48
schema_fields [key ] = value
@@ -72,6 +63,7 @@ def __new__(mcs, name, parents, dct):
72
63
def __call__ (cls , * args , ** kwargs ):
73
64
if kwargs .pop ('__post_load__' , False ):
74
65
kwargs .pop ("many" , None )
66
+ kwargs .pop ("unknown" , None )
75
67
schema = kwargs .pop ('__schema__' )
76
68
obj = cls .__new__ (cls , * args , ** kwargs )
77
69
obj .__dump_lock__ = threading .RLock ()
@@ -87,7 +79,8 @@ def __call__(cls, *args, **kwargs):
87
79
context = kwargs .pop ('context' , None )
88
80
partial = kwargs .pop ('partial' , None )
89
81
many = kwargs .pop ("many" , None )
90
- obj = cls .load (kwargs , many = many , context = context , partial = partial )
82
+ unknown = kwargs .pop ('unknown' , None )
83
+ obj = cls .load (kwargs , many = many , context = context , partial = partial , unknown = unknown )
91
84
return obj
92
85
93
86
@@ -126,8 +119,6 @@ class Model(with_metaclass(ModelMeta)):
126
119
127
120
@classmethod
128
121
def __get_schema_class__ (cls , ** kwargs ):
129
- if MM2 :
130
- kwargs .setdefault ('strict' , True )
131
122
return cls .__schema_class__ (** kwargs )
132
123
133
124
def __setattr_default__ (self , key , value ):
@@ -179,18 +170,14 @@ def context(self, value):
179
170
self .__schema__ .context = value
180
171
181
172
@classmethod
182
- def load (cls , data , context = None , many = None , partial = None ):
173
+ def load (cls , data , context = None , many = None , partial = None , unknown = None ):
183
174
schema = cls .__get_schema_class__ (context = context , partial = partial )
184
- loaded = schema .load (data , many = many )
185
- if MM2 :
186
- return loaded [0 ]
175
+ loaded = schema .load (data , many = many , unknown = unknown )
187
176
return loaded
188
177
189
178
def dump (self ):
190
179
with self .__dump_mode_on__ ():
191
180
dump = self .__schema__ .dump (self )
192
- if MM2 :
193
- return dump .data
194
181
return dump
195
182
196
183
@classmethod
@@ -199,16 +186,16 @@ def load_json(cls,
199
186
context = None ,
200
187
many = None ,
201
188
partial = None ,
189
+ unknown = None ,
202
190
* args ,
203
191
** kwargs ):
204
192
schema = cls .__get_schema_class__ (context = context )
205
193
loaded = schema .loads (data ,
206
194
many = many ,
207
195
partial = partial ,
196
+ unknown = unknown ,
208
197
* args ,
209
198
** kwargs )
210
- if MM2 :
211
- return loaded [0 ]
212
199
return loaded
213
200
214
201
def dump_json (self ):
@@ -220,22 +207,19 @@ def load_yaml(cls,
220
207
context = None ,
221
208
many = None ,
222
209
partial = None ,
210
+ unknown = None ,
223
211
* args ,
224
212
** kwargs ):
225
- loaded = yaml .load (data , * args , ** kwargs )
226
- return cls .load (loaded , context = context , many = many , partial = partial )
213
+ loaded = yaml .load (data , Loader = yaml . FullLoader )
214
+ return cls .load (loaded , context = context , many = many , partial = partial , unknown = unknown )
227
215
228
216
def dump_yaml (self , default_flow_style = False ):
229
217
return yaml .dump (self .dump (), default_flow_style = default_flow_style )
230
218
231
219
@classmethod
232
220
def load_ini (cls , data , context = None , partial = None , ** kwargs ):
233
221
parser = configparser .ConfigParser (** kwargs )
234
- if PY2 :
235
- fp = io .StringIO (data )
236
- parser .readfp (fp )
237
- else :
238
- parser .read_string (data )
222
+ parser .read_string (data )
239
223
ddata = {s : dict (parser .items (s )) for s in parser .sections ()}
240
224
ddata .update (parser .defaults ())
241
225
return cls .load (ddata , context = context , partial = partial )
@@ -258,8 +242,6 @@ def dump_ini(self, **kwargs):
258
242
@classmethod
259
243
def validate (cls , data , context = None , many = None , partial = None ):
260
244
kwargs = {'context' : context }
261
- if MM2 :
262
- kwargs ['strict' ] = False
263
245
schema = cls .__get_schema_class__ (** kwargs )
264
246
return schema .validate (data , many = many , partial = partial )
265
247
@@ -297,8 +279,6 @@ def dump_many(data, context=None):
297
279
else :
298
280
schema = obj .__get_schema_class__ (context = context )
299
281
obj_data = schema .dump (obj )
300
- if MM2 :
301
- obj_data = obj_data [0 ]
302
282
ret .append (obj_data )
303
283
elif (isinstance (obj , collections .Sequence )
304
284
and not isinstance (obj , str )):
0 commit comments