diff options
author | Rafael H. Schloming <rhs@apache.org> | 2007-11-07 20:46:36 +0000 |
---|---|---|
committer | Rafael H. Schloming <rhs@apache.org> | 2007-11-07 20:46:36 +0000 |
commit | c9900041bf70ac76a4eb8753965fe24426ba8cd4 (patch) | |
tree | 9f8f4d8e54358a59e6f86cf70db15fb37c26ca90 /python/qpid/client.py | |
parent | 710b8a1f1285b9aa5bccee5b1906500667dd7bc5 (diff) | |
download | qpid-python-c9900041bf70ac76a4eb8753965fe24426ba8cd4.tar.gz |
added factory for structs, and made default spec loading based on AMQP_SPEC environment variable
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@592888 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/qpid/client.py')
-rw-r--r-- | python/qpid/client.py | 33 |
1 files changed, 30 insertions, 3 deletions
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) |