1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 from qpid.codec010 import StringCodec
21 from qpid.ops import PRIMITIVE
22
24 type = PRIMITIVE[name]
25
26 def encode(x):
27 sc = StringCodec()
28 sc.write_primitive(type, x)
29 return sc.encoded
30
31 def decode(x):
32 sc = StringCodec(x)
33 return sc.read_primitive(type)
34
35 return encode, decode
36
37
38
39
40 TYPE_MAPPINGS={
41 dict: "amqp/map",
42 list: "amqp/list",
43 unicode: "text/plain; charset=utf8",
44 unicode: "text/plain",
45 buffer: None,
46 str: None,
47 None.__class__: None
48 }
49
50 DEFAULT_CODEC = (lambda x: x, lambda x: x)
51
53 if x is None:
54 return None
55 else:
56 return x.encode("utf8")
57
59 if x is None:
60 return None
61 else:
62 return x.decode("utf8")
63
64 TYPE_CODEC={
65 "amqp/map": codec("map"),
66 "amqp/list": codec("list"),
67 "text/plain; charset=utf8": (encode_text_plain, decode_text_plain),
68 "text/plain": (encode_text_plain, decode_text_plain),
69 "": DEFAULT_CODEC,
70 None: DEFAULT_CODEC
71 }
72
75
78
79 UNSPECIFIED = object()
80
82
83 """
84 A message consists of a standard set of fields, an application
85 defined set of properties, and some content.
86
87 @type id: str
88 @ivar id: the message id
89 @type subject: str
90 @ivar subject: message subject
91 @type user_id: str
92 @ivar user_id: the user-id of the message producer
93 @type reply_to: str
94 @ivar reply_to: the address to send replies
95 @type correlation_id: str
96 @ivar correlation_id: a correlation-id for the message
97 @type durable: bool
98 @ivar durable: message durability
99 @type priority: int
100 @ivar priority: message priority
101 @type ttl: float
102 @ivar ttl: time-to-live measured in seconds
103 @type properties: dict
104 @ivar properties: application specific message properties
105 @type content_type: str
106 @ivar content_type: the content-type of the message
107 @type content: str, unicode, buffer, dict, list
108 @ivar content: the message content
109 """
110
111 - def __init__(self, content=None, content_type=UNSPECIFIED, id=None,
112 subject=None, user_id=None, reply_to=None, correlation_id=None,
113 durable=None, priority=None, ttl=None, properties=None):
114 """
115 Construct a new message with the supplied content. The
116 content-type of the message will be automatically inferred from
117 type of the content parameter.
118
119 @type content: str, unicode, buffer, dict, list
120 @param content: the message content
121
122 @type content_type: str
123 @param content_type: the content-type of the message
124 """
125 self.id = id
126 self.subject = subject
127 self.user_id = user_id
128 self.reply_to = reply_to
129 self.correlation_id = correlation_id
130 self.durable = durable
131 self.priority = priority
132 self.ttl = ttl
133 self.redelivered = False
134 if properties is None:
135 self.properties = {}
136 else:
137 self.properties = properties
138 if content_type is UNSPECIFIED:
139 self.content_type = get_type(content)
140 else:
141 self.content_type = content_type
142 self.content = content
143
145 args = []
146 for name in ["id", "subject", "user_id", "reply_to", "correlation_id",
147 "priority", "ttl"]:
148 value = self.__dict__[name]
149 if value is not None: args.append("%s=%r" % (name, value))
150 for name in ["durable", "redelivered", "properties"]:
151 value = self.__dict__[name]
152 if value: args.append("%s=%r" % (name, value))
153 if self.content_type != get_type(self.content):
154 args.append("content_type=%r" % self.content_type)
155 if self.content is not None:
156 if args:
157 args.append("content=%r" % self.content)
158 else:
159 args.append(repr(self.content))
160 return "Message(%s)" % ", ".join(args)
161
163
165 self.type = type
166 self.options = options
167
169 args = [str(self.type)] + \
170 ["%s=%r" % (k, v) for k, v in self.options.items()]
171 return "Disposition(%s)" % ", ".join(args)
172
173 __all__ = ["Message", "Disposition"]
174