Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 from base64 import b64encode, b64decode
00002 from zlib import compress, decompress
00003 
00004 
00005 def encodeStrings(strings):
00006     """ compress and encode a list of strings into a single string """
00007     def _encode(strings):
00008         for string in strings:
00009             yield '%d:%s' % (len(string), string)
00010     return b64encode(compress(''.join(_encode(strings))))
00011 
00012 
00013 def decodeStrings(data):
00014     """ decode and uncompress a string as a generator """
00015     data = decompress(b64decode(data))
00016     while data:
00017         pos = data.find(':')
00018         end = pos + int(data[:pos]) + 1
00019         yield data[pos+1:end]
00020         data = data[end:]
00021 
00022 
00023 def encodeRequestData((body, env)):
00024     """ encode the relevant request data, i.e. body and env """
00025     def _iterdata():
00026         yield body
00027         for key, val in env.iteritems():
00028             yield str(key)
00029             yield str(val)
00030     return encodeStrings(_iterdata())
00031 
00032 
00033 def decodeRequestData(data):
00034     """ decode the relevant request data, i.e. body and env """
00035     def _pertwo():
00036         while True:
00037             yield data.next(), data.next()
00038     data = decodeStrings(data)
00039     return data.next(), dict(_pertwo())
00040 
00041 
00042 def encodeInts(ints):
00043     """ encode a list of integers into a string """
00044     return ':'.join(map(str, ints))
00045 
00046 
00047 def decodeInts(data):
00048     """ decode a string to a list of integers """
00049     return map(int, data.split(':'))
00050