<s> """<STR_LIT>""" <EOL> import base64 <EOL> import itertools <EOL> import logging <EOL> from google . protobuf . descriptor import FieldDescriptor <EOL> logger = logging . getLogger ( __name__ ) <EOL> def _decode_field ( message , field , value ) : <EOL> """<STR_LIT>""" <EOL> if field . type == FieldDescriptor . TYPE_MESSAGE : <EOL> decode ( getattr ( message , field . name ) , value ) <EOL> else : <EOL> try : <EOL> if field . type == FieldDescriptor . TYPE_BYTES : <EOL> value = base64 . b64decode ( value ) <EOL> setattr ( message , field . name , value ) <EOL> except ( ValueError , TypeError ) as e : <EOL> logger . warning ( '<STR_LIT>' , <EOL> message . __class__ . __name__ , field . name , e ) <EOL> def _decode_repeated_field ( message , field , value_list ) : <EOL> """<STR_LIT>""" <EOL> if field . type == FieldDescriptor . TYPE_MESSAGE : <EOL> for value in value_list : <EOL> decode ( getattr ( message , field . name ) . add ( ) , value ) <EOL> else : <EOL> try : <EOL> for value in value_list : <EOL> if field . type == FieldDescriptor . TYPE_BYTES : <EOL> value = base64 . b64decode ( value ) <EOL> getattr ( message , field . name ) . append ( value ) <EOL> except ( ValueError , TypeError ) as e : <EOL> logger . warning ( '<STR_LIT>' , <EOL> message . __class__ . __name__ , field . name , e ) <EOL> message . ClearField ( field . name ) <EOL> def decode ( message , pblite , ignore_first_item = False ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( pblite , list ) : <EOL> logger . warning ( '<STR_LIT>' , <EOL> type ( pblite ) ) <EOL> return <EOL> if ignore_first_item : <EOL> pblite = pblite [ <NUM_LIT:1> : ] <EOL> if len ( pblite ) > <NUM_LIT:0> and isinstance ( pblite [ - <NUM_LIT:1> ] , dict ) : <EOL> extra_fields = { int ( field_number ) : value for field_number , value <EOL> in pblite [ - <NUM_LIT:1> ] . items ( ) } <EOL> pblite = pblite [ : - <NUM_LIT:1> ] <EOL> else : <EOL> extra_fields = { } <EOL> fields_values = itertools . chain ( enumerate ( pblite , start = <NUM_LIT:1> ) , <EOL> extra_fields . items ( ) ) <EOL> for field_number , value in fields_values : <EOL> if value is None : <EOL> continue <EOL> try : <EOL> field = message . DESCRIPTOR . fields_by_number [ field_number ] <EOL> except KeyError : <EOL> if value not in [ [ ] , '<STR_LIT>' , <NUM_LIT:0> ] : <EOL> logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , message . __class__ . __name__ , field_number , <EOL> value ) <EOL> continue <EOL> if field . label == FieldDescriptor . LABEL_REPEATED : <EOL> _decode_repeated_field ( message , field , value ) <EOL> else : <EOL> _decode_field ( message , field , value ) </s>
