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,8 @@ 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 (kwargs , many = many , context = context , partial = partial , unknown = unknown )
9184 return obj
9285
9386
@@ -126,8 +119,6 @@ class Model(with_metaclass(ModelMeta)):
126119
127120 @classmethod
128121 def __get_schema_class__ (cls , ** kwargs ):
129- if MM2 :
130- kwargs .setdefault ('strict' , True )
131122 return cls .__schema_class__ (** kwargs )
132123
133124 def __setattr_default__ (self , key , value ):
@@ -179,18 +170,14 @@ def context(self, value):
179170 self .__schema__ .context = value
180171
181172 @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 ):
183174 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 )
187176 return loaded
188177
189178 def dump (self ):
190179 with self .__dump_mode_on__ ():
191180 dump = self .__schema__ .dump (self )
192- if MM2 :
193- return dump .data
194181 return dump
195182
196183 @classmethod
@@ -199,16 +186,16 @@ def load_json(cls,
199186 context = None ,
200187 many = None ,
201188 partial = None ,
189+ unknown = None ,
202190 * args ,
203191 ** kwargs ):
204192 schema = cls .__get_schema_class__ (context = context )
205193 loaded = schema .loads (data ,
206194 many = many ,
207195 partial = partial ,
196+ unknown = unknown ,
208197 * args ,
209198 ** kwargs )
210- if MM2 :
211- return loaded [0 ]
212199 return loaded
213200
214201 def dump_json (self ):
@@ -220,22 +207,19 @@ def load_yaml(cls,
220207 context = None ,
221208 many = None ,
222209 partial = None ,
210+ unknown = None ,
223211 * args ,
224212 ** 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 )
227215
228216 def dump_yaml (self , default_flow_style = False ):
229217 return yaml .dump (self .dump (), default_flow_style = default_flow_style )
230218
231219 @classmethod
232220 def load_ini (cls , data , context = None , partial = None , ** kwargs ):
233221 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 )
239223 ddata = {s : dict (parser .items (s )) for s in parser .sections ()}
240224 ddata .update (parser .defaults ())
241225 return cls .load (ddata , context = context , partial = partial )
@@ -258,8 +242,6 @@ def dump_ini(self, **kwargs):
258242 @classmethod
259243 def validate (cls , data , context = None , many = None , partial = None ):
260244 kwargs = {'context' : context }
261- if MM2 :
262- kwargs ['strict' ] = False
263245 schema = cls .__get_schema_class__ (** kwargs )
264246 return schema .validate (data , many = many , partial = partial )
265247
@@ -297,8 +279,6 @@ def dump_many(data, context=None):
297279 else :
298280 schema = obj .__get_schema_class__ (context = context )
299281 obj_data = schema .dump (obj )
300- if MM2 :
301- obj_data = obj_data [0 ]
302282 ret .append (obj_data )
303283 elif (isinstance (obj , collections .Sequence )
304284 and not isinstance (obj , str )):
0 commit comments