Пример #1
0
def transferToDataStructure(format,
                            dataStruct,
                            dStart,
                            buffer,
                            parentFormat,
                            isTopLevelStruct,
                            oclass=None):
    ftype = _IPC.formatType(format)

    if (ftype == LengthFMT):
        IPC.Raise("Python version of IPC can only use explicit formats")
    elif (ftype == PrimitiveFMT):
        try:
            dataStruct.__dict__  # Is this a settable object
        except AttributeError:
            IPC.Raise(
                invalidStructFormat(
                    type(dataStruct),
                    primFmttrs.PrimType(_IPC.formatPrimitiveProc(format))))
        primFmttrs.Decode(_IPC.formatPrimitiveProc(format), dataStruct, dStart,
                          buffer)
    elif (ftype == PointerFMT):
        theChar = _IPC.formatGetChar(buffer)
        if (theChar == '\0'):
            primFmttrs.setObjectField(dataStruct, dStart, None)
        else:
            transferToDataStructure(
                _IPC.formatChoosePtrFormat(format, parentFormat), dataStruct,
                dStart, buffer, 0, isTopLevelStruct, oclass)
    elif (ftype == StructFMT):
        formatArray = _IPC.formatFormatArray(format)
        structStart = 0
        n = _IPC.formatFormatArrayMax(formatArray)
        if (isTopLevelStruct):
            struct = dataStruct
        else:
            struct1 = primFmttrs.getObjectField(dataStruct, dStart)
            struct = validateObject(struct1, dataStruct, dStart)
            if (not struct == struct1):
                primFmttrs.setObjectField(dataStruct, dStart, struct)
        for i in range(1, n):
            transferToDataStructure(
                _IPC.formatFormatArrayItemPtr(formatArray, i), struct,
                structStart, buffer, format, False)
            structStart = structStart + 1
    elif (ftype == FixedArrayFMT):
        formatArray = _IPC.formatFormatArray(format)
        nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
        size = _IPC.formatFormatArrayItemInt(formatArray, 2)
        if (isTopLevelStruct and isinstance(dataStruct, (tuple, list))):
            arrayObject = dataStruct
        else:
            arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
            arrayObject = validateArrayObject(arrayObject, size, dataStruct,
                                              dStart)
            primFmttrs.setObjectField(dataStruct, dStart, arrayObject)

        arrayTransferToDataStructure(
            arrayObject, buffer, 2,
            _IPC.formatFormatArrayMax(formatArray) - 1, size,
            isSimpleType(nextFormat), formatArray, nextFormat, dataStruct,
            dStart, False, oclass)
    elif (ftype == VarArrayFMT):
        formatArray = _IPC.formatFormatArray(format)
        nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
        # The total size of the array is the stored first
        size = _IPC.formatGetInt(buffer)
        numDims = _IPC.formatFormatArrayMax(formatArray) - 2

        if (numDims > 1): size = varArrayDimSize(2, formatArray, dataStruct)

        if (not feasibleToDecodeVarArray(size, formatArray, dStart)):
            IPC.Raise("Python version of IPC cannot decode " + \
                      "multi-dimensional variable length arrays unless " + \
                      "the size variables appear BEFORE the array " + \
                      "in the enclosing structure")
        elif (isTopLevelStruct):
            arrayObject = dataStruct
        else:
            arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
            arrayObject = validateArrayObject(arrayObject, size, dataStruct,
                                              dStart)
            primFmttrs.setObjectField(dataStruct, dStart, arrayObject)

        arrayTransferToDataStructure(arrayObject, buffer, 2, numDims + 1, size,
                                     isSimpleType(nextFormat), formatArray,
                                     nextFormat, dataStruct, dStart, True,
                                     oclass)
    elif (ftype == NamedFMT):
        transferToDataStructure(_IPC.findNamedFormat(format), dataStruct,
                                dStart, buffer, parentFormat, isTopLevelStruct)
    elif (ftype == EnumFMT):
        primFmttrs.Decode(primFmttrs.INT_FMT, dataStruct, dStart, buffer)
    else:
        IPC.Raise("Unhandled format: %s" % ftype)
