Ejemplo n.º 1
0
 def parseValue(self, *args):
   if len(args) is 0:
     valtype = self.buffer.readUInt8()
   else:
     valtype = args[0]
   if valtype < OBJECT:
     return self.buffer.readValue(valtype)
   elif valtype is VARARRAY:
     valtype = self.buffer.readUInt8()
     length = self.buffer.readValue(valtype)
     ret = []
     i = 0
     while i < length:
       ret.append(self.parseValue())
       i += 1
     return ret
   elif valtype is OBJECT:
     index = self.objectLayoutIndex[self.buffer.readValue(self.OLItype)]
     ret = {}
     i = 0
     while i < len(index):
       ret[self.stringIndex[index[i]]] = self.parseValue()
       i += 1
   elif valtype is STRING:
     return self.stringIndex[self.buffer.readValue(self.stringIndexType)]
   elif valtype is UNDEFINED:
     return None
   elif valtype is NULL:
     return Null();
   elif valtype is TRUE:
     return True
   elif valtype is FALSE:
     return False
   elif valtype is DATE:
     return date.fromtimestamp(self.buffer.readUInt32())
   elif valtype is REGEXP:
     return re.compile(self.readString())
   elif valtype is NAN:
     return float("nan");
   elif valtype is POSITIVE_INFINITY:
     return float("inf");
   elif valtype is MINUS_INFINITY:
     return float("-inf");
   elif valtype is BUFFER:
     ret = StringBuffer();
     length = self.parseValue(self.buffer.readUInt8());
     i = 0;
     while i < length:
       ret.writeUInt8(self.buffer.readUInt8(), i);
       i += 1
     return ret
   else:
     raise Exception('Invalid LEON.')
   return ret
Ejemplo n.º 2
0
Archivo: io.py Proyecto: hmcq6/pyleon
 def writeString(self, string):
   buf = StringBuffer()
   for c in string:
     buf.writeUInt8(ord(c), -1)
   buf.writeUInt8(0, -1)
   self.append(buf)
Ejemplo n.º 3
0
Archivo: io.py Proyecto: hmcq6/pyleon
 def writeValue(self, *args):
   if len(args) < 3:
     implicit = False
   else:
     implicit = args[2]
   valtype = args[1]
   val = args[0]
   typeByte = StringBuffer()
   typeByte.writeUInt8(valtype, 0)
   if not implicit:
     self.append(typeByte)
   if valtype is UNDEFINED or valtype is TRUE or valtype is FALSE or valtype is NULL or valtype is NAN:
     return 1
   if valtype is STRING:
     if len(self.stringIndex) is 0:
       self.writeString(val)
       return 2 + len(val)
     self.writeValue(self.stringIndex.index(val), self.stringIndexType, True)
     return 2
   if valtype is SIGNED | CHAR:
     buf = StringBuffer()
     buf.writeInt8(val, 0)
     self.append(buf)
     return 2
   if valtype is CHAR:
     buf = StringBuffer()
     buf.writeUInt8(val, 0)
     self.append(buf)
     return 2
   if valtype is SIGNED | SHORT:
     buf = StringBuffer()
     buf.writeInt16LE(val, 0)
     self.append(buf)
     return 3
   if valtype is SHORT:
     buf = StringBuffer()
     buf.writeInt16LE(val, 0)
     self.append(buf)
     return 5
   if valtype is SIGNED | INT:
     buf = StringBuffer()
     buf.writeInt32LE(val, 0)
     self.append(buf)
     return 5
   if valtype is INT:
     buf = StringBuffer()
     buf.writeUInt32LE(val, 0)
     self.append(buf)
     return 5
   if valtype is FLOAT:
     buf = StringBuffer()
     buf.writeFloatLE(val, 0)
     self.append(buf)
     return 5
   if valtype is DOUBLE:
     buf = StringBuffer()
     buf.writeDoubleLE(val, 0)
     self.append(buf)
     return 9
   if valtype is VARARRAY:
     self.writeValue(len(val), typeCheck(len(val)))
     i = 0
     while i < len(val):
       self.writeValue(val[i], typeCheck(val[i]))
       i += 1
   if valtype is OBJECT:
     index = matchLayout(val, self.stringIndex, self.OLI)
     if not implicit:
       self.writeValue(index, self.OLItype, True)
     i = 0
     while i < len(self.OLI[index]):
       tmp = val[self.stringIndex[self.OLI[index][i]]]
       self.writeValue(tmp, typeCheck(tmp))
       i += 1
   if valtype is DATE:
     self.writeValue(val.timestamp(), INT, true)
   if valtype is REGEXP:
     self.writeString(val.pattern)