PageRenderTime 35ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/apiary/tools/codec.py

https://bitbucket.org/lindenlab/apiary/
Python | 140 lines | 94 code | 6 blank | 40 comment | 2 complexity | ed94663c4e4c61fec1440978e5d40c2d MD5 | raw file
  1. #
  2. # $LicenseInfo:firstyear=2010&license=mit$
  3. #
  4. # Copyright (c) 2010, Linden Research, Inc.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. # $/LicenseInfo$
  24. #
  25. '''
  26. codec provides abstractions for messages encodings which live in message queues or data files.
  27. '''
  28. import re
  29. import simplejson
  30. from cStringIO import StringIO
  31. class Message (object):
  32. '''
  33. A message has an arbitrary structure of headers and an opaque byte-sequence body.
  34. The headers structure is a dict containing any values which can be encoded with json.
  35. '''
  36. MaxElementSize = 2 ** 27 # 128 MiB
  37. BodyLengthKey = 'body_length'
  38. PrologTemplate = '# Apiary Message: %d header bytes.\n'
  39. PrologPattern = re.compile(r'# Apiary Message: (\d+) header bytes.')
  40. @classmethod
  41. def decode_many_from_file(cls, fp):
  42. '''
  43. This returns an iterator which yields messages as they are parsed.
  44. '''
  45. m = cls.decode_from_file(fp)
  46. while m:
  47. yield m
  48. m = cls.decode_from_file(fp)
  49. @classmethod
  50. def decode_from_string(cls, source):
  51. return cls.decode_from_file(StringIO(source))
  52. @classmethod
  53. def decode_from_file(cls, fp):
  54. '''
  55. Given a file-like object, return a Message instance, None, or raise a FormatException.
  56. None is raised if the file is at EOF.
  57. '''
  58. prolog = fp.readline()
  59. if prolog == '':
  60. return None
  61. m = cls.PrologPattern.match(prolog)
  62. if m is None:
  63. raise FormatError('Could not decode prolog: %r', prolog)
  64. hlen = int(m.group(1))
  65. if hlen > cls.MaxElementSize:
  66. raise FormatError('Prolog header length %d is larger than the maximum allowed %d bytes.',
  67. hlen,
  68. cls.MaxElementSize)
  69. headerchunk = fp.read(hlen)
  70. newline = fp.read(1)
  71. if newline != '\n':
  72. raise FormatError('Headers not terminated by a newline.')
  73. headers = simplejson.loads(headerchunk)
  74. if type(headers) is not dict:
  75. raise FormatError('Headers must be a mapping, but stream contains: %r',
  76. headers)
  77. # Translate the keys to utf8 so that ** application works:
  78. headers = dict( ((k.encode('utf8'), v) for (k, v) in headers.items()) )
  79. bodylength = headers.get(cls.BodyLengthKey)
  80. if type(bodylength) is not int or not (0 <= bodylength < cls.MaxElementSize):
  81. raise FormatError('Invalid %r header: %r',
  82. cls.BodyLengthKey,
  83. bodylength)
  84. body = fp.read(bodylength)
  85. newline = fp.read(1)
  86. if newline != '\n':
  87. raise FormatError('Body not terminated by a newline.')
  88. try:
  89. return cls(body, **headers)
  90. except TypeError, e:
  91. raise FormatError('Invalid headers - %s: %r',
  92. ' '.join(e.args),
  93. headers)
  94. def __init__(self, body, **headers):
  95. if headers.has_key(self.BodyLengthKey):
  96. lenhdr = headers.pop(self.BodyLengthKey)
  97. if len(body) != lenhdr:
  98. raise FormatError('Incorrect %s header: %d != %d', self.BodyLengthKey, lenhdr, len(body))
  99. self.body = body
  100. self.headers = headers
  101. def encode_to_string(self):
  102. f = StringIO()
  103. self.encode_to_file(f)
  104. return f.getvalue()
  105. def encode_to_file(self, fp):
  106. hdrs = self.headers.copy()
  107. hdrs[self.BodyLengthKey] = len(self.body)
  108. try:
  109. headerchunk = simplejson.dumps(hdrs)
  110. except TypeError, e:
  111. raise FormatError('Failure to encode headers - %s: %r',
  112. ' '.join(e.args),
  113. hdrs)
  114. hlen = len(headerchunk)
  115. if hlen > self.MaxElementSize:
  116. raise FormatError('Header encoding is %d bytes which is larger than the maximum allowed %d bytes.',
  117. hlen,
  118. self.MaxElementSize)
  119. fp.write(self.PrologTemplate % hlen)
  120. fp.write(headerchunk + '\n')
  121. fp.write(self.body + '\n')
  122. class FormatError (Exception):
  123. def __init__(self, tmpl, *args):
  124. Exception.__init__(self, tmpl % args)