Пример #2
0
def transferToBuffer(format, dataStruct, dStart, buffer, parentFormat,
                     isTopLevelStruct):
    ftype = _IPC.formatType(format)

    if (ftype == LengthFMT):
        IPC.Raise("Python version of IPC can only use explicit formats")
    elif (ftype == PrimitiveFMT):
        if (isTopLevelStruct):
            object = IPCdata()
            object._f0 = dataStruct
            dataStruct = object
        primFmttrs.Encode(_IPC.formatPrimitiveProc(format), dataStruct, dStart,
                          buffer)
    elif (ftype == PointerFMT):
        object = primFmttrs.getObjectField(dataStruct, dStart)
        # 'Z' means data, 0 means NIL
        if (object is None): theChar = '\0'
        else: theChar = 'Z'
        _IPC.formatPutChar(buffer, theChar)
        if (not object is None):
            transferToBuffer(_IPC.formatChoosePtrFormat(format, parentFormat),
                             dataStruct, dStart, buffer, 0, isTopLevelStruct)
    elif (ftype == StructFMT):
        formatArray = _IPC.formatFormatArray(format)
        structStart = 0
        n = _IPC.formatFormatArrayMax(formatArray)
        if (isTopLevelStruct): struct = dataStruct
        else: struct = primFmttrs.getObjectField(dataStruct, dStart)
        for i in range(1, n):
            transferToBuffer(_IPC.formatFormatArrayItemPtr(formatArray, i),
                             struct, structStart, buffer, format, False)
            structStart = structStart + 1
    elif (ftype == FixedArrayFMT):
        formatArray = _IPC.formatFormatArray(format)
        nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
        if (isTopLevelStruct): arrayObject = dataStruct
        else: arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
        if (not isinstance(arrayObject, (tuple, list))):
            IPC.Raise(invalidArrayFormat(dataStruct, dStart))
        else:
            arrayTransferToBuffer(arrayObject, buffer, 2,
                                  _IPC.formatFormatArrayMax(formatArray) - 1,
                                  isSimpleType(nextFormat), formatArray,
                                  nextFormat, dataStruct, False)
    elif (ftype == VarArrayFMT):
        formatArray = _IPC.formatFormatArray(format)
        nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
        if (isTopLevelStruct): arrayObject = dataStruct
        else: arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
        # For the size of the array
        _IPC.formatPutInt(buffer, varArrayNumElements(formatArray, dataStruct))
        if (not isinstance(arrayObject, (tuple, list))):
            IPC.Raise(invalidArrayFormat(dataStruct, dStart))
        else:
            arrayTransferToBuffer(arrayObject, buffer, 2,
                                  _IPC.formatFormatArrayMax(formatArray) - 1,
                                  isSimpleType(nextFormat), formatArray,
                                  nextFormat, dataStruct, True)
    elif (ftype == NamedFMT):
        transferToBuffer(_IPC.findNamedFormat(format), dataStruct, dStart,
                         buffer, parentFormat, isTopLevelStruct)
    elif (ftype == EnumFMT):
        primFmttrs.Encode(primFmttrs.INT_FMT, dataStruct, dStart, buffer)
    else:
        IPC.Raise("Unhandled format: %s" % ftype)
