[Zope-Checkins] CVS: Zope/lib/python/ZTUtils - Tree.py:1.6.6.5

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


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

Modified Files:
      Tag: Zope-2_6-branch
	Tree.py 
Log Message:
Merge zlib compression for tree expansion maps from trunk.


=== Zope/lib/python/ZTUtils/Tree.py 1.6.6.4 => 1.6.6.5 ===
--- Zope/lib/python/ZTUtils/Tree.py:1.6.6.4	Fri Oct  4 12:51:24 2002
+++ Zope/lib/python/ZTUtils/Tree.py	Fri Oct  4 22:10:58 2002
@@ -150,6 +150,7 @@
 
 from binascii import b2a_base64, a2b_base64
 from string import translate, maketrans
+import zlib
 
 a2u_map = maketrans('+/=', '-._')
 u2a_map = maketrans('-._', '+/=')
@@ -177,7 +178,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().
@@ -197,7 +198,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.
@@ -206,6 +210,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 = []