def testExtremeDoubleValues(self, message_module):
    message = message_module.TestAllTypes()

    # Most positive exponent, no significand bits set.
    kMostPosExponentNoSigBits = math.pow(2, 1023)
    message.optional_double = kMostPosExponentNoSigBits
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == kMostPosExponentNoSigBits)

    # Most positive exponent, one significand bit set.
    kMostPosExponentOneSigBit = 1.5 * math.pow(2, 1023)
    message.optional_double = kMostPosExponentOneSigBit
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == kMostPosExponentOneSigBit)

    # Repeat last two cases with values of same magnitude, but negative.
    message.optional_double = -kMostPosExponentNoSigBits
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == -kMostPosExponentNoSigBits)

    message.optional_double = -kMostPosExponentOneSigBit
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == -kMostPosExponentOneSigBit)

    # Most negative exponent, no significand bits set.
    kMostNegExponentNoSigBits = math.pow(2, -1023)
    message.optional_double = kMostNegExponentNoSigBits
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == kMostNegExponentNoSigBits)

    # Most negative exponent, one significand bit set.
    kMostNegExponentOneSigBit = 1.5 * math.pow(2, -1023)
    message.optional_double = kMostNegExponentOneSigBit
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == kMostNegExponentOneSigBit)

    # Repeat last two cases with values of the same magnitude, but negative.
    message.optional_double = -kMostNegExponentNoSigBits
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == -kMostNegExponentNoSigBits)

    message.optional_double = -kMostNegExponentOneSigBit
    message.ParseFromString(message.SerializeToString())
    self.assertTrue(message.optional_double == -kMostNegExponentOneSigBit)
Exemplo n.º 2
0
    def testExtremeFloatValues(self):
        message = unittest_pb2.TestAllTypes()

        # Most positive exponent, no significand bits set.
        kMostPosExponentNoSigBits = math.pow(2, 127)
        message.optional_float = kMostPosExponentNoSigBits
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == kMostPosExponentNoSigBits)

        # Most positive exponent, one significand bit set.
        kMostPosExponentOneSigBit = 1.5 * math.pow(2, 127)
        message.optional_float = kMostPosExponentOneSigBit
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == kMostPosExponentOneSigBit)

        # Repeat last two cases with values of same magnitude, but negative.
        message.optional_float = -kMostPosExponentNoSigBits
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == -kMostPosExponentNoSigBits)

        message.optional_float = -kMostPosExponentOneSigBit
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == -kMostPosExponentOneSigBit)

        # Most negative exponent, no significand bits set.
        kMostNegExponentNoSigBits = math.pow(2, -127)
        message.optional_float = kMostNegExponentNoSigBits
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == kMostNegExponentNoSigBits)

        # Most negative exponent, one significand bit set.
        kMostNegExponentOneSigBit = 1.5 * math.pow(2, -127)
        message.optional_float = kMostNegExponentOneSigBit
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == kMostNegExponentOneSigBit)

        # Repeat last two cases with values of the same magnitude, but negative.
        message.optional_float = -kMostNegExponentNoSigBits
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == -kMostNegExponentNoSigBits)

        message.optional_float = -kMostNegExponentOneSigBit
        message.ParseFromString(message.SerializeToString())
        self.assertTrue(message.optional_float == -kMostNegExponentOneSigBit)
Exemplo n.º 3
0
    def serialize_input(self, args=None, kwargs=None):
        args_len = 0
        kwargs_len = 0
        if args:
            args_len = len(args)
        if kwargs:
            kwargs_len = len(kwargs)

        total_len = args_len + kwargs_len
        if total_len == 0:
            raise Exception('Serializer cannot be called with 0 arguments')
        if total_len > 1:
            raise Exception(
                'Protobuf serializer cannot have more than one argument for an input'
            )

        # Because we checked the total length of all of the args, these two things are mutually exclusive
        if args_len > 0:
            message = args[0]
        if kwargs_len > 0:
            message = kwargs.items()[0]

        return message.SerializeToString()
