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

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


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

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


=== Zope/lib/python/ZTUtils/Tree.py 1.4.14.2 => 1.4.14.3 ===
--- Zope/lib/python/ZTUtils/Tree.py:1.4.14.2	Fri Oct  4 12:51:43 2002
+++ Zope/lib/python/ZTUtils/Tree.py	Fri Oct  4 22:10:44 2002
@@ -151,6 +151,7 @@
 from binascii import b2a_base64, a2b_base64
 import string
 from string import split, join, translate
+import zlib
 
 a2u_map = string.maketrans('+/=', '-._')
 u2a_map = string.maketrans('-._', '+/=')
@@ -178,7 +179,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().
@@ -198,8 +199,11 @@
         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.
 
@@ -207,6 +211,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 = []