Package qpid :: Package messaging :: Module message
[frames] | no frames]

Source Code for Module qpid.messaging.message

  1  # 
  2  # Licensed to the Apache Software Foundation (ASF) under one 
  3  # or more contributor license agreements.  See the NOTICE file 
  4  # distributed with this work for additional information 
  5  # regarding copyright ownership.  The ASF licenses this file 
  6  # to you under the Apache License, Version 2.0 (the 
  7  # "License"); you may not use this file except in compliance 
  8  # with the License.  You may obtain a copy of the License at 
  9  # 
 10  #   http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, 
 13  # software distributed under the License is distributed on an 
 14  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 15  # KIND, either express or implied.  See the License for the 
 16  # specific language governing permissions and limitations 
 17  # under the License. 
 18  # 
 19   
 20  from qpid.codec010 import StringCodec 
 21  from qpid.ops import PRIMITIVE 
 22   
23 -def codec(name):
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 # XXX: need to correctly parse the mime type and deal with 38 # content-encoding header 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
52 -def encode_text_plain(x):
53 if x is None: 54 return None 55 else: 56 return x.encode("utf8")
57
58 -def decode_text_plain(x):
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
73 -def get_type(content):
74 return TYPE_MAPPINGS[content.__class__]
75
76 -def get_codec(content_type):
77 return TYPE_CODEC.get(content_type, DEFAULT_CODEC)
78 79 UNSPECIFIED = object() 80
81 -class Message:
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
144 - def __repr__(self):
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
162 -class Disposition:
163
164 - def __init__(self, type, **options):
165 self.type = type 166 self.options = options
167
168 - def __repr__(self):
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