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,14 @@ 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 (
84
+ kwargs ,
85
+ many = many ,
86
+ context = context ,
87
+ partial = partial ,
88
+ unknown = unknown
89
+ )
91
90
return obj
92
91
93
92
@@ -126,8 +125,6 @@ class Model(with_metaclass(ModelMeta)):
126
125
127
126
@classmethod
128
127
def __get_schema_class__ (cls , ** kwargs ):
129
- if MM2 :
130
- kwargs .setdefault ('strict' , True )
131
128
return cls .__schema_class__ (** kwargs )
132
129
133
130
def __setattr_default__ (self , key , value ):
@@ -179,18 +176,14 @@ def context(self, value):
179
176
self .__schema__ .context = value
180
177
181
178
@classmethod
182
- def load (cls , data , context = None , many = None , partial = None ):
179
+ def load (cls , data , context = None , many = None , partial = None , unknown = None ):
183
180
schema = cls .__get_schema_class__ (context = context , partial = partial )
184
- loaded = schema .load (data , many = many )
185
- if MM2 :
186
- return loaded [0 ]
181
+ loaded = schema .load (data , many = many , unknown = unknown )
187
182
return loaded
188
183
189
184
def dump (self ):
190
185
with self .__dump_mode_on__ ():
191
186
dump = self .__schema__ .dump (self )
192
- if MM2 :
193
- return dump .data
194
187
return dump
195
188
196
189
@classmethod
@@ -199,16 +192,16 @@ def load_json(cls,
199
192
context = None ,
200
193
many = None ,
201
194
partial = None ,
195
+ unknown = None ,
202
196
* args ,
203
197
** kwargs ):
204
198
schema = cls .__get_schema_class__ (context = context )
205
199
loaded = schema .loads (data ,
206
200
many = many ,
207
201
partial = partial ,
202
+ unknown = unknown ,
208
203
* args ,
209
204
** kwargs )
210
- if MM2 :
211
- return loaded [0 ]
212
205
return loaded
213
206
214
207
def dump_json (self ):
@@ -220,22 +213,25 @@ def load_yaml(cls,
220
213
context = None ,
221
214
many = None ,
222
215
partial = None ,
216
+ unknown = None ,
223
217
* args ,
224
218
** kwargs ):
225
- loaded = yaml .load (data , * args , ** kwargs )
226
- return cls .load (loaded , context = context , many = many , partial = partial )
219
+ loaded = yaml .load (data , Loader = yaml .FullLoader )
220
+ return cls .load (
221
+ loaded ,
222
+ context = context ,
223
+ many = many ,
224
+ partial = partial ,
225
+ unknown = unknown
226
+ )
227
227
228
228
def dump_yaml (self , default_flow_style = False ):
229
229
return yaml .dump (self .dump (), default_flow_style = default_flow_style )
230
230
231
231
@classmethod
232
232
def load_ini (cls , data , context = None , partial = None , ** kwargs ):
233
233
parser = configparser .ConfigParser (** kwargs )
234
- if PY2 :
235
- fp = io .StringIO (data )
236
- parser .readfp (fp )
237
- else :
238
- parser .read_string (data )
234
+ parser .read_string (data )
239
235
ddata = {s : dict (parser .items (s )) for s in parser .sections ()}
240
236
ddata .update (parser .defaults ())
241
237
return cls .load (ddata , context = context , partial = partial )
@@ -258,8 +254,6 @@ def dump_ini(self, **kwargs):
258
254
@classmethod
259
255
def validate (cls , data , context = None , many = None , partial = None ):
260
256
kwargs = {'context' : context }
261
- if MM2 :
262
- kwargs ['strict' ] = False
263
257
schema = cls .__get_schema_class__ (** kwargs )
264
258
return schema .validate (data , many = many , partial = partial )
265
259
@@ -297,8 +291,6 @@ def dump_many(data, context=None):
297
291
else :
298
292
schema = obj .__get_schema_class__ (context = context )
299
293
obj_data = schema .dump (obj )
300
- if MM2 :
301
- obj_data = obj_data [0 ]
302
294
ret .append (obj_data )
303
295
elif (isinstance (obj , collections .Sequence )
304
296
and not isinstance (obj , str )):
0 commit comments