Exemplo n.º 4
0
Arquivo: pbutil.py Projeto: 50417/phd
def ToFile(
    message: ProtocolBuffer,
    path: pathlib.Path,
    exist_ok: bool = True,
    assume_filename: typing.Optional[typing.Union[str, pathlib.Path]] = None
) -> ProtocolBuffer:
    """Write a protocol buffer to a file.

  This method uses attempts to guess the encoding from the path suffix,
  supporting binary, text, and json formatted messages. The mapping of suffixes
  to formatting is, in order:
      *.txt.gz: Gzipped text format.
      *.txt: Text format.
      *.pbtxt.gz: Gzipped text format.
      *.pbtxt: Text format.
      *.json.gz: Gzipped JSON format.
      *.json: JSON format.
      *.gz: Gzipped binary format.
      *: Binary format.

  Args:
    message: A message instance to write to file. The message must be
      initialized, i.e. have all required fields set.
    path: Path to the proto file.
    exist_ok: If True, overwrite existing file.
    assume_filename: For the purpose of determining the encoding from the file
      extension, use this name rather than the true path.

  Returns:
    The parsed message (same as the message argument).

  Raises:
    EncodeError: If the message is not initialized, i.e. it is missing required
      fields.
    FileNotFoundError: If the parent directory of the requested path does not
      exist.
    IsADirectoryError: If the requested path is a directory.
    FileExistsError: If the requested path already exists and exist_ok is False.
  """
    if not exist_ok and path.exists():
        raise FileExistsError(f'Refusing to overwrite {path}')

    # The SerializeToString() method refuses to encode a message which is not
    # initialized, whereas the MessageToString() and MessageToJson() methods do
    # not. This API should be consistent, so we enforce that all formats require
    # the message to be initialized.
    if not message.IsInitialized():
        class_name = type(message).__name__
        raise EncodeError(f"Required fields not set: '{class_name}'")

    suffixes = pathlib.Path(
        assume_filename).suffixes if assume_filename else path.suffixes
    if suffixes and suffixes[-1] == '.gz':
        suffixes.pop()
        open_function = gzip.open
    else:
        open_function = open

    suffix = suffixes[-1] if suffixes else ''
    mode = 'wt' if suffix in {'.txt', '.pbtxt', '.json'} else 'wb'

    with open_function(path, mode) as f:
        if suffix == '.txt' or suffix == '.pbtxt':
            f.write(google.protobuf.text_format.MessageToString(message))
        elif suffix == '.json':
            f.write(
                google.protobuf.json_format.MessageToJson(
                    message, preserving_proto_field_name=True))
        else:
            f.write(message.SerializeToString())

    return message
Exemplo n.º 5
0
def ToFile(message: ProtocolBuffer, path: pathlib.Path,
           exist_ok: bool = True,
           assume_filename: typing.Optional[
             typing.Union[str, pathlib.Path]] = None) -> ProtocolBuffer:
  """Write a protocol buffer to a file.

  This method uses attempts to guess the encoding from the path suffix,
  supporting binary, text, and json formatted messages. The mapping of suffixes
  to formatting is, in order:
      *.txt.gz: Gzipped text format.
      *.txt: Text format.
      *.pbtxt.gz: Gzipped text format.
      *.pbtxt: Text format.
      *.json.gz: Gzipped JSON format.
      *.json: JSON format.
      *.gz: Gzipped binary format.
      *: Binary format.

  Args:
    message: A message instance to write to file. The message must be
      initialized, i.e. have all required fields set.
    path: Path to the proto file.
    exist_ok: If True, overwrite existing file.
    assume_filename: For the purpose of determining the encoding from the file
      extension, use this name rather than the true path.

  Returns:
    The parsed message (same as the message argument).

  Raises:
    EncodeError: If the message is not initialized, i.e. it is missing required
      fields.
    FileNotFoundError: If the parent directory of the requested path does not
      exist.
    IsADirectoryError: If the requested path is a directory.
    FileExistsError: If the requested path already exists and exist_ok is False.
  """
  if not exist_ok and path.exists():
    raise FileExistsError(f'Refusing to overwrite {path}')

  # The SerializeToString() method refuses to encode a message which is not
  # initialized, whereas the MessageToString() and MessageToJson() methods do
  # not. This API should be consistent, so we enforce that all formats require
  # the message to be initialized.
  if not message.IsInitialized():
    class_name = type(message).__name__
    raise EncodeError(f"Required fields not set: '{class_name}'")

  suffixes = pathlib.Path(
      assume_filename).suffixes if assume_filename else path.suffixes
  if suffixes and suffixes[-1] == '.gz':
    suffixes.pop()
    open_function = gzip.open
  else:
    open_function = open

  suffix = suffixes[-1] if suffixes else ''
  mode = 'wt' if suffix in {'.txt', '.pbtxt', '.json','.mdl'} else 'wb'
  with open_function(path, mode) as f:

    if suffix == '.txt' or suffix == '.pbtxt':
      f.write(google.protobuf.text_format.MessageToString(message))
    elif suffix == '.json':
      f.write(google.protobuf.json_format.MessageToJson(
          message, preserving_proto_field_name=True))
    elif suffix == '.mdl':
      '''keywords =        ["slope"      , "ContentPreviewEnabled"      , "ScreenColor"      , "OutValues"      , "SrcBlock"      , "SampleTime"      , "ICPrevInput"      , "Filename"      , "Branch"      , "SID"      , "BlockType"      , "ymin"      , "Port"      , "OutputDataTypeScalingMode"      , "ymax"      , "TimeValues"      , "SrcPort"      , "SourceBlock"      , "SaturateOnIntegerOverflow"      , "start"      , "includeCurrent"      , "ModelBrowserVisibility"      , "X0"      , "ZeroZ"      , "OutputPortMap"      , "rep_seq_t"      , "SaveFormat"      , "Save2DSignal"      , "Ts"      , "LibraryVersion"      , "rep_seq_y"      , "Value"      , "PaperType"      , "Points"      , "T"      , "FixptAsFi"      , "Position"      , "InitialConditionSetting"      , "DoSatur"      , "Ports"      , "OutMax"      , "NumInputPorts"      , "Decimation"      , "Period"      , "ShowPageBoundaries"      , "PoleZ"      , "InputProcessing"      , "OutMin"      , "xmax"      , "Name"      , "PaperOrientation"      , "VariableName"      , "SIDHighWatermark"      , "DelayLengthSource"      , "f1"      , "f2"      , "Gain"      , "PulseType"      , "System"      , "tsamp"      , "PaperPositionMode"      , "ShowEnablePort"      , "seed"      , "Poles"      , "Location"      , "Units"      , "PortBlocksUseCompactNotation"      , "LookUpMeth"      , "ConRadixGroup"      , "TiledPageScale"      , "ReportName"      , "xmin"      , "uplimit"      , "DstBlock"      , "ZoomFactor"      , "InputPortMap"      , "TiledPaperMargins"      , "After"      , "SourceProductName"      , "ZOrder"      , "NumDelays"      , "OutScaling"      , "Line"      , "PaperUnits"      , "RndMeth"      , "Cov"      , "NumBits"      , "ICPrevOutput"      , "DelayOrder"      , "SourceProductBaseCode"      , "IconDisplay"      , "LockScale"      , "Model"      , "samptime"      , "{"      , "Block"      , "SourceType"      , "ICPrevScaledInput"      , "Open"      , "st"      , "PulseWidth"      , "MaxDataPoints"      , "VectorParams1D"      , "OutDataType"      , "Floating"      , "DstPort"      , "}"      , "vinit"      , "ModelBrowserWidth"      , "OutDataTypeStr"]
      outputString = ""

      for txt in message.text.split():
        if txt in keywords:
          outputString = outputString+"\n" + txt 
        else: 
          outputString= outputString+ " "+ txt
       ''' 
      
      f.write(message.text) # TODO: Needs to be changed to also capture other information
    else:
      f.write(message.SerializeToString())

  return message
