def testFieldPresence(self): message = unittest_proto3_arena_pb2.TestAllTypes() # We can't test presence of non-repeated, non-submessage fields. with self.assertRaises(ValueError): message.HasField("optional_int32") with self.assertRaises(ValueError): message.HasField("optional_float") with self.assertRaises(ValueError): message.HasField("optional_string") with self.assertRaises(ValueError): message.HasField("optional_bool") # But we can still test presence of submessage fields. self.assertFalse(message.HasField("optional_nested_message")) # As with proto2, we can't test presence of fields that don't exist, or # repeated fields. with self.assertRaises(ValueError): message.HasField("field_doesnt_exist") with self.assertRaises(ValueError): message.HasField("repeated_int32") with self.assertRaises(ValueError): message.HasField("repeated_nested_message") # Fields should default to their type-specific default. self.assertEqual(0, message.optional_int32) self.assertEqual(0, message.optional_float) self.assertEqual("", message.optional_string) self.assertEqual(False, message.optional_bool) self.assertEqual(0, message.optional_nested_message.bb) # Setting a submessage should still return proper presence information. message.optional_nested_message.bb = 0 self.assertTrue(message.HasField("optional_nested_message")) # Set the fields to non-default values. message.optional_int32 = 5 message.optional_float = 1.1 message.optional_string = "abc" message.optional_bool = True message.optional_nested_message.bb = 15 # Clearing the fields unsets them and resets their value to default. message.ClearField("optional_int32") message.ClearField("optional_float") message.ClearField("optional_string") message.ClearField("optional_bool") message.ClearField("optional_nested_message") self.assertEqual(0, message.optional_int32) self.assertEqual(0, message.optional_float) self.assertEqual("", message.optional_string) self.assertEqual(False, message.optional_bool) self.assertEqual(0, message.optional_nested_message.bb)
def printQuery(self, message): if message.HasField('question'): qclass = 1 if message.question.HasField('qClass'): qclass = message.question.qClass print("- Question: %d, %d, %s" % (qclass, message.question.qType, message.question.qName))
def _protobuf_to_dict(self, message): parsed_message = {} if message.type == message.RAW and message.HasField('raw_message'): if message.raw_message.HasField('bus'): parsed_message['bus'] = message.raw_message.bus if message.raw_message.HasField('message_id'): parsed_message['id'] = message.raw_message.message_id if message.raw_message.HasField('data'): parsed_message['data'] = "0x%x" % message.raw_message.data elif message.type == message.TRANSLATED: parsed_message['name'] = message.translated_message.name if message.translated_message.HasField('numeric_event'): parsed_message[ 'event'] = message.translated_message.numeric_event elif message.translated_message.HasField('boolean_event'): parsed_message[ 'event'] = message.translated_message.boolean_event elif message.translated_message.HasField('string_event'): parsed_message[ 'event'] = message.translated_message.string_event if message.translated_message.HasField('numeric_value'): parsed_message[ 'value'] = message.translated_message.numeric_value elif message.translated_message.HasField('boolean_value'): parsed_message[ 'value'] = message.translated_message.boolean_value elif message.translated_message.HasField('string_value'): parsed_message[ 'value'] = message.translated_message.string_value else: parsed_message = None else: parsed_message = None return parsed_message
def printResponse(self, message): if message.HasField('response'): response = message.response if response.HasField('queryTimeSec'): datestr = datetime.datetime.fromtimestamp( response.queryTimeSec).strftime('%Y-%m-%d %H:%M:%S') if response.HasField('queryTimeUsec'): datestr = datestr + '.' + str(response.queryTimeUsec) print("- Query time: %s" % (datestr)) policystr = '' if response.HasField('appliedPolicy') and response.appliedPolicy: policystr = ', Applied policy: ' + response.appliedPolicy if response.HasField('appliedPolicyType'): policystr = policystr + ' (' + self.getAppliedPolicyTypeAsString( response.appliedPolicyType) + ')' if response.HasField('appliedPolicyTrigger'): policystr = policystr + ', Trigger = ' + response.appliedPolicyTrigger if response.HasField('appliedPolicyHit'): policystr = policystr + ', Hit = ' + response.appliedPolicyHit tagsstr = '' if response.tags: tagsstr = ', Tags: ' + ','.join(response.tags) rrscount = len(response.rrs) print("- Response Code: %d, RRs: %d%s%s" % (response.rcode, rrscount, policystr, tagsstr)) for rr in response.rrs: rrclass = 1 rdatastr = '' rrudr = 0 if rr.HasField('class'): rrclass = getattr(rr, 'class') rrtype = rr.type if rr.HasField('udr'): rrudr = rr.udr if (rrclass == 1 or rrclass == 255) and rr.HasField('rdata'): if rrtype == 1: rdatastr = socket.inet_ntop(socket.AF_INET, rr.rdata) elif rrtype == 5: rdatastr = rr.rdata elif rrtype == 28: rdatastr = socket.inet_ntop(socket.AF_INET6, rr.rdata) print("\t - %d, %d, %s, %d, %s, %d" % (rrclass, rrtype, rr.name, rr.ttl, rdatastr, rrudr))
def _protobuf_to_dict(self, message): parsed_message = {} if message.type == message.RAW and message.HasField('raw_message'): raw_message = message.raw_message if raw_message.HasField('bus'): parsed_message['bus'] = raw_message.bus if raw_message.HasField('message_id'): parsed_message['id'] = raw_message.message_id if raw_message.HasField('data'): parsed_message['data'] = "0x%s" % binascii.hexlify(raw_message.data) elif message.type == message.DIAGNOSTIC: diagnostic_message = message.diagnostic_message if diagnostic_message.HasField('bus'): parsed_message['bus'] = diagnostic_message.bus if diagnostic_message.HasField('message_id'): parsed_message['id'] = diagnostic_message.message_id if diagnostic_message.HasField('mode'): parsed_message['mode'] = diagnostic_message.mode if diagnostic_message.HasField('pid'): parsed_message['pid'] = diagnostic_message.pid if diagnostic_message.HasField('success'): parsed_message['success'] = diagnostic_message.success if diagnostic_message.HasField('negative_response_code'): parsed_message['negative_response_code'] = diagnostic_message.negative_response_code if diagnostic_message.HasField('payload'): parsed_message['payload'] = "0x%s" % binascii.hexlify(diagnostic_message.payload) elif message.type == message.TRANSLATED: translated_message = message.translated_message parsed_message['name'] = translated_message.name if translated_message.HasField('numeric_event'): parsed_message['event'] = translated_message.numeric_event elif translated_message.HasField('boolean_event'): parsed_message['event'] = translated_message.boolean_event elif translated_message.HasField('string_event'): parsed_message['event'] = translated_message.string_event if translated_message.HasField('numeric_value'): parsed_message['value'] = translated_message.numeric_value elif translated_message.HasField('boolean_value'): parsed_message['value'] = translated_message.boolean_value elif translated_message.HasField('string_value'): parsed_message['value'] = translated_message.string_value else: parsed_message = None else: parsed_message = None return parsed_message
def _protobuf_to_dict(cls, message): parsed_message = {} if message is not None: if message.type == message.CAN and message.HasField('can_message'): cls._build_can_parsed_message(message, parsed_message) elif message.type == message.DIAGNOSTIC: cls._build_diagnostic_parsed_message(message, parsed_message) elif message.type == message.SIMPLE: cls._build_simple_parsed_message(message, parsed_message) elif message.type == message.CONTROL_COMMAND: cls._build_control_command_parsed_message( message, parsed_message) elif message.type == message.COMMAND_RESPONSE: cls._build_command_response_parsed_message( message, parsed_message) else: parsed_message = None return parsed_message
def save_prototext(filename, **kwargs): """Save a prototext file. LbannPB fields (e.g. `model`, `data_reader`, `optimizer`) are accepted via `kwargs`. """ # Construct protobuf message message = lbann_pb2.LbannPB() field_names = message.DESCRIPTOR.fields_by_name.keys() # Make sure keyword arguments are valid for key, val in kwargs.items(): if key not in field_names: raise TypeError("'{}' is an invalid keyword " "argument for this function".format(key)) if val is not None: field = getattr(message, key) if isinstance(val, google.protobuf.message.Message): field.CopyFrom(val) else: field.CopyFrom(val.export_proto()) field.SetInParent() # Make sure default optimizer is set # TODO: This is a hack that should be removed when possible. LBANN # requires the prototext file to provide a default optimizer. It # would be better if LBANN used no optimizer if one isn't # provided. if not message.HasField('optimizer'): from lbann import Optimizer message.optimizer.CopyFrom(NoOptimizer().export_proto()) message.optimizer.SetInParent() # Write to file with open(filename, 'wb') as f: f.write( google.protobuf.text_format.MessageToString( message, use_index_order=True).encode())
def printResponse(self, message): if message.trace: print("- Event Trace:") for event in message.trace: ev = self.getEventAsString(event.event) if event.event == dnsmessage_pb2.PBDNSMessage.CustomEvent and event.HasField( 'custom'): ev += ":" + event.custom ev += '(' + str(event.ts) valstr = '' if event.HasField('boolVal'): valstr = str(event.boolVal) elif event.HasField('intVal'): valstr = str(event.intVal) elif event.HasField('stringVal'): valstr = event.stringVal elif event.HasField('bytesVal'): valstr = binascii.hexlify(event.bytesVal) if len(valstr) > 0: valstr = ',' + valstr if not event.start: startstr = ',done' else: startstr = '' print("\t- %s%s%s)" % (ev, valstr, startstr)) if message.HasField('response'): response = message.response if response.HasField('queryTimeSec'): datestr = datetime.datetime.fromtimestamp( response.queryTimeSec).strftime('%Y-%m-%d %H:%M:%S') if response.HasField('queryTimeUsec'): datestr = datestr + '.' + str(response.queryTimeUsec) print("- Query time: %s" % (datestr)) policystr = '' if response.HasField('appliedPolicy') and response.appliedPolicy: policystr = ', Applied policy: ' + response.appliedPolicy if response.HasField('appliedPolicyType'): policystr = policystr + ' (' + self.getAppliedPolicyTypeAsString( response.appliedPolicyType) + ')' if response.HasField('appliedPolicyTrigger'): policystr = policystr + ', Trigger = ' + response.appliedPolicyTrigger if response.HasField('appliedPolicyHit'): policystr = policystr + ', Hit = ' + response.appliedPolicyHit tagsstr = '' if response.tags: tagsstr = ', Tags: ' + ','.join(response.tags) rrscount = len(response.rrs) print("- Response Code: %d, RRs: %d%s%s" % (response.rcode, rrscount, policystr, tagsstr)) for rr in response.rrs: rrclass = 1 rdatastr = '' rrudr = 0 if rr.HasField('class'): rrclass = getattr(rr, 'class') rrtype = rr.type if rr.HasField('udr'): rrudr = rr.udr if (rrclass == 1 or rrclass == 255) and rr.HasField('rdata'): if rrtype == 1: rdatastr = socket.inet_ntop(socket.AF_INET, rr.rdata) elif rrtype == 5: rdatastr = rr.rdata elif rrtype == 28: rdatastr = socket.inet_ntop(socket.AF_INET6, rr.rdata) print("\t - %d, %d, %s, %d, %s, %d" % (rrclass, rrtype, rr.name, rr.ttl, rdatastr, rrudr))
def testFieldPresence(self): message = unittest_pb2.TestAllTypes() self.assertFalse(message.HasField("optional_int32")) self.assertFalse(message.HasField("optional_bool")) self.assertFalse(message.HasField("optional_nested_message")) with self.assertRaises(ValueError): message.HasField("field_doesnt_exist") with self.assertRaises(ValueError): message.HasField("repeated_int32") with self.assertRaises(ValueError): message.HasField("repeated_nested_message") self.assertEqual(0, message.optional_int32) self.assertEqual(False, message.optional_bool) self.assertEqual(0, message.optional_nested_message.bb) # Fields are set even when setting the values to default values. message.optional_int32 = 0 message.optional_bool = False message.optional_nested_message.bb = 0 self.assertTrue(message.HasField("optional_int32")) self.assertTrue(message.HasField("optional_bool")) self.assertTrue(message.HasField("optional_nested_message")) # Set the fields to non-default values. message.optional_int32 = 5 message.optional_bool = True message.optional_nested_message.bb = 15 self.assertTrue(message.HasField("optional_int32")) self.assertTrue(message.HasField("optional_bool")) self.assertTrue(message.HasField("optional_nested_message")) # Clearing the fields unsets them and resets their value to default. message.ClearField("optional_int32") message.ClearField("optional_bool") message.ClearField("optional_nested_message") self.assertFalse(message.HasField("optional_int32")) self.assertFalse(message.HasField("optional_bool")) self.assertFalse(message.HasField("optional_nested_message")) self.assertEqual(0, message.optional_int32) self.assertEqual(False, message.optional_bool) self.assertEqual(0, message.optional_nested_message.bb)
def _protobuf_to_dict(cls, message): parsed_message = {} if message is not None: if message.type == message.CAN and message.HasField('can_message'): can_message = message.can_message if can_message.HasField('bus'): parsed_message['bus'] = can_message.bus if can_message.HasField('id'): parsed_message['id'] = can_message.id if can_message.HasField('data'): parsed_message['data'] = "0x%s" % binascii.hexlify(can_message.data) if can_message.HasField('frame_format'): if can_message.frame_format == openxc_pb2.RawMessage.STANDARD: parsed_message['frame_format'] = "standard" elif can_message.frame_format == openxc_pb2.RawMessage.EXTENDED: parsed_message['frame_format'] = "extended" elif message.type == message.DIAGNOSTIC: diagnostic_message = message.diagnostic_response if diagnostic_message.HasField('bus'): parsed_message['bus'] = diagnostic_message.bus if diagnostic_message.HasField('message_id'): parsed_message['id'] = diagnostic_message.message_id if diagnostic_message.HasField('mode'): parsed_message['mode'] = diagnostic_message.mode if diagnostic_message.HasField('pid'): parsed_message['pid'] = diagnostic_message.pid if diagnostic_message.HasField('success'): parsed_message['success'] = diagnostic_message.success if diagnostic_message.HasField('value'): parsed_message['value'] = diagnostic_message.value if diagnostic_message.HasField('negative_response_code'): parsed_message['negative_response_code'] = diagnostic_message.negative_response_code if diagnostic_message.HasField('payload'): parsed_message['payload'] = "0x%s" % binascii.hexlify(diagnostic_message.payload) elif message.type == message.SIMPLE: simple_message = message.simple_message parsed_message['name'] = simple_message.name if simple_message.HasField('event'): event = simple_message.event if event.HasField('numeric_value'): parsed_message['event'] = event.numeric_value elif event.HasField('boolean_value'): parsed_message['event'] = event.boolean_value elif event.HasField('string_value'): parsed_message['event'] = event.string_value if simple_message.HasField('value'): value = simple_message.value if value.HasField('numeric_value'): parsed_message['value'] = value.numeric_value elif value.HasField('boolean_value'): parsed_message['value'] = value.boolean_value elif value.HasField('string_value'): parsed_message['value'] = value.string_value else: parsed_message = None else: parsed_message = None elif message.type == message.CONTROL_COMMAND: command = message.control_command if command.type == openxc_pb2.ControlCommand.VERSION: parsed_message['command'] = "version" elif command.type == openxc_pb2.ControlCommand.DEVICE_ID: parsed_message['command'] = "device_id" elif command.type == openxc_pb2.ControlCommand.DIAGNOSTIC: parsed_message['command'] = "diagnostic_request" parsed_message['request'] = {} action = command.diagnostic_request.action if action == openxc_pb2.DiagnosticControlCommand.ADD: parsed_message['action'] = "add" elif action == openxc_pb2.DiagnosticControlCommand.CANCEL: parsed_message['action'] = "cancel" request = command.diagnostic_request.request parsed_message['request']['id'] = request.message_id parsed_message['request']['bus'] = request.bus parsed_message['request']['mode'] = request.mode if request.HasField('frequency'): parsed_message['request']['frequency'] = request.frequency if request.HasField('name'): parsed_message['request']['name'] = request.name if request.HasField('multiple_responses'): parsed_message['request']['multiple_responses'] = request.multiple_responses if request.HasField('pid'): parsed_message['request']['pid'] = request.pid if request.HasField('payload'): parsed_message['request']['payload'] = "0x%s" % binascii.hexlify(request.payload) elif command.type == openxc_pb2.ControlCommand.PASSTHROUGH: parsed_message['command'] = "passthrough" parsed_message['bus'] = command.passthrough_mode_request.bus parsed_message['enabled'] = command.passthrough_mode_request.enabled elif command.type == openxc_pb2.ControlCommand.PREDEFINED_OBD2_REQUESTS: parsed_message['command'] = "predefined_obd2" parsed_message['enabled'] = command.predefined_obd2_requests_command.enabled elif command.type == openxc_pb2.ControlCommand.ACCEPTANCE_FILTER_BYPASS: parsed_message['command'] = "af_bypass" parsed_message['bus'] = command.acceptance_filter_bypass_command.bus parsed_message['bypass'] = command.acceptance_filter_bypass_command.bypass elif command.type == openxc_pb2.ControlCommand.PAYLOAD_FORMAT: parsed_message['command'] = "payload_format" if command.payload_format_command.format == openxc_pb2.PayloadFormatCommand.JSON: parsed_message['format'] = "json" elif command.payload_format_command.format == openxc_pb2.PayloadFormatCommand.PROTOBUF: parsed_message['format'] = "protobuf" elif message.type == message.COMMAND_RESPONSE: response = message.command_response if response.type == openxc_pb2.ControlCommand.VERSION: parsed_message['command_response'] = "version" elif response.type == openxc_pb2.ControlCommand.DEVICE_ID: parsed_message['command_response'] = "device_id" elif response.type == openxc_pb2.ControlCommand.DIAGNOSTIC: parsed_message['command_response'] = "diagnostic_request" elif response.type == openxc_pb2.ControlCommand.PASSTHROUGH: parsed_message['command_response'] = "passthrough" elif response.type == openxc_pb2.ControlCommand.PAYLOAD_FORMAT: parsed_message['command_response'] = "payload_format" elif response.type == openxc_pb2.ControlCommand.ACCEPTANCE_FILTER_BYPASS: parsed_message['command_response'] = "af_bypass" elif response.type == openxc_pb2.ControlCommand.PREDEFINED_OBD2_REQUESTS: parsed_message['command_response'] = "predefined_obd2" else: raise UnrecognizedBinaryCommandError(response.type) parsed_message['status'] = response.status if response.HasField('message'): parsed_message['message'] = response.message else: parsed_message = None return parsed_message