def getFloatField(object, n): theFloat = getField(object, n, float) if (not theFloat is None): return theFloat else: Raise("%s (field %s of %s) is not a float" % \ (theFloat, getNthFieldName(object, n), object))
def getDoubleField(object, n): theDouble = getField(object, n, float) if (not theDouble is None): return theDouble else: Raise("%s (field %s of %s) is not a double" % \ (theDouble, getNthFieldName(object, n), object))
def getShortField(object, n): theShort = getField(object, n, int) if (not theShort is None and abs(theShort) <= 0XFFFF): return theShort else: Raise("%s (field %s of %s) is not a short" % \ (theShort, getNthFieldName(object, n), object))
def getStringField(object, n): theString = getField(object, n, str) if (not theString is None): return theString else: Raise("%s (field %s of %s) is not a string" % \ (theString, getNthFieldName(object, n), object))
def getIntField(object, n): theInt = getField(object, n, int) if (not theInt is None and abs(theInt) <= 0XFFFFFF): return theInt else: Raise("%s (field %s of %s) is not an int" % \ (theInt, getNthFieldName(object, n), object))
def getCharField(object, n): theChar = getField(object, n, str) if (not theChar is None and len(theChar) == 1): return theChar else: Raise("%s (field %s of %s) is not a single character" % \ (theChar, getNthFieldName(object, n), object))
def getByteField(object, n): theByte = getField(object, n, int) if (not theByte is None and abs(theByte) <= 0XFF): return theByte else: Raise("%s (field %s of %s) is not a byte" % \ (theByte, getNthFieldName(object, n), object))
def getLongField(object, n): theLong = getField(object, n, int) if (not theLong is None): return theLong else: Raise("%s (field %s of %s) is not a long" % \ (theLong, getNthFieldName(object, n), object))
def getBooleanField(object, n): theBoolean = getField(object, n, bool) if (isinstance(theBoolean, bool)): if (theBoolean == True): return 1 else: return 0 else: Raise("%s (field %s of %s) is not Boolean" % \ (theBoolean, getNthFieldName(object, n), object))
def Encode(self, dataStruct, dstart, buffer): theLong = getLongField(dataStruct, dstart) if (-0X7FFFFFFF <= theLong <= 0X7FFFFFFF): _IPC.formatPutInt(buffer, theLong) # 8 byte longs not implemented, yet #elif (0 <= theLong < 0XFFFFFFFFFFFFFFFF) : # _IPC.formatPutLong(buffer, theLong) else: Raise("Will lose precision in transferring long: %d" % theLong)
def EncodeElement(self, array, index, buffer): theLong = array[index] if (-0X7FFFFFFF <= theLong <= 0X7FFFFFFF): _IPC.formatPutInt(buffer, theLong) # 8 byte longs not implemented, yet #elif (0 <= theLong < 0XFFFFFFFFFFFFFFFF) : # _IPC.formatPutLong(buffer, theLong) else: Raise("Will lose precision in transferring long: %d" % theLong)
def getField(object, n, theClass=None): fieldName = getNthFieldName(object, n) try: field = object.__dict__[fieldName] if (theClass is None or isinstance(field, theClass)): return field else: Raise("getField: %r not of class %s" % (field, theClass.__name__)) except KeyError: return None
def findClass(className, parent=None): split = className.split('.') slen = len(split) try: if (slen > 1): module = sys.modules[split[slen - 2]] elif (not parent is None): module = sys.modules[parent.__module__] else: module = sys.modules['__main__'] return module.__dict__[split[slen - 1]] except KeyError: Raise('%s not a valid class name' % className)
def setLongField(object, n, theLong): if (isinstance(theLong, int)): return setField(object, n, theLong) else: Raise("%s is not a long" % theLong)
def setBooleanField(object, n, theBoolean): if (theBoolean in (0, 1)): if (theBoolean == 1): return setField(object, n, True) else: return setField(object, n, False) else: Raise("%s is not Boolean" % theBoolean)
def pickTrans(type): if (0 <= type < MAXFORMATTERS): fn = TransFormatArray[type] if (not fn is None): return fn Raise("pickTrans: Unhandled format %s" % type)
def setIntField(object, n, theInt): if (isinstance(theInt, int) and abs(theInt) <= 0XFFFFFFFF): return setField(object, n, theInt) else: Raise("%s is not a int" % theInt)
def setByteField(object, n, theByte): if (isinstance(theByte, int) and abs(theByte) <= 0XFF): return setField(object, n, theByte) else: Raise("%s is not a byte" % theByte)
def setStringField(object, n, theString): if (isinstance(theString, str)): return setField(object, n, theString) else: Raise("%s is not a string" % theString)
def setShortField(object, n, theShort): if (isinstance(theShort, int) and abs(theShort) <= 0XFFFF): return setField(object, n, theShort) else: Raise("%s is not a short" % theShort)
def setDoubleField(object, n, theDouble): if (isinstance(theDouble, float)): return setField(object, n, theDouble) else: Raise("%s is not a double" % theDouble)
def setCharField(object, n, theChar): if (isinstance(theChar, str) and len(theChar) == 1): return setField(object, n, theChar) else: Raise("%s is not a single character" % theChar)
def setFloatField(object, n, theFloat): if (isinstance(theFloat, float)): return setField(object, n, theFloat) else: Raise("%s is not a float" % theFloat)