Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_message.py 3.9KB

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