You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rh...@apache.org on 2008/03/07 21:22:19 UTC
svn commit: r634803 - in /incubator/qpid/trunk/qpid/python: qpid/codec010.py
qpid/delegates.py qpid/spec010.py tests/__init__.py tests/codec010.py
Author: rhs
Date: Fri Mar 7 12:22:18 2008
New Revision: 634803
URL: http://svn.apache.org/viewvc?rev=634803&view=rev
Log:
added support for maps
Added:
incubator/qpid/trunk/qpid/python/tests/codec010.py (with props)
Modified:
incubator/qpid/trunk/qpid/python/qpid/codec010.py
incubator/qpid/trunk/qpid/python/qpid/delegates.py
incubator/qpid/trunk/qpid/python/qpid/spec010.py
incubator/qpid/trunk/qpid/python/tests/__init__.py
Modified: incubator/qpid/trunk/qpid/python/qpid/codec010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/codec010.py?rev=634803&r1=634802&r2=634803&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/codec010.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/codec010.py Fri Mar 7 12:22:18 2008
@@ -20,11 +20,18 @@
from packer import Packer
from datatypes import RangedSet
+class CodecException(Exception): pass
+
class Codec(Packer):
def __init__(self, spec):
self.spec = spec
+ def write_void(self, v):
+ assert v == None
+ def read_void(self):
+ return None
+
def write_bit(self, b):
if not b: raise ValueError(b)
def read_bit(self):
@@ -148,10 +155,26 @@
self.write(b)
def write_map(self, m):
- self.write_uint32(0) #hack
+ sc = StringCodec(self.spec)
+ for k, v in m.items():
+ type = self.spec.encoding(v.__class__)
+ if type == None:
+ raise CodecException("no encoding for %s" % v.__class__)
+ sc.write_str8(k)
+ sc.write_uint8(type.code)
+ type.encode(sc, v)
+ # XXX: need to put in count when CPP supports it
+ self.write_vbin32(sc.encoded)
def read_map(self):
- size = self.read_uint32() #hack
- self.read(size) #hack
+ sc = StringCodec(self.spec, self.read_vbin32())
+ result = {}
+ while sc.encoded:
+ k = sc.read_str8()
+ code = sc.read_uint8()
+ type = self.spec.types[code]
+ v = type.decode(sc)
+ result[k] = v
+ return result
def write_array(self, a):
pass
Modified: incubator/qpid/trunk/qpid/python/qpid/delegates.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/delegates.py?rev=634803&r1=634802&r2=634803&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/delegates.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/delegates.py Fri Mar 7 12:22:18 2008
@@ -17,8 +17,7 @@
# under the License.
#
-import connection010
-import session
+import os, connection010, session
from util import notify
from datatypes import RangedSet
@@ -117,12 +116,16 @@
class Client(Delegate):
+ PROPERTIES = {"product": "qpid python client",
+ "version": "development",
+ "platform": os.name}
+
def start(self):
self.connection.write_header(self.spec.major, self.spec.minor)
self.connection.read_header()
def connection_start(self, ch, start):
- ch.connection_start_ok()
+ ch.connection_start_ok(client_properties=Client.PROPERTIES)
def connection_tune(self, ch, tune):
ch.connection_tune_ok()
Modified: incubator/qpid/trunk/qpid/python/qpid/spec010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/spec010.py?rev=634803&r1=634802&r2=634803&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/spec010.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/spec010.py Fri Mar 7 12:22:18 2008
@@ -122,7 +122,6 @@
def register(self, node):
Named.register(self, node)
- node.types.append(self)
Node.register(self)
class Primitive(Coded, Type):
@@ -133,6 +132,11 @@
self.fixed = fixed
self.variable = variable
+ def register(self, node):
+ Type.register(self, node)
+ if self.code is not None:
+ self.spec.types[self.code] = self
+
def is_present(self, value):
if self.fixed == 0:
return value
@@ -265,7 +269,8 @@
def register(self, node):
Composite.register(self, node)
- self.spec.structs[self.code] = self
+ if self.code is not None:
+ self.spec.structs[self.code] = self
def __str__(self):
fields = ",\n ".join(["%s: %s" % (f.name, f.type.qname)
@@ -384,7 +389,6 @@
Named.__init__(self, name)
Coded.__init__(self, code)
Node.__init__(self, children)
- self.types = []
self.controls = []
self.commands = []
@@ -441,6 +445,16 @@
class Spec(Node):
+ ENCODINGS = {
+ basestring: "vbin16",
+ int: "int32",
+ long: "int64",
+ None.__class__: "void",
+ list: "list",
+ tuple: "list",
+ dict: "map"
+ }
+
def __init__(self, major, minor, port, children):
Node.__init__(self, children)
self.major = major
@@ -448,7 +462,7 @@
self.port = port
self.constants = []
self.classes = []
- self.types = []
+ self.types = {}
self.qname = None
self.spec = self
self.klass = None
@@ -456,6 +470,14 @@
self.controls = {}
self.commands = {}
self.structs = {}
+
+ def encoding(self, klass):
+ if Spec.ENCODINGS.has_key(klass):
+ return self.named[Spec.ENCODINGS[klass]]
+ for base in klass.__bases__:
+ result = self.encoding(base)
+ if result != None:
+ return result
class Implement:
Modified: incubator/qpid/trunk/qpid/python/tests/__init__.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/tests/__init__.py?rev=634803&r1=634802&r2=634803&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/tests/__init__.py (original)
+++ incubator/qpid/trunk/qpid/python/tests/__init__.py Fri Mar 7 12:22:18 2008
@@ -27,3 +27,4 @@
from datatypes import *
from connection010 import *
from spec010 import *
+from codec010 import *
Added: incubator/qpid/trunk/qpid/python/tests/codec010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/tests/codec010.py?rev=634803&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/python/tests/codec010.py (added)
+++ incubator/qpid/trunk/qpid/python/tests/codec010.py Fri Mar 7 12:22:18 2008
@@ -0,0 +1,57 @@
+#
+# 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 unittest import TestCase
+from qpid.spec010 import load
+from qpid.codec010 import StringCodec
+from qpid.testlib import testrunner
+
+class CodecTest(TestCase):
+
+ def setUp(self):
+ self.spec = load(testrunner.get_spec_file("amqp.0-10.xml"))
+
+ def check(self, type, value):
+ t = self.spec[type]
+ sc = StringCodec(self.spec)
+ t.encode(sc, value)
+ decoded = t.decode(sc)
+ assert decoded == value
+
+ def testMapString(self):
+ self.check("map", {"string": "this is a test"})
+
+ def testMapInt(self):
+ self.check("map", {"int": 3})
+
+ def testMapLong(self):
+ self.check("map", {"long": 2**32})
+
+ def testMapNone(self):
+ self.check("map", {"none": None})
+
+ def testMapNested(self):
+ self.check("map", {"map": {"string": "nested test"}})
+
+ def testMapAll(self):
+ self.check("map", {"string": "this is a test",
+ "int": 3,
+ "long": 2**32,
+ "none": None,
+ "map": {"string": "nested map"}})
Propchange: incubator/qpid/trunk/qpid/python/tests/codec010.py
------------------------------------------------------------------------------
svn:eol-style = native