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/10 20:12:13 UTC
svn commit: r635660 - in /incubator/qpid/trunk/qpid/python: qpid/ tests/
tests_0-10/
Author: rhs
Date: Mon Mar 10 12:12:09 2008
New Revision: 635660
URL: http://svn.apache.org/viewvc?rev=635660&view=rev
Log:
renamed datatypes.Struct.type -> datatypes.Struct._type; this avoids naming conflicts with metadata-driven fields; moved argument validation -> datatypes.Struct and improved error checking; improved datatypes.Struct.__repr__
Modified:
incubator/qpid/trunk/qpid/python/qpid/assembler.py
incubator/qpid/trunk/qpid/python/qpid/codec010.py
incubator/qpid/trunk/qpid/python/qpid/connection010.py
incubator/qpid/trunk/qpid/python/qpid/datatypes.py
incubator/qpid/trunk/qpid/python/qpid/delegates.py
incubator/qpid/trunk/qpid/python/qpid/session.py
incubator/qpid/trunk/qpid/python/qpid/spec010.py
incubator/qpid/trunk/qpid/python/tests/connection010.py
incubator/qpid/trunk/qpid/python/tests/spec010.py
incubator/qpid/trunk/qpid/python/tests_0-10/example.py
incubator/qpid/trunk/qpid/python/tests_0-10/queue.py
Modified: incubator/qpid/trunk/qpid/python/qpid/assembler.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/assembler.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/assembler.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/assembler.py Mon Mar 10 12:12:09 2008
@@ -46,9 +46,9 @@
def decode_command(self, spec):
sc = StringCodec(spec, self.payload)
- cmd = sc.read_command()
+ hdr, cmd = sc.read_command()
cmd.id = self.id
- return cmd
+ return hdr, cmd
def decode_header(self, spec):
sc = StringCodec(spec, self.payload)
Modified: incubator/qpid/trunk/qpid/python/qpid/codec010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/codec010.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/codec010.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/codec010.py Mon Mar 10 12:12:09 2008
@@ -18,7 +18,7 @@
#
from packer import Packer
-from datatypes import RangedSet
+from datatypes import RangedSet, Struct
class CodecException(Exception): pass
@@ -184,27 +184,32 @@
def read_struct32(self):
size = self.read_uint32()
code = self.read_uint16()
- struct = self.spec.structs[code]
- return struct.decode_fields(self)
+ type = self.spec.structs[code]
+ fields = type.decode_fields(self)
+ return Struct(type, **fields)
def write_struct32(self, value):
sc = StringCodec(self.spec)
- sc.write_uint16(value.type.code)
- value.type.encode_fields(sc, value)
+ 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):
+ def write_control(self, ctrl):
+ type = ctrl._type
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)
+ type = self.spec.commands[self.read_uint16()]
+ hdr = self.spec["session.header"].decode(self)
+ cmd = type.decode(self)
+ return hdr, cmd
+ def write_command(self, hdr, cmd):
+ self.write_uint16(cmd._type.code)
+ hdr._type.encode(self, hdr)
+ cmd._type.encode(self, cmd)
def read_size(self, width):
if width > 0:
Modified: incubator/qpid/trunk/qpid/python/qpid/connection010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/connection010.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/connection010.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/connection010.py Mon Mar 10 12:12:09 2008
@@ -166,7 +166,7 @@
def invoke(self, type, args, kwargs):
cntrl = type.new(args, kwargs)
sc = StringCodec(self.connection.spec)
- sc.write_control(type, cntrl)
+ sc.write_control(cntrl)
self.connection.write_segment(Segment(True, True, type.segment_type,
type.track, self.id, sc.encoded))
Modified: incubator/qpid/trunk/qpid/python/qpid/datatypes.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/datatypes.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/datatypes.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/datatypes.py Mon Mar 10 12:12:09 2008
@@ -21,15 +21,48 @@
class Struct:
- def __init__(self, fields):
- self.__dict__ = fields
+ def __init__(self, _type, *args, **kwargs):
+ if len(args) > len(_type.fields):
+ raise TypeError("%s() takes at most %s arguments (%s given)" %
+ (_type.name, len(_type.fields), len(args)))
- def __repr__(self):
- return "Struct(%s)" % ", ".join(["%s=%r" % (k, v)
- for k, v in self.__dict__.items()])
+ self._type = _type
+
+ idx = 0
+ for field in _type.fields:
+ if idx < len(args):
+ arg = args[idx]
+ if kwargs.has_key(field.name):
+ raise TypeError("%s() got multiple values for keyword argument '%s'" %
+ (_type.name, field.name))
+ elif kwargs.has_key(field.name):
+ arg = kwargs.pop(field.name)
+ else:
+ arg = field.default()
+ setattr(self, field.name, arg)
+ idx += 1
+
+ if kwargs:
+ unexpected = kwargs.keys()[0]
+ raise TypeError("%s() got an unexpected keywoard argument '%s'" %
+ (_type.name, unexpected))
- def fields(self):
- return self.__dict__
+ def __getitem__(self, name):
+ return getattr(self, name)
+
+ def __setitem__(self, name, value):
+ if not hasattr(self, name):
+ raise AttributeError("'%s' object has no attribute '%s'" %
+ (self._type.name, name))
+ setattr(self, name, value)
+
+ def __repr__(self):
+ fields = []
+ for f in self._type.fields:
+ v = self[f.name]
+ if f.type.is_present(v):
+ fields.append("%s=%r" % (f.name, v))
+ return "%s(%s)" % (self._type.name, ", ".join(fields))
class Message:
Modified: incubator/qpid/trunk/qpid/python/qpid/delegates.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/delegates.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/delegates.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/delegates.py Mon Mar 10 12:12:09 2008
@@ -38,7 +38,7 @@
if seg.track == self.control:
cntrl = seg.decode(self.spec)
- attr = cntrl.type.qname.replace(".", "_")
+ attr = cntrl._type.qname.replace(".", "_")
getattr(self, attr)(ch, cntrl)
elif ssn is None:
ch.session_detached()
Modified: incubator/qpid/trunk/qpid/python/qpid/session.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/session.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/session.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/session.py Mon Mar 10 12:12:09 2008
@@ -129,7 +129,8 @@
cmd = type.new(args, kwargs)
sc = StringCodec(self.spec)
- sc.write_command(type, cmd)
+ hdr = Struct(self.spec["session.header"])
+ sc.write_command(hdr, cmd)
seg = Segment(True, (message == None or
(message.headers == None and message.body == None)),
@@ -174,10 +175,10 @@
def dispatch(self, assembly):
segments = assembly[:]
- cmd = assembly.pop(0).decode(self.spec)
+ hdr, cmd = assembly.pop(0).decode(self.spec)
args = []
- for st in cmd.type.segments:
+ for st in cmd._type.segments:
if assembly:
seg = assembly[0]
if seg.type == st.segment_type:
@@ -188,10 +189,10 @@
assert len(assembly) == 0
- attr = cmd.type.qname.replace(".", "_")
+ attr = cmd._type.qname.replace(".", "_")
result = getattr(self.delegate, attr)(cmd, *args)
- if cmd.type.result:
+ if cmd._type.result:
self.execution_result(cmd.id, result)
if result is not INCOMPLETE:
Modified: incubator/qpid/trunk/qpid/python/qpid/spec010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/spec010.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/spec010.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/spec010.py Mon Mar 10 12:12:09 2008
@@ -188,34 +188,18 @@
self.pack = pack
def new(self, args, kwargs):
- if len(args) > len(self.fields):
- raise TypeError("%s takes at most %s arguments (%s given)" %
- (self.name, len(self.fields), len(self.args)))
-
- result = {"type": self}
-
- for a, f, in zip(args, self.fields):
- result[f.name] = a
-
- for k, v in kwargs.items():
- f = self.named.get(k)
- if f == None:
- raise TypeError("%s got an unexpected keyword argument '%s'" %
- (self.name, k))
- result[f.name] = v
-
- return datatypes.Struct(result)
+ return datatypes.Struct(self, *args, **kwargs)
def decode(self, codec):
codec.read_size(self.size)
- return self.decode_fields(codec)
+ return datatypes.Struct(self, **self.decode_fields(codec))
def decode_fields(self, codec):
flags = 0
for i in range(self.pack):
flags |= (codec.read_uint8() << 8*i)
- result = {"type": self}
+ result = {}
for i in range(len(self.fields)):
f = self.fields[i]
@@ -223,7 +207,7 @@
result[f.name] = f.type.decode(codec)
else:
result[f.name] = None
- return datatypes.Struct(result)
+ return result
def encode(self, codec, value):
sc = StringCodec(self.spec)
@@ -231,12 +215,11 @@
codec.write_size(self.size, len(sc.encoded))
codec.write(sc.encoded)
- def encode_fields(self, codec, value):
- values = value.__dict__
+ def encode_fields(self, codec, values):
flags = 0
for i in range(len(self.fields)):
f = self.fields[i]
- if f.type.is_present(values.get(f.name)):
+ if f.type.is_present(values[f.name]):
flags |= (0x1 << i)
for i in range(self.pack):
codec.write_uint8((flags >> 8*i) & 0xFF)
@@ -253,6 +236,9 @@
self.type = type
self.exceptions = []
+ def default(self):
+ return None
+
def register(self, node):
Named.register(self, node)
node.fields.append(self)
@@ -328,22 +314,9 @@
def register(self, node):
Instruction.register(self, node)
node.commands.append(self)
- self.header = self.spec["session.header"]
self.spec.commands[self.code] = self
self.segment_type = self.spec["segment_type.command"].value
self.track = self.spec["track.command"].value
-
- def decode(self, codec):
- hdr = self.header.decode(codec)
- args = Instruction.decode(self, codec)
- result = {}
- result.update(hdr.fields())
- result.update(args.fields())
- return datatypes.Struct(result)
-
- def encode(self, codec, cmd):
- self.header.encode(codec, cmd)
- Instruction.encode(self, codec, cmd)
class Header(Segment, Node):
Modified: incubator/qpid/trunk/qpid/python/tests/connection010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/tests/connection010.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/tests/connection010.py (original)
+++ incubator/qpid/trunk/qpid/python/tests/connection010.py Mon Mar 10 12:12:09 2008
@@ -49,7 +49,7 @@
self.queue = queue
def queue_query(self, qq):
- return qq.type.result.type.new((qq.queue,), {})
+ return qq._type.result.type.new((qq.queue,), {})
def message_transfer(self, cmd, header, body):
self.queue.put((cmd, header, body))
Modified: incubator/qpid/trunk/qpid/python/tests/spec010.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/tests/spec010.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/tests/spec010.py (original)
+++ incubator/qpid/trunk/qpid/python/tests/spec010.py Mon Mar 10 12:12:09 2008
@@ -31,11 +31,11 @@
def testSessionHeader(self):
hdr = self.spec["session.header"]
sc = StringCodec(self.spec)
- hdr.encode(sc, Struct({"sync": True}))
+ hdr.encode(sc, Struct(hdr, sync=True))
assert sc.encoded == "\x01\x01"
sc = StringCodec(self.spec)
- hdr.encode(sc, Struct({"sync": False}))
+ hdr.encode(sc, Struct(hdr, sync=False))
assert sc.encoded == "\x01\x00"
def encdec(self, type, value):
@@ -45,16 +45,20 @@
return decoded
def testMessageProperties(self):
- props = Struct({"content_length": 0xDEADBEEF,
- "reply_to":
- Struct({"exchange": "the exchange name", "routing_key": "the routing key"})})
- dec = self.encdec(self.spec["message.message_properties"], props)
+ mp = self.spec["message.message_properties"]
+ rt = self.spec["message.reply_to"]
+
+ props = Struct(mp, content_length=0xDEADBEEF,
+ reply_to=Struct(rt, exchange="the exchange name",
+ routing_key="the routing key"))
+ dec = self.encdec(mp, props)
assert props.content_length == dec.content_length
assert props.reply_to.exchange == dec.reply_to.exchange
assert props.reply_to.routing_key == dec.reply_to.routing_key
def testMessageSubscribe(self):
- cmd = Struct({"exclusive": True, "destination": "this is a test"})
+ ms = self.spec["message.subscribe"]
+ cmd = Struct(ms, exclusive=True, destination="this is a test")
dec = self.encdec(self.spec["message.subscribe"], cmd)
assert cmd.exclusive == dec.exclusive
assert cmd.destination == dec.destination
Modified: incubator/qpid/trunk/qpid/python/tests_0-10/example.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/tests_0-10/example.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/tests_0-10/example.py (original)
+++ incubator/qpid/trunk/qpid/python/tests_0-10/example.py Mon Mar 10 12:12:09 2008
@@ -60,7 +60,7 @@
session.exchange_declare("test", "direct")
# Here we use keyword arguments.
- session.queue_declare(session, queue="test-queue", exclusive=True, auto_delete=True)
+ session.queue_declare(queue="test-queue", exclusive=True, auto_delete=True)
session.exchange_bind(queue="test-queue", exchange="test", binding_key="key")
# Call Session.subscribe to register as a consumer.
Modified: incubator/qpid/trunk/qpid/python/tests_0-10/queue.py
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/tests_0-10/queue.py?rev=635660&r1=635659&r2=635660&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/tests_0-10/queue.py (original)
+++ incubator/qpid/trunk/qpid/python/tests_0-10/queue.py Mon Mar 10 12:12:09 2008
@@ -269,7 +269,7 @@
session = self.conn.session("replacement", 2)
#empty queue:
- session.message_subscribe(session, destination="consumer_tag", queue="delete-me-2")
+ session.message_subscribe(destination="consumer_tag", queue="delete-me-2")
session.message_flow(destination="consumer_tag", unit=0, value=0xFFFFFFFF)
session.message_flow(destination="consumer_tag", unit=1, value=0xFFFFFFFF)
queue = session.incoming("consumer_tag")