[ZPT] CVS: Zope/lib/python/ZTUtils - Tree.py:1.12

Martijn Pieters mj@zope.com
Fri, 4 Oct 2002 22:10:02 -0400


Update of /cvs-repository/Zope/lib/python/ZTUtils
In directory cvs.zope.org:/tmp/cvs-serv8973/lib/python/ZTUtils

Modified Files:
	Tree.py 
Log Message:
Use zlib compression on Tree expansion encodings, switched on by default. By
using compression, a far greater number of expanded node states can be
encoded.


=== Zope/lib/python/ZTUtils/Tree.py 1.11 => 1.12 ===
--- Zope/lib/python/ZTUtils/Tree.py:1.11	Fri Oct  4 16:06:52 2002
+++ Zope/lib/python/ZTUtils/Tree.py	Fri Oct  4 22:10:01 2002
@@ -187,6 +187,7 @@
 
 from binascii import b2a_base64, a2b_base64
 from string import translate, maketrans
+import zlib
 
 a2u_map = maketrans('+/=', '-._')
 u2a_map = maketrans('-._', '+/=')
@@ -214,7 +215,7 @@
         frags.append(a2b_base64(s[i:i + 76]))
     return ''.join(frags)
 
-def encodeExpansion(nodes):
+def encodeExpansion(nodes, compress=1):
     '''Encode the expanded node ids of a tree into a string.
 
     Accepts a list of nodes, such as that produced by root.flat().
@@ -234,7 +235,10 @@
         steps.append(node.id)
         node.expansion_number = n
         n = n + 1
-    return ':'.join(steps)
+    result = ':'.join(steps)
+    if compress:
+        result = ':'  + b2a(zlib.compress(result, 9))
+    return result
 
 def decodeExpansion(s, nth=None):
     '''Decode an expanded node map from a string.
@@ -243,6 +247,9 @@
     '''
     if len(s) > 8192: # Set limit to 8K, to avoid DoS attacks.
         raise ValueError('Encoded node map too large')
+
+    if s[0] == ':': # Compressed state
+        s = zlib.decompress(a2b(s[1:]))
     
     map = m = {}
     mstack = []