diff --git a/coolamqp/framing/base.py b/coolamqp/framing/base.py index 5f7ccf2514863f5f2614dbc123edd551e37570d4..31753c93c5e9e1bc99f6792c1709a492ff626b61 100644 --- a/coolamqp/framing/base.py +++ b/coolamqp/framing/base.py @@ -92,6 +92,15 @@ class AMQPContentPropertyList(object): # todo they are immutable, so they could just serialize themselves... + def get(self, property_name, default=None): + """ + Return a particular property, or default if not defined + :param property_name: property name, unicode + :param default: default value + :return: memoryview or bytes + """ + return getattr(self, property_name, default) + @staticmethod def zero_property_flags(property_flags): """ diff --git a/coolamqp/framing/compilation/compile_definitions.py b/coolamqp/framing/compilation/compile_definitions.py index 0109147ab1d5dca885626b17cac36fd80718b514..62a957d53a2ba88897478d87fae438cd1cd6f008 100644 --- a/coolamqp/framing/compilation/compile_definitions.py +++ b/coolamqp/framing/compilation/compile_definitions.py @@ -55,7 +55,7 @@ binary string? It's a memoryview all right. Only thing that isn't are field names in tables. """ -import struct, collections, warnings, logging, six +import struct, collections, logging, six from coolamqp.framing.base import AMQPClass, AMQPMethodPayload, AMQPContentPropertyList from coolamqp.framing.field_table import enframe_table, deframe_table, frame_table_size @@ -230,7 +230,7 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved # # If you do not know in advance what properties you will be using, it is correct to use # this constructor. - + if zpf in BasicContentPropertyList.PARTICULAR_CLASSES: return %s.PARTICULAR_CLASSES[zpf](**kwargs) else: logger.debug('Property field (%s:%d) not seen yet, compiling', repr(zpf)) diff --git a/coolamqp/framing/definitions.py b/coolamqp/framing/definitions.py index cab1e0787980ef1a353b69ea557e695e8e2e5df2..e472c3d376f2de66819fb6592677c6cfe8b5dd92 100644 --- a/coolamqp/framing/definitions.py +++ b/coolamqp/framing/definitions.py @@ -23,7 +23,7 @@ binary string? It's a memoryview all right. Only thing that isn't are field names in tables. """ -import struct, collections, warnings, logging, six +import struct, collections, logging, six from coolamqp.framing.base import AMQPClass, AMQPMethodPayload, AMQPContentPropertyList from coolamqp.framing.field_table import enframe_table, deframe_table, frame_table_size @@ -2359,6 +2359,23 @@ class BasicContentPropertyList(AMQPContentPropertyList): ]) zpf = six.binary_type(zpf) +# If you know in advance what properties you will be using, use typized constructors like +# +# runs once +# my_type = BasicContentPropertyList.typize('content_type', 'content_encoding') +# +# runs many times +# props = my_type('text/plain', 'utf8') +# +# instead of +# +# # runs many times +# props = BasicContentPropertyList(content_type='text/plain', content_encoding='utf8') +# +# This way you will be faster. +# +# If you do not know in advance what properties you will be using, it is correct to use +# this constructor. if zpf in BasicContentPropertyList.PARTICULAR_CLASSES: return BasicContentPropertyList.PARTICULAR_CLASSES[zpf](**kwargs) else: diff --git a/tests/test_objects.py b/tests/test_objects.py index 6a4c0c69e5700e5fdd3907494aaadef6eac5bc08..e3a109d0ef84031ec3b61bed2fbebc59d73a8f50 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -5,9 +5,7 @@ It sounds like a melody from __future__ import print_function, absolute_import, division import six import unittest - - -from coolamqp.objects import NodeDefinition +from coolamqp.objects import NodeDefinition, MessageProperties class TestObjects(unittest.TestCase): @@ -23,3 +21,10 @@ class TestObjects(unittest.TestCase): n1 = NodeDefinition(u'amqp://ala:ma@kota/') self.assertEquals(n1.virtual_host, u'/') + + def test_get_message_properties(self): + empty_p_msg = MessageProperties() + ce_p_msg = MessageProperties(content_encoding=b'wtf') + + self.assertIsNone(empty_p_msg.get('content_encoding'), None) + self.assertEquals(ce_p_msg.get('content_encoding', b'wtf'), b'wtf')