Пример #3
0
def bufferSize1(format, dataStruct, dStart, parentFormat, isTopLevelStruct):
    bufSize = 0
    ftype = _IPC.formatType(format)

    if (ftype == LengthFMT):
        IPC.Raise("Python version of IPC can only use explicit formats")
    elif (ftype == PrimitiveFMT):
        primType = _IPC.formatPrimitiveProc(format)
        bufSize = bufSize + primFmttrs.ELength(primType, dataStruct, dStart)
    elif (ftype == PointerFMT):
        bufSize = bufSize + 1
        if (not primFmttrs.getObjectField(dataStruct, dStart) is None):
            bufSize = bufSize + \
                      bufferSize1(_IPC.formatChoosePtrFormat(format, parentFormat),
                                  dataStruct, dStart, 0, isTopLevelStruct)
    elif (ftype == StructFMT):
        formatArray = _IPC.formatFormatArray(format)
        structStart = 0
        n = _IPC.formatFormatArrayMax(formatArray)
        if (isTopLevelStruct):
            struct = dataStruct
        else:
            struct = primFmttrs.getObjectField(dataStruct, dStart)
        for i in range(1, n):
            bufSize = bufSize + \
                      bufferSize1(_IPC.formatFormatArrayItemPtr(formatArray, i),
                                  struct, structStart, format, False)
            structStart = structStart + 1
    elif (ftype == FixedArrayFMT):
        formatArray = _IPC.formatFormatArray(format)
        nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
        if (isTopLevelStruct):
            arrayObject = dataStruct
        else:
            arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
        if (not isinstance(arrayObject, (tuple, list))):
            IPC.Raise(invalidArrayFormat(dataStruct, dStart))
        elif (isSimpleType(nextFormat)):
            bufSize = bufSize + (
                bufferSize1(nextFormat, arrayObject, 0, 0, False) *
                fixedArrayNumElements(formatArray))
        else:
            bufSize = bufSize + \
                      arrayBufferSize(arrayObject, 2,
                                      _IPC.formatFormatArrayMax(formatArray)-1,
                                      formatArray, nextFormat, None)
    elif (ftype == VarArrayFMT):
        formatArray = _IPC.formatFormatArray(format)
        nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
        if (isTopLevelStruct):
            arrayObject = dataStruct
        else:
            arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
        # For the size of the array
        bufSize = bufSize + primFmttrs.ELength(primFmttrs.INT_FMT, None, 0)
        if (not isinstance(arrayObject, (tuple, list))):
            IPC.Raise(invalidArrayFormat(dataStruct, dStart))
        elif (isSimpleType(nextFormat)):
            bufSize = bufSize + (
                bufferSize1(nextFormat, arrayObject, 0, 0, False) *
                varArrayNumElements(formatArray, dataStruct))
        else:
            bufSize = bufSize + \
                      arrayBufferSize(arrayObject, 2,
                                      _IPC.formatFormatArrayMax(formatArray)-1,
                                      formatArray, nextFormat, dataStruct)
    elif (ftype == NamedFMT):
        bufSize = bufSize + \
                  bufferSize1(_IPC.findNamedFormat(format), dataStruct, dStart,
                              parentFormat, isTopLevelStruct)
    elif (ftype == EnumFMT):
        bufSize = bufSize + primFmttrs.ELength(primFmttrs.INT_FMT, None, 0)
    else:
        IPC.Raise("Unhandled format: %s" % ftype)

    return bufSize
Пример #4
0
def transferToDataStructure (format, dataStruct, dStart, buffer, parentFormat,
                             isTopLevelStruct, oclass=None) :
  ftype = _IPC.formatType(format)

  if (ftype == LengthFMT) :
    IPC.Raise("Python version of IPC can only use explicit formats")
  elif (ftype == PrimitiveFMT) :
    try: dataStruct.__dict__ # Is this a settable object
    except AttributeError:
      IPC.Raise(invalidStructFormat(type(dataStruct),
                                    primFmttrs.PrimType(_IPC.formatPrimitiveProc(format))))
    primFmttrs.Decode(_IPC.formatPrimitiveProc(format), dataStruct, dStart,
                      buffer)
  elif (ftype == PointerFMT) :
    theChar = _IPC.formatGetChar(buffer)
    if (theChar == '\0') :
      primFmttrs.setObjectField(dataStruct, dStart, None)
    else :
      transferToDataStructure(_IPC.formatChoosePtrFormat(format, parentFormat),
 		              dataStruct, dStart, buffer, 0, isTopLevelStruct,
                              oclass)
  elif (ftype == StructFMT) :
    formatArray = _IPC.formatFormatArray(format)
    structStart = 0
    n = _IPC.formatFormatArrayMax(formatArray)
    if (isTopLevelStruct) :
      struct = dataStruct
    else :
      struct1 = primFmttrs.getObjectField(dataStruct, dStart)
      struct = validateObject(struct1, dataStruct, dStart)
      if (not struct == struct1) :
        primFmttrs.setObjectField(dataStruct, dStart, struct)
    for i in range(1, n) :
      transferToDataStructure(_IPC.formatFormatArrayItemPtr(formatArray, i),
                              struct, structStart, buffer, format, False)
      structStart = structStart + 1
  elif (ftype == FixedArrayFMT) :
    formatArray = _IPC.formatFormatArray(format)
    nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
    size = _IPC.formatFormatArrayItemInt(formatArray, 2)
    if (isTopLevelStruct and isinstance(dataStruct, (tuple, list))) :
      arrayObject = dataStruct;
    else :
      arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
      arrayObject = validateArrayObject(arrayObject, size, dataStruct, dStart)
      primFmttrs.setObjectField(dataStruct, dStart, arrayObject)

    arrayTransferToDataStructure(arrayObject, buffer, 2, 
                                 _IPC.formatFormatArrayMax(formatArray)-1,
                                 size, isSimpleType(nextFormat), formatArray,
                                 nextFormat, dataStruct, dStart, False, oclass);
  elif (ftype == VarArrayFMT) :
    formatArray = _IPC.formatFormatArray(format)
    nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
    # The total size of the array is the stored first
    size = _IPC.formatGetInt(buffer)
    numDims = _IPC.formatFormatArrayMax(formatArray) - 2

    if (numDims > 1) : size = varArrayDimSize(2, formatArray, dataStruct)

    if (not feasibleToDecodeVarArray(size, formatArray, dStart)) :
      IPC.Raise("Python version of IPC cannot decode " + \
                "multi-dimensional variable length arrays unless " + \
                "the size variables appear BEFORE the array " + \
                "in the enclosing structure")
    elif (isTopLevelStruct) :
      arrayObject = dataStruct
    else :
      arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
      arrayObject = validateArrayObject(arrayObject, size, dataStruct, dStart)
      primFmttrs.setObjectField(dataStruct, dStart, arrayObject)

    arrayTransferToDataStructure(arrayObject, buffer, 2, numDims+1, size,
                                 isSimpleType(nextFormat), formatArray,
                                 nextFormat, dataStruct, dStart, True, oclass)
  elif (ftype == NamedFMT) :
    transferToDataStructure(_IPC.findNamedFormat(format), dataStruct, dStart,
                            buffer, parentFormat, isTopLevelStruct)
  elif (ftype == EnumFMT) :
    primFmttrs.Decode(primFmttrs.INT_FMT, dataStruct, dStart, buffer)
  else :
    IPC.Raise("Unhandled format: %s" % ftype)
