summaryrefslogtreecommitdiff
path: root/python/qpid/codec010.py
diff options
context:
space:
mode:
authorRafael H. Schloming <rhs@apache.org>2008-03-04 20:03:09 +0000
committerRafael H. Schloming <rhs@apache.org>2008-03-04 20:03:09 +0000
commit75f598b22ea4573cff2d47fdd689b85cee5dd88d (patch)
tree964aa4463e2140c5040dd36137a49ab9c261f19a /python/qpid/codec010.py
parent24435b9c62976e0a4c0857f86057d3c93389b79f (diff)
downloadqpid-python-75f598b22ea4573cff2d47fdd689b85cee5dd88d.tar.gz
import of in-process 0-10 final python client
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@633610 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/qpid/codec010.py')
-rw-r--r--python/qpid/codec010.py186
1 files changed, 186 insertions, 0 deletions
diff --git a/python/qpid/codec010.py b/python/qpid/codec010.py
new file mode 100644
index 0000000000..5894981fc6
--- /dev/null
+++ b/python/qpid/codec010.py
@@ -0,0 +1,186 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+from packer import Packer
+
+class Codec(Packer):
+
+ def __init__(self, spec):
+ self.spec = spec
+
+ def write_bit(self, b):
+ if not b: raise ValueError(b)
+ def read_bit(self):
+ return True
+
+ def read_uint8(self):
+ return self.unpack("!B")
+ def write_uint8(self, n):
+ return self.pack("!B", n)
+
+ def read_int8(self):
+ return self.unpack("!b")
+ def write_int8(self, n):
+ self.pack("!b", n)
+
+ def read_char(self):
+ return self.unpack("!c")
+ def write_char(self, c):
+ self.pack("!c", c)
+
+ def read_boolean(self):
+ return self.read_uint8() != 0
+ def write_boolean(self, b):
+ if b: n = 1
+ else: n = 0
+ self.write_uint8(n)
+
+
+ def read_uint16(self):
+ return self.unpack("!H")
+ def write_uint16(self, n):
+ self.pack("!H", n)
+
+ def read_int16(self):
+ return self.unpack("!h")
+ def write_int16(self, n):
+ return self.unpack("!h", n)
+
+
+ def read_uint32(self):
+ return self.unpack("!L")
+ def write_uint32(self, n):
+ self.pack("!L", n)
+
+ def read_int32(self):
+ return self.unpack("!l")
+ def write_int32(self, n):
+ self.pack("!l", n)
+
+ def read_float(self):
+ return self.unpack("!f")
+ def write_float(self, f):
+ self.pack("!f", f)
+
+ def read_sequence_no(self):
+ return self.read_uint32()
+ def write_sequence_no(self, n):
+ self.write_uint32(n)
+
+
+ def read_uint64(self):
+ return self.unpack("!Q")
+ def write_uint64(self, n):
+ self.pack("!Q", n)
+
+ def read_int64(self):
+ return self.unpack("!q")
+ def write_int64(self, n):
+ self.pack("!q", n)
+
+ def read_double(self):
+ return self.unpack("!d")
+ def write_double(self, d):
+ self.pack("!d", d)
+
+
+ def read_vbin8(self):
+ return self.read(self.read_uint8())
+ def write_vbin8(self, b):
+ self.write_uint8(len(b))
+ self.write(b)
+
+ def read_str8(self):
+ return self.read_vbin8().decode("utf8")
+ def write_str8(self, s):
+ self.write_vbin8(s.encode("utf8"))
+
+
+ def read_vbin16(self):
+ return self.read(self.read_uint16())
+ def write_vbin16(self, b):
+ self.write_uint16(len(b))
+ self.write(b)
+
+ def read_vbin32(self):
+ return self.read(self.read_uint32())
+ def write_vbin32(self, b):
+ self.write_uint32(len(b))
+ self.write(b)
+
+ def write_map(self, m):
+ pass
+ def read_map(self):
+ pass
+
+ def write_array(self, a):
+ pass
+ def read_array(self):
+ pass
+
+ def read_struct32(self):
+ size = self.read_uint32()
+ code = self.read_uint16()
+ struct = self.spec.structs[code]
+ return struct.decode_fields(self)
+ def write_struct32(self, value):
+ sc = StringCodec(self.spec)
+ sc.write_uint16(value.type.code)
+ value.type.encode_fields(sc, value)
+ self.write_vbin32(sc.encoded)
+
+ def read_control(self):
+ cntrl = self.spec.controls[self.read_uint16()]
+ return cntrl.decode(self)
+ def write_control(self, type, ctrl):
+ self.write_uint16(type.code)
+ type.encode(self, ctrl)
+
+ def read_command(self):
+ cmd = self.spec.commands[self.read_uint16()]
+ return cmd.decode(self)
+ def write_command(self, type, cmd):
+ self.write_uint16(type.code)
+ type.encode(self, cmd)
+
+ def read_size(self, width):
+ if width > 0:
+ attr = "read_uint%d" % (width*8)
+ return getattr(self, attr)()
+
+ def write_size(self, width, n):
+ if width > 0:
+ attr = "write_uint%d" % (width*8)
+ getattr(self, attr)(n)
+
+
+
+class StringCodec(Codec):
+
+ def __init__(self, spec, encoded = ""):
+ Codec.__init__(self, spec)
+ self.encoded = encoded
+
+ def write(self, s):
+ self.encoded += s
+
+ def read(self, n):
+ result = self.encoded[:n]
+ self.encoded = self.encoded[n:]
+ return result