summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rwxr-xr-xpython/hello-world7
-rw-r--r--python/qpid/__init__.py11
-rw-r--r--python/qpid/client.py33
-rw-r--r--python/qpid/spec.py4
4 files changed, 44 insertions, 11 deletions
diff --git a/python/hello-world b/python/hello-world
index 5ba14b0fc1..d419fd988b 100755
--- a/python/hello-world
+++ b/python/hello-world
@@ -3,8 +3,7 @@ import qpid
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 @@ ch.message_flow("amq.direct", 1, 0xFFFFFFFF)
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")
diff --git a/python/qpid/__init__.py b/python/qpid/__init__.py
index 7afebaf73b..ff9cc04df8 100644
--- a/python/qpid/__init__.py
+++ b/python/qpid/__init__.py
@@ -21,10 +21,19 @@ import spec, codec, connection, content, peer, delegate, client
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)
if field == None:
diff --git a/python/qpid/client.py b/python/qpid/client.py
index 37df8f272a..44fd6c053f 100644
--- a/python/qpid/client.py
+++ b/python/qpid/client.py
@@ -22,7 +22,7 @@ An AQMP client implementation that uses a custom delegate for
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 @@ from reference import ReferenceId, References
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 @@ class ClientDelegate(Delegate):
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)
diff --git a/python/qpid/spec.py b/python/qpid/spec.py
index 4d0497bced..537ced113b 100644
--- a/python/qpid/spec.py
+++ b/python/qpid/spec.py
@@ -115,9 +115,9 @@ class Spec(Metadata):
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)