Пример #5
0
def transferToBuffer (format, dataStruct, dStart, buffer, parentFormat, 
                      isTopLevelStruct) :
  ftype = _IPC.formatType(format)

  if (ftype == LengthFMT) :
    IPC.Raise("Python version of IPC can only use explicit formats")
  elif (ftype == PrimitiveFMT) :
    if (isTopLevelStruct) :
      object = IPCdata()
      object._f0 = dataStruct
      dataStruct = object
    primFmttrs.Encode(_IPC.formatPrimitiveProc(format), dataStruct, dStart,
 		      buffer)
  elif (ftype == PointerFMT) :
    object = primFmttrs.getObjectField(dataStruct, dStart)
    # 'Z' means data, 0 means NIL
    if (object is None) : theChar = '\0'
    else : theChar = 'Z'
    _IPC.formatPutChar(buffer, theChar);
    if (not object is None) :
      transferToBuffer(_IPC.formatChoosePtrFormat(format, parentFormat),
 		       dataStruct, dStart, buffer, 0, isTopLevelStruct)
  elif (ftype == StructFMT) :
    formatArray = _IPC.formatFormatArray(format)
    structStart = 0
    n = _IPC.formatFormatArrayMax(formatArray)
    if (isTopLevelStruct) : struct = dataStruct
    else : struct = primFmttrs.getObjectField(dataStruct, dStart)
    for i in range(1, n) :
      transferToBuffer(_IPC.formatFormatArrayItemPtr(formatArray, i),
 		       struct, structStart, buffer, format, False)
      structStart = structStart + 1
  elif (ftype == FixedArrayFMT) :
    formatArray = _IPC.formatFormatArray(format)
    nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
    if (isTopLevelStruct) : arrayObject = dataStruct
    else : arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
    if (not isinstance(arrayObject, (tuple, list))) :
      IPC.Raise(invalidArrayFormat(dataStruct, dStart))
    else :
      arrayTransferToBuffer(arrayObject, buffer, 2, 
 			    _IPC.formatFormatArrayMax(formatArray)-1,
 			    isSimpleType(nextFormat),
 			    formatArray, nextFormat, dataStruct, False);
  elif (ftype == VarArrayFMT) :
    formatArray = _IPC.formatFormatArray(format)
    nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
    if (isTopLevelStruct) : arrayObject = dataStruct
    else : arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
    # For the size of the array 
    _IPC.formatPutInt(buffer, varArrayNumElements(formatArray,
 							dataStruct))
    if (not isinstance(arrayObject, (tuple, list))) :
      IPC.Raise(invalidArrayFormat(dataStruct, dStart))
    else :
      arrayTransferToBuffer(arrayObject, buffer, 2, 
 			    _IPC.formatFormatArrayMax(formatArray)-1,
			    isSimpleType(nextFormat),
 			    formatArray, nextFormat, dataStruct, True)
  elif (ftype == NamedFMT) :
    transferToBuffer(_IPC.findNamedFormat(format), dataStruct, dStart, buffer,
 		     parentFormat, isTopLevelStruct)
  elif (ftype == EnumFMT) :
    primFmttrs.Encode(primFmttrs.INT_FMT, dataStruct, dStart, buffer);
  else :
    IPC.Raise("Unhandled format: %s" % ftype)
