<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import cStringIO <EOL> import base64 <EOL> import logging <EOL> from . import message_types <EOL> from . import messages <EOL> from . import util <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> CONTENT_TYPE = '<STR_LIT:application/json>' <EOL> ALTERNATIVE_CONTENT_TYPES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def _load_json_module ( ) : <EOL> """<STR_LIT>""" <EOL> first_import_error = None <EOL> for module_name in [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> try : <EOL> module = __import__ ( module_name , { } , { } , '<STR_LIT>' ) <EOL> if not hasattr ( module , '<STR_LIT>' ) : <EOL> message = ( '<STR_LIT>' % <EOL> module_name ) <EOL> logging . warning ( message ) <EOL> raise ImportError ( message ) <EOL> else : <EOL> return module <EOL> except ImportError , err : <EOL> if not first_import_error : <EOL> first_import_error = err <EOL> logging . error ( '<STR_LIT>' ) <EOL> raise first_import_error <EOL> json = _load_json_module ( ) <EOL> class MessageJSONEncoder ( json . JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def default ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , messages . Enum ) : <EOL> return str ( value ) <EOL> if isinstance ( value , messages . Message ) : <EOL> result = { } <EOL> for field in value . all_fields ( ) : <EOL> item = value . get_assigned_value ( field . name ) <EOL> if item not in ( None , [ ] , ( ) ) : <EOL> if isinstance ( field , messages . BytesField ) : <EOL> if field . repeated : <EOL> item = [ base64 . b64encode ( i ) for i in item ] <EOL> else : <EOL> item = base64 . b64encode ( item ) <EOL> elif isinstance ( field , message_types . DateTimeField ) : <EOL> if field . repeated : <EOL> item = [ i . isoformat ( ) for i in item ] <EOL> else : <EOL> item = item . isoformat ( ) <EOL> result [ field . name ] = item <EOL> for unknown_key in value . all_unrecognized_fields ( ) : <EOL> unrecognized_field , _ = value . get_unrecognized_field_info ( unknown_key ) <EOL> result [ unknown_key ] = unrecognized_field <EOL> return result <EOL> else : <EOL> return super ( MessageJSONEncoder , self ) . default ( value ) <EOL> class _MessageJSONEncoder ( MessageJSONEncoder ) : <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> logging . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> super ( _MessageJSONEncoder , self ) . __init__ ( * args , ** kwds ) <EOL> def encode_message ( message ) : <EOL> """<STR_LIT>""" <EOL> message . check_initialized ( ) <EOL> return json . dumps ( message , cls = MessageJSONEncoder ) <EOL> def decode_message ( message_type , encoded_message ) : <EOL> """<STR_LIT>""" <EOL> if not encoded_message . strip ( ) : <EOL> return message_type ( ) <EOL> dictionary = json . loads ( encoded_message ) <EOL> def find_variant ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , ( int , long ) ) : <EOL> return messages . Variant . INT64 <EOL> elif isinstance ( value , float ) : <EOL> return messages . Variant . DOUBLE <EOL> elif isinstance ( value , basestring ) : <EOL> return messages . Variant . STRING <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> variant_priority = [ None , messages . Variant . INT64 , messages . Variant . DOUBLE , <EOL> messages . Variant . STRING ] <EOL> chosen_priority = <NUM_LIT:0> <EOL> for v in value : <EOL> variant = find_variant ( v ) <EOL> try : <EOL> priority = variant_priority . index ( variant ) <EOL> except IndexError : <EOL> priority = - <NUM_LIT:1> <EOL> if priority > chosen_priority : <EOL> chosen_priority = priority <EOL> return variant_priority [ chosen_priority ] <EOL> return None <EOL> def decode_dictionary ( message_type , dictionary ) : <EOL> """<STR_LIT>""" <EOL> message = message_type ( ) <EOL> for key , value in dictionary . iteritems ( ) : <EOL> if value is None : <EOL> message . reset ( key ) <EOL> continue <EOL> try : <EOL> field = message . field_by_name ( key ) <EOL> except KeyError : <EOL> variant = find_variant ( value ) <EOL> if variant : <EOL> message . set_unrecognized_field ( key , value , variant ) <EOL> else : <EOL> logging . warning ( '<STR_LIT>' , key ) <EOL> continue <EOL> if isinstance ( value , list ) : <EOL> if not value : <EOL> continue <EOL> else : <EOL> value = [ value ] <EOL> valid_value = [ ] <EOL> for item in value : <EOL> if isinstance ( field , messages . EnumField ) : <EOL> try : <EOL> item = field . type ( item ) <EOL> except TypeError : <EOL> raise messages . DecodeError ( '<STR_LIT>' % value [ <NUM_LIT:0> ] ) <EOL> elif isinstance ( field , messages . BytesField ) : <EOL> try : <EOL> item = base64 . b64decode ( item ) <EOL> except TypeError , err : <EOL> raise messages . DecodeError ( '<STR_LIT>' % err ) <EOL> elif isinstance ( field , message_types . DateTimeField ) : <EOL> try : <EOL> item = util . decode_datetime ( item ) <EOL> except ValueError , err : <EOL> raise messages . DecodeError ( err ) <EOL> elif isinstance ( field , messages . MessageField ) : <EOL> item = decode_dictionary ( field . type , item ) <EOL> elif ( isinstance ( field , messages . FloatField ) and <EOL> isinstance ( item , ( int , long , basestring ) ) ) : <EOL> try : <EOL> item = float ( item ) <EOL> except : <EOL> pass <EOL> elif ( isinstance ( field , messages . IntegerField ) and <EOL> isinstance ( item , basestring ) ) : <EOL> try : <EOL> item = int ( item ) <EOL> except : <EOL> pass <EOL> valid_value . append ( item ) <EOL> if field . repeated : <EOL> existing_value = getattr ( message , field . name ) <EOL> setattr ( message , field . name , valid_value ) <EOL> else : <EOL> setattr ( message , field . name , valid_value [ - <NUM_LIT:1> ] ) <EOL> return message <EOL> message = decode_dictionary ( message_type , dictionary ) <EOL> message . check_initialized ( ) <EOL> return message </s>
