<s> """<STR_LIT>""" <EOL> import logging <EOL> from grr . lib import rdfvalue <EOL> from grr . lib import type_info <EOL> from grr . proto import semantic_pb2 <EOL> TYPE_DOUBLE = <NUM_LIT:1> <EOL> TYPE_FLOAT = <NUM_LIT:2> <EOL> TYPE_INT64 = <NUM_LIT:3> <EOL> TYPE_UINT64 = <NUM_LIT:4> <EOL> TYPE_INT32 = <NUM_LIT:5> <EOL> TYPE_FIXED64 = <NUM_LIT:6> <EOL> TYPE_FIXED32 = <NUM_LIT:7> <EOL> TYPE_BOOL = <NUM_LIT:8> <EOL> TYPE_STRING = <NUM_LIT:9> <EOL> TYPE_GROUP = <NUM_LIT:10> <EOL> TYPE_MESSAGE = <NUM_LIT:11> <EOL> TYPE_BYTES = <NUM_LIT:12> <EOL> TYPE_UINT32 = <NUM_LIT> <EOL> TYPE_ENUM = <NUM_LIT> <EOL> TYPE_SFIXED32 = <NUM_LIT:15> <EOL> TYPE_SFIXED64 = <NUM_LIT:16> <EOL> TYPE_SINT32 = <NUM_LIT> <EOL> TYPE_SINT64 = <NUM_LIT> <EOL> MAX_TYPE = <NUM_LIT> <EOL> LABEL_OPTIONAL = <NUM_LIT:1> <EOL> LABEL_REQUIRED = <NUM_LIT:2> <EOL> LABEL_REPEATED = <NUM_LIT:3> <EOL> MAX_LABEL = <NUM_LIT:3> <EOL> _SEMANTIC_PRIMITIVE_TO_FIELD_TYPE = dict ( <EOL> bytes = TYPE_BYTES , <EOL> string = TYPE_STRING , <EOL> integer = TYPE_INT64 , <EOL> unsigned_integer = TYPE_UINT64 , <EOL> ) <EOL> def DefineFromProtobuf ( cls , protobuf ) : <EOL> """<STR_LIT>""" <EOL> message_options = protobuf . DESCRIPTOR . GetOptions ( ) <EOL> semantic_options = message_options . Extensions [ semantic_pb2 . semantic ] <EOL> if semantic_options . description and not cls . __doc__ : <EOL> cls . __doc__ = semantic_options . description <EOL> cls . union_field = semantic_options . union_field or None <EOL> for field in protobuf . DESCRIPTOR . fields : <EOL> type_descriptor = None <EOL> options = field . GetOptions ( ) . Extensions [ semantic_pb2 . sem_type ] <EOL> kwargs = dict ( description = options . description , name = field . name , <EOL> friendly_name = options . friendly_name , <EOL> field_number = field . number , labels = list ( options . label ) ) <EOL> if field . has_default_value : <EOL> kwargs [ "<STR_LIT:default>" ] = field . default_value <EOL> if options . type and field . type != TYPE_MESSAGE : <EOL> rdf_type = getattr ( rdfvalue , options . type , None ) <EOL> if rdf_type : <EOL> required_field_type = _SEMANTIC_PRIMITIVE_TO_FIELD_TYPE [ <EOL> rdf_type . data_store_type ] <EOL> if required_field_type != field . type : <EOL> raise rdfvalue . InitializeError ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( <EOL> cls . __name__ , field . name , rdf_type . data_store_type ) ) <EOL> type_descriptor = type_info . ProtoRDFValue ( rdf_type = options . type , ** kwargs ) <EOL> elif options . type and field . type == TYPE_MESSAGE : <EOL> raise rdfvalue . InitializeError ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( <EOL> cls . __name__ , options . type , field . message_type . name ) ) <EOL> elif field . type == TYPE_DOUBLE : <EOL> type_descriptor = type_info . ProtoDouble ( ** kwargs ) <EOL> elif field . type == TYPE_FLOAT : <EOL> type_descriptor = type_info . ProtoFloat ( ** kwargs ) <EOL> elif field . type == TYPE_BOOL : <EOL> type_descriptor = type_info . ProtoBoolean ( ** kwargs ) <EOL> elif field . type == TYPE_STRING : <EOL> type_descriptor = type_info . ProtoString ( ** kwargs ) <EOL> elif field . type == TYPE_BYTES : <EOL> type_descriptor = type_info . ProtoBinary ( ** kwargs ) <EOL> if options . dynamic_type : <EOL> dynamic_cb = getattr ( cls , options . dynamic_type , None ) <EOL> if dynamic_cb is not None : <EOL> type_descriptor = type_info . ProtoDynamicEmbedded ( <EOL> dynamic_cb = dynamic_cb , ** kwargs ) <EOL> else : <EOL> logging . warning ( "<STR_LIT>" , <EOL> options . dynamic_type ) <EOL> elif ( field . type == TYPE_MESSAGE and options . dynamic_type and <EOL> field . message_type . name == "<STR_LIT>" ) : <EOL> dynamic_cb = getattr ( cls , options . dynamic_type , None ) <EOL> if dynamic_cb is not None : <EOL> type_descriptor = type_info . ProtoDynamicAnyValueEmbedded ( <EOL> dynamic_cb = dynamic_cb , ** kwargs ) <EOL> else : <EOL> logging . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , options . dynamic_type ) <EOL> elif field . type == TYPE_INT64 or field . type == TYPE_INT32 : <EOL> type_descriptor = type_info . ProtoSignedInteger ( ** kwargs ) <EOL> elif field . type == TYPE_UINT32 or field . type == TYPE_UINT64 : <EOL> type_descriptor = type_info . ProtoUnsignedInteger ( ** kwargs ) <EOL> elif field . type == TYPE_MESSAGE and field . message_type : <EOL> type_descriptor = type_info . ProtoEmbedded ( <EOL> nested = field . message_type . name , ** kwargs ) <EOL> if type_descriptor . type : <EOL> semantic_protobuf_primitive = type_descriptor . type . protobuf . __name__ <EOL> if semantic_protobuf_primitive != field . message_type . name : <EOL> raise rdfvalue . InitializeError ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( <EOL> cls . __name__ , field . name , field . message_type . name , <EOL> type_descriptor . type . __name__ , semantic_protobuf_primitive ) ) <EOL> elif field . enum_type : <EOL> enum_desc = field . enum_type <EOL> enum_dict = { } <EOL> enum_descriptions = { } <EOL> enum_labels = { } <EOL> for enum_value in enum_desc . values : <EOL> enum_dict [ enum_value . name ] = enum_value . number <EOL> description = enum_value . GetOptions ( ) . Extensions [ <EOL> semantic_pb2 . description ] <EOL> enum_descriptions [ enum_value . name ] = description <EOL> labels = [ label for label in <EOL> enum_value . GetOptions ( ) . Extensions [ semantic_pb2 . label ] ] <EOL> enum_labels [ enum_value . name ] = labels <EOL> enum_dict = dict ( ( x . name , x . number ) for x in enum_desc . values ) <EOL> type_descriptor = type_info . ProtoEnum ( <EOL> enum_name = enum_desc . name , enum = enum_dict , <EOL> enum_descriptions = enum_descriptions , <EOL> enum_labels = enum_labels , ** kwargs ) <EOL> setattr ( cls , enum_desc . name , type_descriptor . enum_container ) <EOL> if type_descriptor is not None : <EOL> if field . label == LABEL_REPEATED : <EOL> options = field . GetOptions ( ) . Extensions [ semantic_pb2 . sem_type ] <EOL> type_descriptor = type_info . ProtoList ( type_descriptor , <EOL> labels = list ( options . label ) ) <EOL> try : <EOL> cls . AddDescriptor ( type_descriptor ) <EOL> except Exception : <EOL> logging . error ( "<STR_LIT>" , cls ) <EOL> raise <EOL> else : <EOL> logging . error ( "<STR_LIT>" , field . name ) </s>
