4545# ------------------------------------------------------------------------------
4646
4747# Supported transmitted code
48- SUPPORTED_TYPES = (utils .DictType ,) + utils .ITERABLE_TYPES \
49- + utils .PRIMITIVE_TYPES
48+ SUPPORTED_TYPES = (
49+ (utils .DictType ,) + utils .ITERABLE_TYPES + utils .PRIMITIVE_TYPES
50+ )
5051
5152# Regex of invalid module characters
52- INVALID_MODULE_CHARS = r' [^a-zA-Z0-9\_\.]'
53+ INVALID_MODULE_CHARS = r" [^a-zA-Z0-9\_\.]"
5354
5455# ------------------------------------------------------------------------------
5556
@@ -58,7 +59,6 @@ class TranslationError(Exception):
5859 """
5960 Unmarshalling exception
6061 """
61- pass
6262
6363
6464def _slots_finder (clazz , fields_set ):
@@ -100,8 +100,13 @@ def _find_fields(obj):
100100 return fields
101101
102102
103- def dump (obj , serialize_method = None , ignore_attribute = None , ignore = None ,
104- config = jsonrpclib .config .DEFAULT ):
103+ def dump (
104+ obj ,
105+ serialize_method = None ,
106+ ignore_attribute = None ,
107+ ignore = None ,
108+ config = jsonrpclib .config .DEFAULT ,
109+ ):
105110 """
106111 Transforms the given object into a JSON-RPC compliant form.
107112 Converts beans into dictionaries with a __jsonclass__ entry.
@@ -130,8 +135,9 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
130135 pass
131136 else :
132137 if serializer is not None :
133- return serializer (obj , serialize_method , ignore_attribute ,
134- ignore , config )
138+ return serializer (
139+ obj , serialize_method , ignore_attribute , ignore , config
140+ )
135141
136142 # Primitive
137143 if isinstance (obj , utils .PRIMITIVE_TYPES ):
@@ -140,21 +146,24 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
140146 # Iterative
141147 elif isinstance (obj , utils .ITERABLE_TYPES ):
142148 # List, set or tuple
143- return [dump (item , serialize_method , ignore_attribute , ignore , config )
144- for item in obj ]
149+ return [
150+ dump (item , serialize_method , ignore_attribute , ignore , config )
151+ for item in obj
152+ ]
145153
146154 elif isinstance (obj , utils .DictType ):
147155 # Dictionary
148- return {key : dump (value , serialize_method , ignore_attribute ,
149- ignore , config )
150- for key , value in obj .items ()}
156+ return {
157+ key : dump (value , serialize_method , ignore_attribute , ignore , config )
158+ for key , value in obj .items ()
159+ }
151160
152161 # It's not a standard type, so it needs __jsonclass__
153162 module_name = inspect .getmodule (type (obj )).__name__
154163 json_class = obj .__class__ .__name__
155164
156- if module_name not in ('' , ' __main__' ):
157- json_class = ' {0}.{1}' .format (module_name , json_class )
165+ if module_name not in ("" , " __main__" ):
166+ json_class = " {0}.{1}" .format (module_name , json_class )
158167
159168 # Keep the class name in the returned object
160169 return_obj = {"__jsonclass__" : [json_class ]}
@@ -165,16 +174,16 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
165174 # Attrs MUST be a dict.
166175 serialize = getattr (obj , serialize_method )
167176 params , attrs = serialize ()
168- return_obj [' __jsonclass__' ].append (params )
177+ return_obj [" __jsonclass__" ].append (params )
169178 return_obj .update (attrs )
170179 elif utils .is_enum (obj ):
171180 # Add parameters for enumerations
172- return_obj [' __jsonclass__' ].append ([obj .value ])
181+ return_obj [" __jsonclass__" ].append ([obj .value ])
173182 else :
174183 # Otherwise, try to figure it out
175184 # Obviously, we can't assume to know anything about the
176185 # parameters passed to __init__
177- return_obj [' __jsonclass__' ].append ([])
186+ return_obj [" __jsonclass__" ].append ([])
178187
179188 # Prepare filtering lists
180189 known_types = SUPPORTED_TYPES + tuple (config .serialize_handlers )
@@ -188,14 +197,22 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
188197 attrs = {}
189198 for attr_name in fields :
190199 attr_value = getattr (obj , attr_name )
191- if isinstance (attr_value , known_types ) and \
192- attr_value not in ignore_list :
193- attrs [attr_name ] = dump (attr_value , serialize_method ,
194- ignore_attribute , ignore , config )
200+ if (
201+ isinstance (attr_value , known_types )
202+ and attr_value not in ignore_list
203+ ):
204+ attrs [attr_name ] = dump (
205+ attr_value ,
206+ serialize_method ,
207+ ignore_attribute ,
208+ ignore ,
209+ config ,
210+ )
195211 return_obj .update (attrs )
196212
197213 return return_obj
198214
215+
199216# ------------------------------------------------------------------------------
200217
201218
@@ -218,75 +235,88 @@ def load(obj, classes=None):
218235 return [load (entry ) for entry in obj ]
219236
220237 # Otherwise, it's a dict type
221- elif ' __jsonclass__' not in obj :
238+ elif " __jsonclass__" not in obj :
222239 return {key : load (value ) for key , value in obj .items ()}
223240
224241 # It's a dictionary, and it has a __jsonclass__
225- orig_module_name = obj [' __jsonclass__' ][0 ]
226- params = obj [' __jsonclass__' ][1 ]
242+ orig_module_name = obj [" __jsonclass__" ][0 ]
243+ params = obj [" __jsonclass__" ][1 ]
227244
228245 # Validate the module name
229246 if not orig_module_name :
230- raise TranslationError (' Module name empty.' )
247+ raise TranslationError (" Module name empty." )
231248
232- json_module_clean = re .sub (INVALID_MODULE_CHARS , '' , orig_module_name )
249+ json_module_clean = re .sub (INVALID_MODULE_CHARS , "" , orig_module_name )
233250 if json_module_clean != orig_module_name :
234- raise TranslationError ('Module name {0} has invalid characters.'
235- .format (orig_module_name ))
251+ raise TranslationError (
252+ "Module name {0} has invalid characters." .format (orig_module_name )
253+ )
236254
237255 # Load the class
238- json_module_parts = json_module_clean .split ('.' )
256+ json_module_parts = json_module_clean .split ("." )
239257 if classes and len (json_module_parts ) == 1 :
240258 # Local class name -- probably means it won't work
241259 try :
242260 json_class = classes [json_module_parts [0 ]]
243261 except KeyError :
244- raise TranslationError ('Unknown class or module {0}.'
245- .format (json_module_parts [0 ]))
262+ raise TranslationError (
263+ "Unknown class or module {0}." .format (json_module_parts [0 ])
264+ )
246265 else :
247266 # Module + class
248267 json_class_name = json_module_parts .pop ()
249- json_module_tree = '.' .join (json_module_parts )
268+ json_module_tree = "." .join (json_module_parts )
250269 try :
251270 # Use fromlist to load the module itself, not the package
252- temp_module = __import__ (json_module_tree ,
253- fromlist = [json_class_name ])
271+ temp_module = __import__ (
272+ json_module_tree , fromlist = [json_class_name ]
273+ )
254274 except ImportError :
255- raise TranslationError ('Could not import {0} from module {1}.'
256- .format (json_class_name , json_module_tree ))
275+ raise TranslationError (
276+ "Could not import {0} from module {1}." .format (
277+ json_class_name , json_module_tree
278+ )
279+ )
257280
258281 try :
259282 json_class = getattr (temp_module , json_class_name )
260283 except AttributeError :
261- raise TranslationError ("Unknown class {0}.{1}."
262- .format (json_module_tree , json_class_name ))
284+ raise TranslationError (
285+ "Unknown class {0}.{1}." .format (
286+ json_module_tree , json_class_name
287+ )
288+ )
263289
264290 # Create the object
265291 if isinstance (params , utils .ListType ):
266292 try :
267293 new_obj = json_class (* params )
268294 except TypeError as ex :
269- raise TranslationError ("Error instantiating {0}: {1}"
270- .format (json_class .__name__ , ex ))
295+ raise TranslationError (
296+ "Error instantiating {0}: {1}" .format (json_class .__name__ , ex )
297+ )
271298 elif isinstance (params , utils .DictType ):
272299 try :
273300 new_obj = json_class (** params )
274301 except TypeError as ex :
275- raise TranslationError ("Error instantiating {0}: {1}"
276- .format (json_class .__name__ , ex ))
302+ raise TranslationError (
303+ "Error instantiating {0}: {1}" .format (json_class .__name__ , ex )
304+ )
277305 else :
278- raise TranslationError ("Constructor args must be a dict or a list, "
279- "not {0}" .format (type (params ).__name__ ))
306+ raise TranslationError (
307+ "Constructor args must be a dict or a list, "
308+ "not {0}" .format (type (params ).__name__ )
309+ )
280310
281311 # Remove the class information, as it must be ignored during the
282312 # reconstruction of the object
283- raw_jsonclass = obj .pop (' __jsonclass__' )
313+ raw_jsonclass = obj .pop (" __jsonclass__" )
284314
285315 for key , value in obj .items ():
286316 # Recursive loading
287317 setattr (new_obj , key , load (value , classes ))
288318
289319 # Restore the class information for further usage
290- obj [' __jsonclass__' ] = raw_jsonclass
320+ obj [" __jsonclass__" ] = raw_jsonclass
291321
292322 return new_obj
0 commit comments