From f53c2f69c2e8300a144004d31654846120a913a6 Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Mon, 2 Apr 2012 16:48:17 +0200 Subject: [PATCH] Refactoring, using const, removing global variables, passing some new arguments. This phase was to clean up the use of output and style in several places. Now passed as arguments to the sections where it is needed. Tempfile creation also localized. --- treecutter/const.py | 1 + treecutter/directory.py | 2 +- treecutter/link.py | 8 ++++---- treecutter/main.py | 11 ++--------- treecutter/page.py | 14 +++++++------- treecutter/sitemap.py | 23 ++++++++++++----------- 6 files changed, 27 insertions(+), 32 deletions(-) diff --git a/treecutter/const.py b/treecutter/const.py index 924dfde..bdbc911 100644 --- a/treecutter/const.py +++ b/treecutter/const.py @@ -5,3 +5,4 @@ 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', u'html' : u'http://www.w3.org/1999/xhtml'} + diff --git a/treecutter/directory.py b/treecutter/directory.py index edbdee1..0c633a7 100644 --- a/treecutter/directory.py +++ b/treecutter/directory.py @@ -15,7 +15,7 @@ class Directory(): for filename in filenames: if fnmatch.fnmatch(filename, '*.xml'): file_ = os.path.join(dirname,filename) - doc = bindery.parse(file_, prefixes=PREFIXES) + doc = bindery.parse(file_, prefixes=const.PREFIXES) title = doc.xml_select(u'/db:article/db:info/db:title') menu = doc.xml_select(u'/db:article/db:info/db:titleabbrev') if title and menu: diff --git a/treecutter/link.py b/treecutter/link.py index 45504be..69ee9b1 100644 --- a/treecutter/link.py +++ b/treecutter/link.py @@ -39,13 +39,13 @@ class Link(): p.append(page.language()) return p - def render(self): + def render(self, style): for page in self._pages: - page.render() + page.render(style) - def template(self,sitemap): + def template(self,sitemap,style,tdir): for page in self._pages: - page.template(sitemap) + page.template(sitemap,style,tdir) def page(self,lang): for page in self._pages: diff --git a/treecutter/main.py b/treecutter/main.py index bbde870..ba09eaf 100644 --- a/treecutter/main.py +++ b/treecutter/main.py @@ -12,13 +12,6 @@ parser.add_argument('--output', nargs='?', default=os.path.dirname(os.getcwd())+'/htdocs/') args = parser.parse_args() -style_xslt = args.style+"docbook.xsl" -outputdir = args.output - -tmptarget = tempfile.mkdtemp()+'/' - -MAXLEVEL = 10000 - ts = time.time() dir_ = Directory() sitemap = Sitemap() @@ -38,10 +31,10 @@ if len(missing)+len(removed) != 0: sitemap.write_map() sitemap.graph() -sitemap.process() +sitemap.process(args.style) t1 = time.time() -sitemap.publish() +sitemap.publish(args.output,args.style) t2 = time.time() print "Publish [%5.2f s]" % (round(t2-t1,2)) print "Total [%5.2f s]" % (round(t2-ts,2)) diff --git a/treecutter/page.py b/treecutter/page.py index 1bd6cd7..ce12960 100644 --- a/treecutter/page.py +++ b/treecutter/page.py @@ -33,7 +33,7 @@ class Page(): self._rendered_article = art def prepare(self): - self._doc = bindery.parse(self._file, prefixes=PREFIXES) + self._doc = bindery.parse(self._file, prefixes=const.PREFIXES) if self._doc.xml_select(u'/db:article/db:info/db:title'): self._title = unicode(self._doc.article.info.title) if self._doc.xml_select(u'/db:article/db:info/db:titleabbrev'): @@ -44,7 +44,7 @@ class Page(): if code: for c in code: (p, ext) = os.path.splitext(c.href) - if ext in valid_scripts: + if ext in const.valid_scripts: exe = [] exe.append(os.path.join(os.path.abspath(dirname)+'/'+c.href)) if c.xml_select(u"//xi:include[@accept-language]"): @@ -73,7 +73,7 @@ class Page(): if os.path.isfile(im): self._resources.append(im) - def render(self): + def render(self, style): # amara can not handle the docbook stylesheets # xmlarticle = transform(doc,style_xslt) cwd = os.getcwd() @@ -85,7 +85,7 @@ class Page(): tfi.write(self._doc.xml_encode(omit_xml_declaration=True)) tfi.close() # cmd = ["saxon-xslt-xinclude","-o",outfile,infile,style_xslt] - cmd = ["xsltproc","--xinclude","--output",outfile,style_xslt,infile] + cmd = ["xsltproc","--xinclude","--output",outfile,style+"docbook.xsl",infile] retcode = subprocess.call(cmd) if retcode: print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']' @@ -96,17 +96,17 @@ class Page(): os.remove(outfile) os.chdir(cwd) - def template(self,sitemap): + def template(self,sitemap,style,tdir): htmlmenu = sitemap.gen_menu(self._lang,None,"menu") levelmenu = sitemap.gen_menu(self._lang,self,"tree") langmenu = sitemap.lang_menu(self._lang,self._link) - template = Template(file=args.style+'index.'+self._lang+'.html.tmpl', + template = Template(file=style+'index.'+self._lang+'.html.tmpl', searchList=[{'title':self._title}, {'menu':htmlmenu}, {'article':self._rendered_article}, {'levelmenu':levelmenu}, {'langmenu':langmenu}]) - outfile = tmptarget+'html'.join(self._file.rsplit('xml',1)) + outfile = tdir+'html'.join(self._file.rsplit('xml',1)) mkdir_p(os.path.dirname(outfile)) out = open(outfile, 'w') out.write(str(template)) diff --git a/treecutter/sitemap.py b/treecutter/sitemap.py index d87c5c9..7bd81e9 100644 --- a/treecutter/sitemap.py +++ b/treecutter/sitemap.py @@ -18,6 +18,7 @@ class Sitemap(): self._sitelang = set() self._isocode = bindery.parse('/usr/share/xml/iso-codes/iso_639_3.xml') self._tranlang = {} + self._tmptarget = tempfile.mkdtemp()+'/' def add_link(self, link): tokens = filter(None,re.split(r'(^/[\w-]*/|[\w-]*/)',link)) @@ -41,7 +42,7 @@ class Sitemap(): def set(self): return set(link.link() for link in self._tree) - def process(self): + def process(self, style): t1 = time.time() for link in self._tree: link.prepare() @@ -56,11 +57,11 @@ class Sitemap(): t3 = time.time() print "Language [%5.2f s]" % (round(t3-t2,2)) for link in self._tree: - link.render() + link.render(style) t4 = time.time() print "Render [%5.2f s]" % (round(t4-t3,2)) for link in self._tree: - link.template(self) + link.template(self, style, self._tmptarget) t5 = time.time() print "Template [%5.2f s]" % (round(t5-t4,2)) t6 = time.time() @@ -69,7 +70,7 @@ class Sitemap(): for link in self._tree: res = res.union(link.resources()) for f in res: - outfile = tmptarget+f + outfile = self._tmptarget+f mkdir_p(os.path.dirname(outfile)) shutil.copyfile(f,outfile) print "Resources[%5.2f s]" % (round(t6-t5,2)) @@ -78,7 +79,7 @@ class Sitemap(): sitmaplink.add_page((l,'/sitemap.'+l+'.xml')) for l in self._sitelang: sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap")) - sitmaplink.page(l).template(self) + sitmaplink.page(l).template(self,style,self._tmptarget) t7 = time.time() print "Sitemap [%5.2f s]" % (round(t7-t6,2)) @@ -103,10 +104,10 @@ class Sitemap(): html += "" return html - def publish(self): - ssh_cmd(args.output,"mkdir -p") - publish(tmptarget, args.output) + def publish(self,output,style): + ssh_cmd(output,"mkdir -p") + publish(self._tmptarget, output) for res in ["css","images","js","favicon.ico"]: - if (os.path.exists(args.style+res)): - publish(args.style+res, args.output) - ssh_cmd(args.output,"chmod a+rx") + if (os.path.exists(style+res)): + publish(style+res, output) + ssh_cmd(output,"chmod a+rx") -- 2.30.2