22import contextlib
33import json
44import pprint
5- import sys
65import 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
138
149import marshmallow
1510from marshmallow import fields
1813except ImportError :
1914 pass
2015
21- # Checking Marshmallow version
22- MM2 = marshmallow .__version__ .startswith ('2' )
23- PY2 = int (sys .version_info [0 ]) == 2
24-
2516
2617@marshmallow .post_load
2718def __make_object__ (self , data , ** kwargs ):
@@ -51,7 +42,7 @@ def __new__(mcs, name, parents, dct):
5142 schema_fields [method_name ] = dct [method_name ]
5243
5344 elif hasattr (
54- value , '__marshmallow_tags__' if MM2 else
45+ value ,
5546 '__marshmallow_hook__' ) or key in ('Meta' , 'on_bind_field' ,
5647 'handle_error' ):
5748 schema_fields [key ] = value
@@ -72,6 +63,7 @@ def __new__(mcs, name, parents, dct):
7263 def __call__ (cls , * args , ** kwargs ):
7364 if kwargs .pop ('__post_load__' , False ):
7465 kwargs .pop ("many" , None )
66+ kwargs .pop ("unknown" , None )
7567 schema = kwargs .pop ('__schema__' )
7668 obj = cls .__new__ (cls , * args , ** kwargs )
7769 obj .__dump_lock__ = threading .RLock ()
@@ -87,7 +79,14 @@ def __call__(cls, *args, **kwargs):
8779 context = kwargs .pop ('context' , None )
8880 partial = kwargs .pop ('partial' , None )
8981 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+ )
9190 return obj
9291
9392
@@ -126,8 +125,6 @@ class Model(with_metaclass(ModelMeta)):
126125
127126 @classmethod
128127 def __get_schema_class__ (cls , ** kwargs ):
129- if MM2 :
130- kwargs .setdefault ('strict' , True )
131128 return cls .__schema_class__ (** kwargs )
132129
133130 def __setattr_default__ (self , key , value ):
@@ -179,18 +176,14 @@ def context(self, value):
179176 self .__schema__ .context = value
180177
181178 @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 ):
183180 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 )
187182 return loaded
188183
189184 def dump (self ):
190185 with self .__dump_mode_on__ ():
191186 dump = self .__schema__ .dump (self )
192- if MM2 :
193- return dump .data
194187 return dump
195188
196189 @classmethod
@@ -199,16 +192,16 @@ def load_json(cls,
199192 context = None ,
200193 many = None ,
201194 partial = None ,
195+ unknown = None ,
202196 * args ,
203197 ** kwargs ):
204198 schema = cls .__get_schema_class__ (context = context )
205199 loaded = schema .loads (data ,
206200 many = many ,
207201 partial = partial ,
202+ unknown = unknown ,
208203 * args ,
209204 ** kwargs )
210- if MM2 :
211- return loaded [0 ]
212205 return loaded
213206
214207 def dump_json (self ):
@@ -220,22 +213,25 @@ def load_yaml(cls,
220213 context = None ,
221214 many = None ,
222215 partial = None ,
216+ unknown = None ,
223217 * args ,
224218 ** 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+ )
227227
228228 def dump_yaml (self , default_flow_style = False ):
229229 return yaml .dump (self .dump (), default_flow_style = default_flow_style )
230230
231231 @classmethod
232232 def load_ini (cls , data , context = None , partial = None , ** kwargs ):
233233 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 )
239235 ddata = {s : dict (parser .items (s )) for s in parser .sections ()}
240236 ddata .update (parser .defaults ())
241237 return cls .load (ddata , context = context , partial = partial )
@@ -258,8 +254,6 @@ def dump_ini(self, **kwargs):
258254 @classmethod
259255 def validate (cls , data , context = None , many = None , partial = None ):
260256 kwargs = {'context' : context }
261- if MM2 :
262- kwargs ['strict' ] = False
263257 schema = cls .__get_schema_class__ (** kwargs )
264258 return schema .validate (data , many = many , partial = partial )
265259
@@ -297,8 +291,6 @@ def dump_many(data, context=None):
297291 else :
298292 schema = obj .__get_schema_class__ (context = context )
299293 obj_data = schema .dump (obj )
300- if MM2 :
301- obj_data = obj_data [0 ]
302294 ret .append (obj_data )
303295 elif (isinstance (obj , collections .Sequence )
304296 and not isinstance (obj , str )):
0 commit comments