Removed the initial version of tree-cutter.py after refactoring to use objects. v1.0
authorFredrik Unger <fred@tree.se>
Tue, 3 May 2011 20:39:38 +0000 (22:39 +0200)
committerFredrik Unger <fred@tree.se>
Tue, 3 May 2011 20:39:38 +0000 (22:39 +0200)
Added total timings.
The removed part has not been in use for quite some time and this was the final cleanup.

src/tree-cutter.py

index bc4ae7e0db92400f8f33019e7d0f09e33d0b1a19..19ddd87ab32116dbc9596a265961f0dc6d27e66c 100755 (executable)
@@ -406,188 +406,7 @@ class Sitemap():
         publish(args.style+"css", args.output)
         publish(args.style+"images",args.output)
 
-def generateSitemap():
-    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()
-    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
-
-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()
-#  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
-
-def genMenu(page,sitemap,slevel,elevel):
-    title = None
-    sm = []
-    if elevel == MAXLEVEL or elevel == 1 or page == None:
-        html = '<ul>\n'
-        sm = sitemap
-    else:
-        html = '<ul class="tree">\n'
-        idx = sitemap.index(page)
-        while (sitemap[idx]['level'] == page['level']):
-            idx = idx-1
-        title = sitemap[idx]['menu']
-        idx = idx+1
-        while (idx < len(sitemap) and sitemap[idx]['level'] == page['level']):
-            sm.append(sitemap[idx])
-            idx = idx+1
-    oldlevel = slevel
-
-    for p in sm:
-        if slevel > p['level'] or elevel < p['level']:
-            continue
-        if not title and p['link'] == '/':
-            title = p['menu']
-
-        if oldlevel < p['level']:
-            html+='<ul>\n'
-        elif oldlevel > p['level']:
-            if p['link'][-1] == '/':
-                html+='</li>\n'
-            html+='</ul>\n</li>\n'
-        if page != None and page == p:
-            html+='<li class="selected"><a href="%s">%s</a>' % (p['link'],p['menu'])
-        else:
-            html+='<li><a href="%s">%s</a>' % (p['link'],p['menu'])
-        if p['link'][-1] != '/' or p['link'] == '/':
-            html+='</li>\n'
-        oldlevel = p['level']
-    html+='</ul>\n'
-    return (html,title)
-
-def writeToTemplate(page,doc,sitemap):
-    (menu,menuname) = genMenu(page,sitemap,1,MAXLEVEL)
-    (levelmenu,levelname) = genMenu(page,sitemap,page['level'],page['level'])
-    template = Template(file=style_tmpl,
-                        searchList=[{'title':page['title']},
-                                    {'menu':menu},
-                                    {'article':doc},
-                                    {'levelmenu':levelmenu},
-                                    {'levelname':levelname}])
-    outfile = tmptarget+page['output']
-    mkdir_p(os.path.dirname(outfile))
-    out = open(outfile, 'w')
-    out.write(str(template))
-    out.close()
-    for r in page['res']:
-        mkdir_p(os.path.dirname(tmptarget+r))
-        shutil.copyfile(r, tmptarget+r)
-
-def createSitemap(sitemap):
-    (menu,menuname) = genMenu(None,sitemap,1,MAXLEVEL)
-    template = Template(file=style_tmpl,
-                        searchList=[
-            {'title':'Sitemap'},
-            {'menu':menu},
-            {'article':menu},
-            {'levelmenu':''},
-            {'levelname':''}])
-    outfile = tmptarget+'sitemap.en.html'
-    mkdir_p(os.path.dirname(outfile))
-    out = open(outfile, 'w')
-    out.write(str(template))
-    out.close()
-
+ts = time.time()
 dir_ = Directory()
 sitemap = Sitemap()
 
@@ -612,23 +431,4 @@ t1 = time.time()
 sitemap.publish()
 t2 = time.time()
 print "Publish  [%5.2f s]" % (round(t2-t1,2))
-
-sitemap = generateSitemap()
-tmptarget = tempfile.mkdtemp()+'/'
-tot = 0
-for page in sitemap:
-    t1 = time.time()
-    print "Page : %-30s %30s" % (page['link'],
-                        time.ctime(os.stat(page['file']).st_mtime)),
-    doc = expandXincludeTxt(page)
-    pubdoc = xsltConvert(doc)
-    writeToTemplate(page,pubdoc,sitemap)
-    t2 = time.time()
-    print "[%5.2f s]" % (round(t2-t1,2))
-    tot = tot + (t2-t1)
-
-print "Total time\t\t\t\t\t\t\t     [%5.2f s]" % (round(tot,2))
-createSitemap(sitemap)
-publish(tmptarget, args.output)
-publish(args.style+"css", args.output)
-publish(args.style+"images",args.output)
+print "Total    [%5.2f s]" % (round(t2-ts,2))