spoon.ber

Package spoon :: Package ber |
[hide private]
[frames] | no frames]

# Source Code for Package spoon.ber

      1  #
      2  # Copyright (C) 2003-2006, Robey Pointer <robey@lag.net>
      3  #
      4  # Permission is hereby granted, free of charge, to any person obtaining
      5  # a copy of this software and associated documentation files (the
      6  # "Software"), to deal in the Software without restriction, including
      7  # without limitation the rights to use, copy, modify, merge, publish,
      8  # distribute, sublicense, and/or sell copies of the Software, and to
      9  # permit persons to whom the Software is furnished to do so, subject to
     10  # the following conditions:
     11  #
     12  # The above copyright notice and this permission notice shall be included
     13  # in all copies or substantial portions of the Software.
     14  #
     15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     16  # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     17  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     18  # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     19  # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     20  # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     21  # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  #
     23  """
     24  The low level BER based encoding used by spoon to serialize objects.
     25
     26  Users of the spoon library will probably not need to use this package
     27  at all.
     28  """
     29
     30  from cStringIO import StringIO
     31
     32  from common import UNIVERSAL, APPLICATION, CONTEXT, PRIVATE
     33  from common import BERException
     34
     35  from tag import Tag
     36  from stream import BERStream, encoder, zencoder, decoder, encode_container, decode_container
     37  from stream import EOF_TYPE, NULL_TYPE, INT_TYPE, BYTES_TYPE, UTF8_TYPE, LIST_TYPE
     38
     39
     40 -def encode_stream(fd, *items):
     41      """
     42      Encode one or more python objects into a ber stream, written to the
     43      given file object.
     44      """
     45      b = BERStream(fd)
     46      for x in items:
     47          b.add(x)
     48
     49
     50 -def decode_stream(fd):
     51      """
     52      Decode a ber-encoded python object from a file object and return it.
     53      """
     54      return BERStream(fd).next()
     55
     56
     57 -def encode(*items):
     58      """
     59      Encode one or more python objects into a ber stream, and return the
     60      encoded string.
     61      """
     62      s = StringIO()
     63      encode_stream(s, *items)
     64      return s.getvalue()
     65
     66
     67 -def decode(s):
     68      """
     69      Decode a python object from a ber-encoded string and return it.
     70      """
     71      return decode_stream(StringIO(s))
     72
     73  for x in [decode, encode, decode_stream, encode_stream, BERException, Tag,
     74            BERStream, encoder, zencoder,
     75            decoder, encode_container, decode_container,
     76            EOF_TYPE, NULL_TYPE, INT_TYPE, BYTES_TYPE, UTF8_TYPE, LIST_TYPE]:
     77      x.__module__ = "spoon.ber"
     78
     79  __all__ = ["decode",
     80             "encode",
     81             "decode_stream",
     82             "encode_stream",
     83             "UNIVERSAL",
     84             "APPLICATION",
     85             "CONTEXT",
     86             "PRIVATE",
     87             "BERException",
     88             "Tag",
     89             "BERStream",
     90             "encoder",
     91             "zencoder",
     92             "decoder",
     93             "encode_container",
     94             "decode_container",
     95             "EOF_TYPE",
     96             "NULL_TYPE",
     97             "INT_TYPE",
     98             "BYTES_TYPE",
     99             "UTF8_TYPE",
    100             "LIST_TYPE"]
    101