~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/amqplib/client_0_8/basic_message.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Messages for AMQP
 
3
 
 
4
"""
 
5
# Copyright (C) 2007-2008 Barry Pederson <bp@barryp.org>
 
6
#
 
7
# This library is free software; you can redistribute it and/or
 
8
# modify it under the terms of the GNU Lesser General Public
 
9
# License as published by the Free Software Foundation; either
 
10
# version 2.1 of the License, or (at your option) any later version.
 
11
#
 
12
# This library is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
# Lesser General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU Lesser General Public
 
18
# License along with this library; if not, write to the Free Software
 
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 
20
 
 
21
 
 
22
from serialization import GenericContent
 
23
 
 
24
__all__ =  [
 
25
            'Message',
 
26
           ]
 
27
 
 
28
 
 
29
class Message(GenericContent):
 
30
    """
 
31
    A Message for use with the Channnel.basic_* methods.
 
32
 
 
33
    """
 
34
    #
 
35
    # Instances of this class have these attributes, which
 
36
    # are passed back and forth as message properties between
 
37
    # client and server
 
38
    #
 
39
    PROPERTIES = [
 
40
        ('content_type', 'shortstr'),
 
41
        ('content_encoding', 'shortstr'),
 
42
        ('application_headers', 'table'),
 
43
        ('delivery_mode', 'octet'),
 
44
        ('priority', 'octet'),
 
45
        ('correlation_id', 'shortstr'),
 
46
        ('reply_to', 'shortstr'),
 
47
        ('expiration', 'shortstr'),
 
48
        ('message_id', 'shortstr'),
 
49
        ('timestamp', 'timestamp'),
 
50
        ('type', 'shortstr'),
 
51
        ('user_id', 'shortstr'),
 
52
        ('app_id', 'shortstr'),
 
53
        ('cluster_id', 'shortstr')
 
54
        ]
 
55
 
 
56
    def __init__(self, body='', children=None, **properties):
 
57
        """
 
58
        Expected arg types
 
59
 
 
60
            body: string
 
61
            children: (not supported)
 
62
 
 
63
        Keyword properties may include:
 
64
 
 
65
            content_type: shortstr
 
66
                MIME content type
 
67
 
 
68
            content_encoding: shortstr
 
69
                MIME content encoding
 
70
 
 
71
            application_headers: table
 
72
                Message header field table, a dict with string keys,
 
73
                and string | int | Decimal | datetime | dict values.
 
74
 
 
75
            delivery_mode: octet
 
76
                Non-persistent (1) or persistent (2)
 
77
 
 
78
            priority: octet
 
79
                The message priority, 0 to 9
 
80
 
 
81
            correlation_id: shortstr
 
82
                The application correlation identifier
 
83
 
 
84
            reply_to: shortstr
 
85
                The destination to reply to
 
86
 
 
87
            expiration: shortstr
 
88
                Message expiration specification
 
89
 
 
90
            message_id: shortstr
 
91
                The application message identifier
 
92
 
 
93
            timestamp: datetime.datetime
 
94
                The message timestamp
 
95
 
 
96
            type: shortstr
 
97
                The message type name
 
98
 
 
99
            user_id: shortstr
 
100
                The creating user id
 
101
 
 
102
            app_id: shortstr
 
103
                The creating application id
 
104
 
 
105
            cluster_id: shortstr
 
106
                Intra-cluster routing identifier
 
107
 
 
108
        Unicode bodies are encoded according to the 'content_encoding'
 
109
        argument. If that's None, it's set to 'UTF-8' automatically.
 
110
 
 
111
        example:
 
112
 
 
113
            msg = Message('hello world',
 
114
                            content_type='text/plain',
 
115
                            application_headers={'foo': 7})
 
116
 
 
117
        """
 
118
        if isinstance(body, unicode):
 
119
            if properties.get('content_encoding', None) is None:
 
120
                properties['content_encoding'] = 'UTF-8'
 
121
            self.body = body.encode(properties['content_encoding'])
 
122
        else:
 
123
            self.body = body
 
124
 
 
125
        super(Message, self).__init__(**properties)
 
126
 
 
127
 
 
128
    def __eq__(self, other):
 
129
        """
 
130
        Check if the properties and bodies of this Message and another
 
131
        Message are the same.
 
132
 
 
133
        Received messages may contain a 'delivery_info' attribute,
 
134
        which isn't compared.
 
135
 
 
136
        """
 
137
        return super(Message, self).__eq__(other) and (self.body == other.body)