[pypy-svn] rev 1491 - pypy/trunk/doc/funding/tools
tismer at codespeak.net
tismer at codespeak.net
Wed Oct 1 03:54:33 CEST 2003
Author: tismer
Date: Wed Oct 1 03:54:32 2003
New Revision: 1491
Removed:
pypy/trunk/doc/funding/tools/format.py
pypy/trunk/doc/funding/tools/zipmod.py
Log:
moving tools to makedoc
Deleted: /pypy/trunk/doc/funding/tools/format.py
==============================================================================
--- /pypy/trunk/doc/funding/tools/format.py Wed Oct 1 03:54:32 2003
+++ (empty file)
@@ -1,263 +0,0 @@
-import re
-import sys
-
-START = re.compile(r'\s*<[^/][^<]*?>',re.DOTALL)
-STOP = re.compile(r'\s*</[^<]*?>',re.DOTALL)
-EMPTY = re.compile(r'\s*<[^/][^<]*?/>',re.DOTALL)
-OTHER = re.compile(r'\s*<[!\?][^<]*>')
-ANY = re.compile(r'[^<]*',re.DOTALL)
-tab = ' '
-
-tabprop = """<style:properties fo:padding-left="0.191cm" fo:padding-right="0.191cm" fo:padding-top="0cm" fo:padding-bottom="0cm" fo:border-left="0.035cm solid #000000" fo:border-right="none" fo:border-top="0.035cm solid #000000" fo:border-bottom="none"/>"""
-
-class parse(object):
- def __init__(self,text):
- self.text = text
- self.index = 0
-
- def extractTagName(self,txt):
- txt = txt.strip()
- if not txt.startswith('<'):
- return ''
- if txt.find('/') != -1:
- return ''
- i = txt.find(' ')
- if i != -1:
- tagname = txt[1:i]
- else:
- tagname = txt[1:-1]
-
- #print >> sys.stderr,tagname
- return tagname
-
- def start(self,txt):pass
- def stop(self,txt):pass
- def empty(self,txt):pass
- def other(self,txt):pass
- def any(self,txt):pass
- def parse(self):
- content = self.text
- txtlen = len(content)
- while txtlen > self.index:
- m = OTHER.match(content,self.index)
- if m is not None:
- #print 'OTHER:',m.group(),m.span()
- self.index = m.span()[1]
- self.other(m.group())
- continue
- m = EMPTY.match(content,self.index)
- if m is not None:
- #print 'EMPTY:',m.group(),m.span()
- self.index = m.span()[1]
- self.empty(m.group())
- continue
- m = START.match(content,self.index)
- if m is not None:
- #print 'START:',m.group(),m.span()
- self.index = m.span()[1]
- self.start(m.group())
- continue
- m = STOP.match(content,self.index)
- if m is not None:
- #print 'STOP:',m.group(),m.span()
- self.index = m.span()[1]
- self.stop(m.group())
- continue
- m = ANY.match(content,self.index)
- if m is not None:
- #print 'ANY:',m.group(),m.span()
- self.index = m.span()[1]
- txt = m.group()
- txt.strip()
- self.any(txt)
- continue
-
-class format(parse):
- def __init__(self,*argl,**argd):
- super(format,self).__init__(*argl,**argd)
- self.level = 0
- self.outbuf = []
-
- def __str__(self):
- self.parse()
- return '\n'.join(self.outbuf)
-
- def start(self,txt):
- self.outbuf.append(tab*self.level+txt)
- self.level += 1
-
- def stop(self,txt):
- self.level -= 1
- self.outbuf.append(tab*self.level+txt)
-
- def any(self,txt):
- self.outbuf.append(tab*self.level+txt)
-
- def empty(self,txt):
- self.outbuf.append(tab*self.level+txt)
-
- def other(self,txt):
- self.outbuf.append(tab*self.level+txt)
-
-class findTableName(parse):
- def __init__(self,*argl,**argd):
- super(findTableName,self).__init__(*argl,**argd)
- self.tabname = {}
- self.currTable = ''
-
- def any(self,txt):
- if self.currTable and txt.find('Workpackage number') != -1:
- self.tabname[self.currTable] = True
-
- def stop(self,txt):
- if txt.startswith('</table:table'):
- self.currTable = ''
-
- def start(self,txt):
- if txt.startswith('<table:table'):
- i = txt.find('table:name')
- if i != -1:
- start = txt.find('"',i) +1
- stop = txt.find('"',start)
- self.currTable = txt[start:stop]
-
- def __str__(self):
- self.parse()
- return str(self.tabname)
-
- def __call__(self):
- self.parse()
- return self.tabname.keys()
-
-class replaceMargin(format):
- def __init__(self,*argl,**argd):
- super(replaceMargin,self).__init__(*argl,**argd)
- self.tabnames = findTableName(self.text)()
- #print >> sys.stderr,self.tabnames
- self.inStyle = True
-
- def __str__(self):
- self.parse()
- return ''.join(self.outbuf)
-
- def start(self,txt):
- i = txt.find(' ')
- if i:
- elem = txt[1:i]
- else:
- elem = txt[1:-1]
- txt = txt.strip()
- if txt.startswith('<style:style'):
- for tab in self.tabnames:
- if txt.find(tab) != -1 and txt.find('table-cell') != -1:
- #print >> sys.stderr,'found table-cell'
- self.inStyle = True
- self.outbuf.append(txt)
-
- def stop(self,txt):
- txt = txt.strip()
- if self.inStyle:
- self.inStyle = False
- self.outbuf.append(txt)
-
- def other(self,txt):
- txt = txt.strip()
- self.outbuf.append(txt)
-
- def empty(self,txt):
- txt = txt.strip()
- if self.inStyle and txt.find('style:properties') != -1:
- self.outbuf.append(tabprop)
- self.inStyle = False
- else:
- self.outbuf.append(txt)
-
- def any(self,txt):
- txt = txt.strip()
- if txt:
- self.outbuf.append(txt)
-
-class cut(format):
- def __init__(self,*argl,**argd):
- super(cut,self).__init__(*argl,**argd)
- self.level = 0
- self.write = True
- self.stack = []
-
- def __str__(self):
- self.parse()
- return ''.join(self.outbuf)
-
- def start(self,txt):
- if self.write:
- self.outbuf.append(txt)
- self.stack.append(self.write)
-
- def other(self,txt):
- if self.write:
- self.outbuf.append(txt)
-
- def stop(self,txt):
- write = self.stack.pop()
- if write:
- self.outbuf.append(txt)
-
- def empty(self,txt):
- if self.write:
- self.outbuf.append(txt)
-
- def any(self,txt):
- if txt.find('DELETE:BEGIN') != -1:
- if not self.write:
- print >> sys.stderr,self.outbuf[-20:]
- raise Exception('No closing DELETE:END found')
- self.initStartCut()
- elif txt.find('DELETE:END') != -1:
- self.initStopCut()
- else:
- if self.write:
- self.outbuf.append(txt)
-
- def initStartCut(self):
- self.write = False
- self.level = 0
- #CT adjust stack top for enclosing tag
- self.stack.pop()
- self.stack.append(self.write)
- while 1:
- if self.level > 2:
- print >> sys.stderr,'LEN',len(self.outbuf)
- print >> sys.stderr, self.outbuf[-10:]
- raise Exception
- last = self.outbuf.pop()
- last = last.strip()
- print >> sys.stderr,'startCut',last,self.level
- if last.startswith('</'):
- self.level += 1
- continue
- elif last.endswith('/>'):
- continue
- tag = self.extractTagName(last)
- if tag and not self.level:
- break
- elif tag:
- self.level -= 1
- print >> sys.stderr,'DONE'
-
- def initStopCut(self):
- self.write = True
-
-def changeStyle(txt):
- txt = str(cut(txt))
- #txt = str(replaceMargin(txt))
- # not correct yet
- txt = str(format(txt))
- return txt
-
-if __name__ == '__main__':
- import sys
- fn = sys.argv[1]
- content = open(fn).read()
- #f = format(str(replaceMargin(content)))
- f = str(format(content))
- print f
Deleted: /pypy/trunk/doc/funding/tools/zipmod.py
==============================================================================
--- /pypy/trunk/doc/funding/tools/zipmod.py Wed Oct 1 03:54:32 2003
+++ (empty file)
@@ -1,86 +0,0 @@
-#! /usr/bin/env python
-
-import os, sys, zipfile
-from format import changeStyle
-
-# 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.replace('/',os.sep))
- d,f = os.path.split(destPath)
- try:
- os.makedirs(d)
- except:pass
- fs = open(destPath, 'wb')
- fs.write(filemap[fn])
-
- # rebuild a copy of the original ZIP file from tmp. dir.
- name,end = os.path.splitext(docPath)
- newpath = name+'_cp'+end
- zip = zipfile.ZipFile(newpath, 'w', zipfile.ZIP_DEFLATED)
- for fn in filenames:
- destPath = os.path.join(tmp, fn.replace('/',os.sep))
- zip.write(destPath, fn)
-
-
-def filterAll(txt):
- txt = changeStyle(txt)
- #txt = filter(txt)
-
- return txt
-
-if __name__ == '__main__':
- docPath = sys.argv[1]
- modifyOpenOfficeContent(docPath, 'content.xml', filterAll)
More information about the Pypy-commit
mailing list