Exemplo n.º 6
0
    def _dict_to_protobuf(cls, data):
        message = vonxc_pb2.RadioVehicleMessage()
        message.version = vonxc_pb2.RadioVehicleMessage.VERSION_1_0
        message.timestamp = int(time.time())

        if 'device_control' in data:
            message.type = vonxc_pb2.RadioVehicleMessage.DEVICE_CONTROL_REQ
            command = data['device_control']
            if command == 'VERSION_ID':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.VERSION_ID
            if command == 'FWUP':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.FWUP
            if command == 'GET_SERIAL':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.GET_SERIAL
            if command == 'DEVICE_RESET':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.DEVICE_RESET
            if command == 'GET_RTC':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.GET_RTC

            if command == 'FILE_READ_ID':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.FILE_READ_ID
                message.device_control_request.fread.value = data["length"]
                message.device_control_request.fread.offset = data["pos"]
                message.device_control_request.fread.id = data["id"]
            if command == 'FILE_OPEN_ID':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.FILE_OPEN_ID
                name = data["value"]
                mode = data["mode"]
                message.device_control_request.fopen.value = name
                message.device_control_request.fopen.mode = mode
            if command == 'FILE_WRITE_ID':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.FILE_WRITE_ID
                bytestream = data["value"]
                id = data["id"]
                message.device_control_request.fwrite.value = bytestream
                message.device_control_request.fwrite.id = id
            if command == 'FILE_CLOSE_ID':
                id = data["id"]
                message.device_control_request.fclose.id = id
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.FILE_CLOSE_ID
            if command == 'FILE_DELETE_ID':
                name = data["value"]
                message.device_control_request.fdelete.value = name
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.FILE_DELETE_ID
            if command == 'FIXED_VALUE_REQ':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.FIXED_VALUE_REQ

                fixedReq = data["fixedReq"]

                l = []
                for x in fixedReq:
                    print x
                    aValue = vonxc_pb2.FixedValue()
                    aValue.id = x['id']
                    aValue.isSensor = x['is_sensor']
                    print "aValue", aValue
                    l.append(aValue)

                print "l==================", l
                message.device_control_request.fixedReq.valueList.extend(l)
                print message.device_control_request.fixedReq.valueList
            if command == 'DYNAMIC_VALUE_REQ':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.DYNAMIC_VALUE_REQ

                dynamicReq = data["dynamicReq"]

                l = []
                for x in dynamicReq:
                    print x
                    aValue = vonxc_pb2.DynamicField()
                    aValue.id = x['id']
                    print "aValue", aValue
                    l.append(aValue)

                print "l==================", l
                message.device_control_request.dynamicReq.valueList.extend(l)
                print message.device_control_request.dynamicReq.valueList
            if command == 'CLI_CHANGE':
                message.device_control_request.type = vonxc_pb2.DeviceControlRequest.CLI_CHANGE

        print binascii.hexlify(bytearray(message.SerializeToString()))

        return message