diff --git a/README.md b/README.md index 71277c375350026ecaadbade3ca5005a2b27d033..40da0188b3bcd644851329c988b1a572d430e5d5 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,6 @@ Enjoy! Assertions are sprinkled throughout the code. You may wish to run with optimizations enabled if you need every CPU cycle you can get. -**v0.8** series has unstable API. +**v0.8x** series has unstable API. -**v0.9** series will have a stable API. \ No newline at end of file +**v0.9x** series will have a stable API. \ No newline at end of file diff --git a/coolamqp/attaches/consumer.py b/coolamqp/attaches/consumer.py index cbfe6ecad644547e70ffff7bf687f4f10606c917..f0da62eead8850d1f49476df4cf57e6bc230454a 100644 --- a/coolamqp/attaches/consumer.py +++ b/coolamqp/attaches/consumer.py @@ -35,6 +35,8 @@ class Consumer(Channeler): Note that does not attempt to cancel consumers, or any of such nonsense. Having a channel per consumer gives you the unique possibility of simply closing the channel. Since this implies cancelling the consumer, here you go. + + WARNING: READ DEFAULT VALUES IN CONSTRUCTOR! TAKE CARE WHAT YOUR CONSUMERS DO! """ def __init__(self, queue, on_message, no_ack=True, qos=None, cancel_on_failure=False, diff --git a/coolamqp/objects.py b/coolamqp/objects.py index d02c7b6ed9f55deb78b4a87c0c9e640cba064ee0..31a099f7d8b9ce430f930188be01b78c68178124 100644 --- a/coolamqp/objects.py +++ b/coolamqp/objects.py @@ -243,16 +243,27 @@ class NodeDefinition(object): """ Create a cluster node definition. - a = ClusterNode(host='192.168.0.1', user='admin', password='password', + a = NodeDefinition(host='192.168.0.1', user='admin', password='password', virtual_host='vhost') or - a = ClusterNode('192.168.0.1', 'admin', 'password') + a = NodeDefinition('192.168.0.1', 'admin', 'password') + + or + + a = NodeDefinition('amqp://user:password@host/virtual_host') + + or + + a = NodeDefinition('amqp://user:password@host:port/virtual_host', hearbeat=20) + Additional keyword parameters that can be specified: heartbeat - heartbeat interval in seconds port - TCP port to use. Default is 5672 + + :raise ValueError: invalid parameters """ self.heartbeat = kwargs.pop('heartbeat', None) @@ -269,8 +280,47 @@ class NodeDefinition(object): self.virtual_host = '/' elif len(args) == 4: self.host, self.user, self.password, self.virtual_host = args + elif len(args) == 1 and isinstance(args[0], six.text_type): + # AMQP connstring + connstr = args[0] + if not connstr.startswith(u'amqp://'): + raise ValueError(u'should begin with amqp://') + + connstr = connstr.replace(u'amqp://', u'') + self.user, connstr = connstr.split(u':', 1) + self.password, connstr = connstr.split(u'@', 1) + self.host, self.virtual_host = connstr.split(u'/', 1) + + if len(self.virtual_host) == 0: + # empty virtual host is / + self.virtual_host = u'/' + + if u':' in self.host: + host, port = self.host.split(u':', 1) + self.port = int(port) + # else get that port from kwargs else: - raise NotImplementedError #todo implement this + raise ValueError(u'What did you exactly pass?') + + @staticmethod + def from_str(connstr, **kwargs): + """ + Return a NodeDefinition from an AMQP connection string. + + It should look like: + + + + :param connstr: a unicode + + :param heartbeat: heartbeat to use + + :return: NodeDefinition instance + :raise ValueError: invalid string + """ + + return NodeDefinition(host=host, port=5672, user=user, password=passw, virtual_host=virtual_host, heartbeat=kwargs.get('heartbeat', None)) + def __str__(self): return six.text_type(b'amqp://%s:%s@%s/%s'.encode('utf8') % (self.host, self.port, self.user, self.virtual_host)) \ No newline at end of file diff --git a/setup.py b/setup.py index dfc890aac4bb149526a9e4b226fea348dc5ea775..2a2fb2a897cfc51c10c2b25f4c0d42f2065a46b2 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup(name=u'CoolAMQP', - version='0.8', + version='0.80', description=u'The fastest AMQP client', author=u'DMS Serwis s.c.', author_email=u'piotrm@smok.co', diff --git a/tests/test_objects.py b/tests/test_objects.py new file mode 100644 index 0000000000000000000000000000000000000000..6a4c0c69e5700e5fdd3907494aaadef6eac5bc08 --- /dev/null +++ b/tests/test_objects.py @@ -0,0 +1,25 @@ +# coding=UTF-8 +""" +It sounds like a melody +""" +from __future__ import print_function, absolute_import, division +import six +import unittest + + +from coolamqp.objects import NodeDefinition + + +class TestObjects(unittest.TestCase): + def test_node_definition_from_amqp(self): + + n1 = NodeDefinition(u'amqp://ala:ma@kota/psa') + + self.assertEquals(n1.user, u'ala') + self.assertEquals(n1.password, u'ma') + self.assertEquals(n1.virtual_host, u'psa') + self.assertEquals(n1.host, u'kota') + + n1 = NodeDefinition(u'amqp://ala:ma@kota/') + + self.assertEquals(n1.virtual_host, u'/')