From daef4142d04322c9c0e9677ea6db405a4eb3c740 Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Mon, 28 Mar 2011 09:55:24 +0200 Subject: [PATCH] Properly indenting the code, using /usr/share/doc/python2.6/examples/Tools/scripts/reindent.py --- src/tree-cutter.py | 346 ++++++++++++++++++++++----------------------- 1 file changed, 173 insertions(+), 173 deletions(-) diff --git a/src/tree-cutter.py b/src/tree-cutter.py index 35d4bb5..351f5d4 100755 --- a/src/tree-cutter.py +++ b/src/tree-cutter.py @@ -42,200 +42,200 @@ def publish(src,target): print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']' def generateSitemap(): - sitemap = [] - try: - sfile = open('sitemap.txt') - flist = sfile.read().split() + sitemap = [] + try: + sfile = open('sitemap.txt') + flist = sfile.read().split() + sfile.close() + for f in flist: + sitemap.append(dict(link=f)) + except IOError, what_error: + print 'Sitemap missing - generating one.' + + for dirname, dirnames, filenames in os.walk('.'): + for filename in filenames: + if fnmatch.fnmatch(filename, '*.xml'): + xfile = os.path.join(dirname,filename) + doc = bindery.parse(xfile, + prefixes={u'db': u'http://docbook.org/ns/docbook', + u'xi': u'http://www.w3.org/2001/XInclude', + u'xl': u'http://www.w3.org/1999/xlink'}) + title = doc.xml_select(u'/db:article/db:info/db:title') + menu = doc.xml_select(u'/db:article/db:info/db:titleabbrev') + code = doc.xml_select(u"//xi:include[@parse='text']") + resource = doc.xml_select(u"//db:link[@xl:href]") + image = doc.xml_select(u"//db:imagedata[@fileref]") + exe = 0 + for c in code: + (p, ext) = os.path.splitext(c.href) + if ext in valid_scripts: + exe = 1 + + if title and menu: + found = 0 + base = xfile.split('.')[1] + link = base.replace('index','') + level = len(filter(None,re.split(r'(^/\w*/|\w*/)',link))) + res = [] + for r in resource: + rf = os.path.join(dirname,r.href) + if os.path.isfile(rf): + res.append(rf) + for i in image: + im = os.path.join(dirname,i.fileref) + if os.path.isfile(im): + res.append(im) + page = dict(title=unicode(doc.article.info.title), + menu=unicode(doc.article.info.titleabbrev), + output=os.path.join(dirname, + filename.replace('xml','html')), + exe=exe, + file=xfile, + res=res, + level=level) + for l in sitemap: + if l['link'] == link: + found = 1 + l.update(page) + if not found: + print "adding "+link+" to sitemap" + dd = dict(link=link) + dd.update(page) + sitemap.append(dd) + sfile = open('sitemap.txt','w') + for l in sitemap: + sfile.write(l['link']+'\n') sfile.close() - for f in flist: - sitemap.append(dict(link=f)) - except IOError, what_error: - print 'Sitemap missing - generating one.' - - for dirname, dirnames, filenames in os.walk('.'): - for filename in filenames: - if fnmatch.fnmatch(filename, '*.xml'): - xfile = os.path.join(dirname,filename) - doc = bindery.parse(xfile, - prefixes={u'db': u'http://docbook.org/ns/docbook', - u'xi': u'http://www.w3.org/2001/XInclude', - u'xl': u'http://www.w3.org/1999/xlink'}) - title = doc.xml_select(u'/db:article/db:info/db:title') - menu = doc.xml_select(u'/db:article/db:info/db:titleabbrev') - code = doc.xml_select(u"//xi:include[@parse='text']") - resource = doc.xml_select(u"//db:link[@xl:href]") - image = doc.xml_select(u"//db:imagedata[@fileref]") - exe = 0 - for c in code: - (p, ext) = os.path.splitext(c.href) - if ext in valid_scripts: - exe = 1 - - if title and menu: - found = 0 - base = xfile.split('.')[1] - link = base.replace('index','') - level = len(filter(None,re.split(r'(^/\w*/|\w*/)',link))) - res = [] - for r in resource: - rf = os.path.join(dirname,r.href) - if os.path.isfile(rf): - res.append(rf) - for i in image: - im = os.path.join(dirname,i.fileref) - if os.path.isfile(im): - res.append(im) - page = dict(title=unicode(doc.article.info.title), - menu=unicode(doc.article.info.titleabbrev), - output=os.path.join(dirname, - filename.replace('xml','html')), - exe=exe, - file=xfile, - res=res, - level=level) - for l in sitemap: - if l['link'] == link: - found = 1 - l.update(page) - if not found: - print "adding "+link+" to sitemap" - dd = dict(link=link) - dd.update(page) - sitemap.append(dd) - sfile = open('sitemap.txt','w') - for l in sitemap: - sfile.write(l['link']+'\n') - sfile.close() - return sitemap + return sitemap def expandXincludeTxt(page): - doc = bindery.parse(page['file'], - prefixes={u'db': u'http://docbook.org/ns/docbook', - u'xi': u'http://www.w3.org/2001/XInclude'}) - if page['exe']: - code = doc.xml_select(u"//xi:include[@parse='text']") - for c in code: - (p, ext) = os.path.splitext(c.href) - if ext in valid_scripts: - exe = os.path.join(os.path.abspath(c.href)) - xml = subprocess.Popen([exe],stdout=subprocess.PIPE) - xstr = bindery.parse(str(xml.stdout.read())) - id = c.xml_index_on_parent - for x in xstr.xml_children: - c.xml_parent.xml_insert(id,x) - c.xml_parent.xml_remove(c) - return doc + doc = bindery.parse(page['file'], + prefixes={u'db': u'http://docbook.org/ns/docbook', + u'xi': u'http://www.w3.org/2001/XInclude'}) + if page['exe']: + code = doc.xml_select(u"//xi:include[@parse='text']") + for c in code: + (p, ext) = os.path.splitext(c.href) + if ext in valid_scripts: + exe = os.path.join(os.path.abspath(c.href)) + xml = subprocess.Popen([exe],stdout=subprocess.PIPE) + xstr = bindery.parse(str(xml.stdout.read())) + id = c.xml_index_on_parent + for x in xstr.xml_children: + c.xml_parent.xml_insert(id,x) + c.xml_parent.xml_remove(c) + return doc def xsltConvert(doc): # amara can not handle the docbook stylesheets # xmlarticle = transform(doc,style_xslt) - cwd = os.getcwd() - rundir = os.path.dirname(page['file']) - os.chdir(rundir) - infile = os.path.basename(tempfile.mktemp()) - outfile = tempfile.mktemp() - tfi = open(infile,'w') - tfi.write(doc.xml_encode()) - tfi.close() + cwd = os.getcwd() + rundir = os.path.dirname(page['file']) + os.chdir(rundir) + infile = os.path.basename(tempfile.mktemp()) + outfile = tempfile.mktemp() + tfi = open(infile,'w') + tfi.write(doc.xml_encode()) + tfi.close() # cmd = ["saxon-xslt-xinclude","-o",outfile,infile,style_xslt] - cmd = ["xsltproc","--xinclude","--output",outfile,style_xslt,infile] - retcode = subprocess.call(cmd) - if retcode: - print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']' - tfo = open(outfile,'r') - result = tfo.read() - tfo.close() - os.remove(infile) - os.remove(outfile) - os.chdir(cwd) - return result + cmd = ["xsltproc","--xinclude","--output",outfile,style_xslt,infile] + retcode = subprocess.call(cmd) + if retcode: + print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']' + tfo = open(outfile,'r') + result = tfo.read() + tfo.close() + os.remove(infile) + os.remove(outfile) + os.chdir(cwd) + return result def genMenu(page,sitemap,slevel,elevel): - title = None - sm = [] - if elevel == MAXLEVEL or elevel == 1 or page == None: - html = '