[pypy-svn] rev 1466 - pypy/trunk/doc/funding/tools

dinu at codespeak.net dinu at codespeak.net
Tue Sep 30 15:26:35 CEST 2003


Author: dinu
Date: Tue Sep 30 15:26:34 2003
New Revision: 1466

Added:
   pypy/trunk/doc/funding/tools/zipmod.py
Log:
initial checkin

Added: pypy/trunk/doc/funding/tools/zipmod.py
==============================================================================
--- (empty file)
+++ pypy/trunk/doc/funding/tools/zipmod.py	Tue Sep 30 15:26:34 2003
@@ -0,0 +1,72 @@
+#! /usr/bin/env python
+
+import os, sys, zipfile
+
+# s = """..... <text:p text:style-name="P4">:DELETE:BEGIN</text:p><text:p text:style-name="P4"/><table:table table:name="Table3" table:style-name="Table3"><table:table-column table:style-name="Table3.A"/><text:p text:style-name="P4">:DELETE:END</text:p> ----------""" * 2
+
+def filter(s):
+    "Remove string all parts of a string within certain tags."
+    
+    text0 = '<text:p text:style-name="P4">'
+    text1 = '</text:p>'
+    tag0 = ":DELETE:BEGIN"
+    tag1 = ":DELETE:END"
+
+    list = []
+    while 1:
+        p = s.find(tag0)
+        if p == -1: break
+        s0 = s[:p]
+        t0 = s[p-len(text0):p]
+        t1 = s[p+len(tag0):p+len(tag0)+len(text1)]
+        list.append(s[:p-len(text0)])
+        s = s[p+len(tag0)+len(text1):]
+        p = s.find(tag1)
+        if p == -1: break
+        s0 = s[:p]
+        t0 = s[p-len(text0):p]
+        t1 = s[p+len(tag1):p+len(tag1)+len(text1)]
+        s = s[p+len(tag1)+len(text1):]
+    list.append(s)
+
+    return ''.join(list)
+
+
+def modifyOpenOfficeContent(docPath, filename, func):
+    "Read the content XML file from a zipped Open Office document."
+
+    # read ZIP file, build {filename:open(filename).read(), ...}
+    zip = zipfile.ZipFile(docPath, 'r')
+    filenames = zip.namelist()
+    filemap = {}
+    for fn in filenames:
+        filemap[fn] = zip.read(fn)
+    zip.close()
+
+    # modify specific filename by applying func to it
+    content = filemap[filename]
+    modContent = func(content)
+    filemap[filename] = modContent
+
+    # build temp. directory structure as in ZIP file
+    # (zipfile has no extract or modify method!)
+    tmp = 'tmp'
+    try: os.mkdir(tmp)
+    except OSError: pass
+    for fn in filenames:
+        dirName = os.path.join(tmp, os.path.dirname(fn))
+        try: os.mkdir(dirName)
+        except OSError: pass
+        destPath = os.path.join(tmp, fn)
+        open(destPath, 'w').write(filemap[fn])
+
+    # rebuild a copy of the original ZIP file from tmp. dir.
+    zip = zipfile.ZipFile(docPath, 'w', zipfile.ZIP_DEFLATED)
+    for fn in filenames:
+        destPath = os.path.join(tmp, fn)
+        zip.write(destPath, fn)
+
+
+if __name__ == '__main__':
+    docPath = sys.argv[1]
+    modifyOpenOfficeContent(docPath, 'content.xml', filter)


More information about the Pypy-commit mailing list