Пример #6
0
def bufferSize1 (format, dataStruct, dStart, parentFormat, isTopLevelStruct) :
  bufSize = 0
  ftype = _IPC.formatType(format)
  
  if (ftype == LengthFMT) :
    IPC.Raise("Python version of IPC can only use explicit formats")
  elif (ftype == PrimitiveFMT) :
    primType = _IPC.formatPrimitiveProc(format)
    bufSize = bufSize + primFmttrs.ELength(primType, dataStruct, dStart)
  elif (ftype == PointerFMT) :
    bufSize = bufSize + 1
    if (not primFmttrs.getObjectField(dataStruct, dStart) is None) :
      bufSize = bufSize + \
                bufferSize1(_IPC.formatChoosePtrFormat(format, parentFormat),
                            dataStruct, dStart, 0, isTopLevelStruct)
  elif (ftype == StructFMT) :
    formatArray = _IPC.formatFormatArray(format)
    structStart = 0
    n = _IPC.formatFormatArrayMax(formatArray);
    if (isTopLevelStruct) :
      struct = dataStruct
    else :
      struct = primFmttrs.getObjectField(dataStruct, dStart)
    for i in range(1, n) :
      bufSize = bufSize + \
                bufferSize1(_IPC.formatFormatArrayItemPtr(formatArray, i),
                            struct, structStart, format, False)
      structStart = structStart + 1
  elif (ftype == FixedArrayFMT) :
    formatArray = _IPC.formatFormatArray(format)
    nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
    if (isTopLevelStruct) :
      arrayObject = dataStruct
    else :
      arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
    if (not isinstance(arrayObject, (tuple, list))) :
      IPC.Raise(invalidArrayFormat(dataStruct, dStart))
    elif (isSimpleType(nextFormat)) :
      bufSize = bufSize + (bufferSize1(nextFormat, arrayObject, 0, 0, False) *
                           fixedArrayNumElements(formatArray))
    else :
      bufSize = bufSize + \
                arrayBufferSize(arrayObject, 2, 
                                _IPC.formatFormatArrayMax(formatArray)-1,
                                formatArray, nextFormat, None)
  elif (ftype == VarArrayFMT) :
    formatArray = _IPC.formatFormatArray(format)
    nextFormat = _IPC.formatFormatArrayItemPtr(formatArray, 1)
    if (isTopLevelStruct) :
      arrayObject = dataStruct
    else :
      arrayObject = primFmttrs.getObjectField(dataStruct, dStart)
    # For the size of the array
    bufSize = bufSize + primFmttrs.ELength(primFmttrs.INT_FMT, None, 0)
    if (not isinstance(arrayObject, (tuple, list))) :
      IPC.Raise(invalidArrayFormat(dataStruct, dStart))
    elif (isSimpleType(nextFormat)) :
      bufSize = bufSize + (bufferSize1(nextFormat, arrayObject, 0, 0, False) *
                           varArrayNumElements(formatArray, dataStruct))
    else :
      bufSize = bufSize + \
                arrayBufferSize(arrayObject, 2, 
                                _IPC.formatFormatArrayMax(formatArray)-1,
                                formatArray, nextFormat, dataStruct)
  elif (ftype == NamedFMT) :
    bufSize = bufSize + \
              bufferSize1(_IPC.findNamedFormat(format), dataStruct, dStart,
                          parentFormat, isTopLevelStruct);
  elif (ftype == EnumFMT) :
    bufSize = bufSize + primFmttrs.ELength(primFmttrs.INT_FMT, None, 0);
  else :
    IPC.Raise("Unhandled format: %s" % ftype)

  return bufSize;