<s> import types <EOL> import inspect <EOL> import re <EOL> import traceback <EOL> from jsonrpclib import config <EOL> iter_types = [ <EOL> types . DictType , <EOL> types . ListType , <EOL> types . TupleType <EOL> ] <EOL> string_types = [ <EOL> types . StringType , <EOL> types . UnicodeType <EOL> ] <EOL> numeric_types = [ <EOL> types . IntType , <EOL> types . LongType , <EOL> types . FloatType <EOL> ] <EOL> value_types = [ <EOL> types . BooleanType , <EOL> types . NoneType <EOL> ] <EOL> supported_types = iter_types + string_types + numeric_types + value_types <EOL> invalid_module_chars = r'<STR_LIT>' <EOL> class TranslationError ( Exception ) : <EOL> pass <EOL> def dump ( obj , serialize_method = None , ignore_attribute = None , ignore = [ ] ) : <EOL> if not serialize_method : <EOL> serialize_method = config . serialize_method <EOL> if not ignore_attribute : <EOL> ignore_attribute = config . ignore_attribute <EOL> obj_type = type ( obj ) <EOL> if obj_type in numeric_types + string_types + value_types : <EOL> return obj <EOL> if obj_type in iter_types : <EOL> if obj_type in ( types . ListType , types . TupleType ) : <EOL> new_obj = [ ] <EOL> for item in obj : <EOL> new_obj . append ( dump ( item , serialize_method , <EOL> ignore_attribute , ignore ) ) <EOL> if obj_type is types . TupleType : <EOL> new_obj = tuple ( new_obj ) <EOL> return new_obj <EOL> else : <EOL> new_obj = { } <EOL> for key , value in obj . iteritems ( ) : <EOL> new_obj [ key ] = dump ( value , serialize_method , <EOL> ignore_attribute , ignore ) <EOL> return new_obj <EOL> module_name = inspect . getmodule ( obj ) . __name__ <EOL> class_name = obj . __class__ . __name__ <EOL> json_class = class_name <EOL> if module_name not in [ '<STR_LIT>' , '<STR_LIT:__main__>' ] : <EOL> json_class = '<STR_LIT>' % ( module_name , json_class ) <EOL> return_obj = { "<STR_LIT>" : [ json_class , ] } <EOL> if serialize_method in dir ( obj ) : <EOL> serialize = getattr ( obj , serialize_method ) <EOL> params , attrs = serialize ( ) <EOL> return_obj [ '<STR_LIT>' ] . append ( params ) <EOL> return_obj . update ( attrs ) <EOL> return return_obj <EOL> return_obj [ '<STR_LIT>' ] . append ( [ ] ) <EOL> attrs = { } <EOL> ignore_list = getattr ( obj , ignore_attribute , [ ] ) + ignore <EOL> for attr_name , attr_value in obj . __dict__ . iteritems ( ) : <EOL> if type ( attr_value ) in supported_types and attr_name not in ignore_list and attr_value not in ignore_list : <EOL> attrs [ attr_name ] = dump ( attr_value , serialize_method , <EOL> ignore_attribute , ignore ) <EOL> return_obj . update ( attrs ) <EOL> return return_obj <EOL> def load ( obj ) : <EOL> if type ( obj ) in string_types + numeric_types + value_types : <EOL> return obj <EOL> if type ( obj ) is types . ListType : <EOL> return_list = [ ] <EOL> for entry in obj : <EOL> return_list . append ( load ( entry ) ) <EOL> return return_list <EOL> if '<STR_LIT>' not in obj . keys ( ) : <EOL> return_dict = { } <EOL> for key , value in obj . iteritems ( ) : <EOL> new_value = load ( value ) <EOL> return_dict [ key ] = new_value <EOL> return return_dict <EOL> orig_module_name = obj [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> params = obj [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> if orig_module_name == '<STR_LIT>' : <EOL> raise TranslationError ( '<STR_LIT>' ) <EOL> json_module_clean = re . sub ( invalid_module_chars , '<STR_LIT>' , orig_module_name ) <EOL> if json_module_clean != orig_module_name : <EOL> raise TranslationError ( '<STR_LIT>' % <EOL> orig_module_name ) <EOL> json_module_parts = json_module_clean . split ( '<STR_LIT:.>' ) <EOL> json_class = None <EOL> if len ( json_module_parts ) == <NUM_LIT:1> : <EOL> if json_module_parts [ <NUM_LIT:0> ] not in config . classes . keys ( ) : <EOL> raise TranslationError ( '<STR_LIT>' % <EOL> json_module_parts [ <NUM_LIT:0> ] ) <EOL> json_class = config . classes [ json_module_parts [ <NUM_LIT:0> ] ] <EOL> else : <EOL> json_class_name = json_module_parts . pop ( ) <EOL> json_module_tree = '<STR_LIT:.>' . join ( json_module_parts ) <EOL> try : <EOL> temp_module = __import__ ( json_module_tree ) <EOL> except ImportError : <EOL> raise TranslationError ( '<STR_LIT>' % <EOL> ( json_class_name , json_module_tree ) ) <EOL> json_class = getattr ( temp_module , json_class_name ) <EOL> new_obj = None <EOL> if type ( params ) is types . ListType : <EOL> new_obj = json_class ( * params ) <EOL> elif type ( params ) is types . DictType : <EOL> new_obj = json_class ( ** params ) <EOL> else : <EOL> raise TranslationError ( '<STR_LIT>' ) <EOL> for key , value in obj . iteritems ( ) : <EOL> if key == '<STR_LIT>' : <EOL> continue <EOL> setattr ( new_obj , key , value ) <EOL> return new_obj </s>
