Author: rhs
Date: Wed Nov 7 12:46:36 2007
New Revision: 592888
URL: http://svn.apache.org/viewvc?rev=592888&view=rev
Log:
added factory for structs, and made default spec loading based on AMQP_SPEC
environment variable
Modified:
incubator/qpid/trunk/qpid/python/hello-world
incubator/qpid/trunk/qpid/python/qpid/__init__.py
incubator/qpid/trunk/qpid/python/qpid/client.py
incubator/qpid/trunk/qpid/python/qpid/spec.py
Modified: incubator/qpid/trunk/qpid/python/hello-world
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/hello-world?rev=592888&r1=592887&r2=592888&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/hello-world (original)
+++ incubator/qpid/trunk/qpid/python/hello-world Wed Nov 7 12:46:36 2007
@@ -3,8 +3,7 @@
from qpid.client import Client
from qpid.content import Content
-spec = qpid.spec.load("../specs/amqp.0-10-preview.xml")
-client = Client("127.0.0.1", 5672, spec)
+client = Client("127.0.0.1", 5672)
client.start({"LOGIN": "guest", "PASSWORD": "guest"})
ch = client.channel(1)
ch.session_open()
@@ -17,9 +16,7 @@
msg = Content("hello world")
msg["content_type"] = "text/plain"
msg["routing_key"] = "test"
-msg["reply_to"] = spec.struct("reply_to")
-msg["reply_to"].exchange_name = "asdf"
-msg["reply_to"].routing_key = "fdsa"
+msg["reply_to"] = client.structs.reply_to("asdf", "fdsa")
msg["application_headers"] = {"x": 1, "y": 2, "z": "zee"}
ch.message_transfer(destination="amq.direct", content=msg)
queue = client.queue("amq.direct")
Modified: incubator/qpid/trunk/qpid/python/qpid/__init__.py
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/__init__.py?rev=592888&r1=592887&r2=592888&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/__init__.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/__init__.py Wed Nov 7 12:46:36 2007
@@ -21,9 +21,18 @@
class Struct:
- def __init__(self, type):
+ def __init__(self, type, *args, **kwargs):
self.__dict__["type"] = type
self.__dict__["_values"] = {}
+
+ if len(args) > len(self.type.fields):
+ raise TypeError("too many args")
+
+ for a, f in zip(args, self.type.fields):
+ self.set(f.name, a)
+
+ for k, a in kwargs.items():
+ self.set(k, a)
def _check(self, attr):
field = self.type.fields.byname.get(attr)
Modified: incubator/qpid/trunk/qpid/python/qpid/client.py
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/client.py?rev=592888&r1=592887&r2=592888&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/client.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/client.py Wed Nov 7 12:46:36 2007
@@ -22,7 +22,7 @@
interacting with the server.
"""
-import threading
+import os, threading
from peer import Peer, Closed
from delegate import Delegate
from connection import Connection, Frame, connect
@@ -33,10 +33,18 @@
class Client:
- def __init__(self, host, port, spec, vhost = None):
+ def __init__(self, host, port, spec = None, vhost = None):
self.host = host
self.port = port
- self.spec = spec
+ if spec:
+ self.spec = spec
+ else:
+ try:
+ name = os.environ["AMQP_SPEC"]
+ except KeyError:
+ raise EnvironmentError("environment variable AMQP_SPEC must be set")
+ self.spec = load(name)
+ self.structs = StructFactory(self.spec)
self.mechanism = None
self.response = None
@@ -158,3 +166,22 @@
self.client.closed = True
self.client.reason = reason
self.client.started.set()
+
+class StructFactory:
+
+ def __init__(self, spec):
+ self.spec = spec
+ self.factories = {}
+
+ def __getattr__(self, name):
+ if self.factories.has_key(name):
+ return self.factories[name]
+ elif self.spec.domains.byname.has_key(name):
+ f = lambda *args, **kwargs: self.struct(name, *args, **kwargs)
+ self.factories[name] = f
+ return f
+ else:
+ raise AttributeError(name)
+
+ def struct(self, name, *args, **kwargs):
+ return self.spec.struct(name, *args, **kwargs)
Modified: incubator/qpid/trunk/qpid/python/qpid/spec.py
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/python/qpid/spec.py?rev=592888&r1=592887&r2=592888&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/python/qpid/spec.py (original)
+++ incubator/qpid/trunk/qpid/python/qpid/spec.py Wed Nov 7 12:46:36 2007
@@ -115,9 +115,9 @@
klass, meth = parts
return self.classes.byname[klass].methods.byname[meth]
- def struct(self, name):
+ def struct(self, name, *args, **kwargs):
type = self.domains.byname[name].type
- return qpid.Struct(type)
+ return qpid.Struct(type, *args, **kwargs)
def define_module(self, name, doc = None):
module = new.module(name, doc)