Refactoring, using const, removing global variables, passing some new arguments.
authorFredrik Unger <fred@tree.se>
Mon, 2 Apr 2012 14:48:17 +0000 (16:48 +0200)
committerFredrik Unger <fred@tree.se>
Mon, 2 Apr 2012 14:48:17 +0000 (16:48 +0200)
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
treecutter/directory.py
treecutter/link.py
treecutter/main.py
treecutter/page.py
treecutter/sitemap.py

index 924dfde9171d9f15b693f6f1b4ee2c4b8e82e6ac..bdbc91113c5d5527868268a55739e1b537ae016f 100644 (file)
@@ -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'}
+
index edbdee1ba4422ec84c59cfff70f1a1cee7360ee7..0c633a784b11f04e158fbfdd723e8cb59168bab5 100644 (file)
@@ -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:
index 45504be77ede1038a2383a32a79743d6619153fd..69ee9b1cfcd58e951c8b35aecbb7fbcf3deacebb 100644 (file)
@@ -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:
index bbde87083269b17ca9e14fff10dfa99a96e25871..ba09eaf801d080d523ade9dcbc640f24d1675f51 100644 (file)
@@ -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))
index 1bd6cd7002d871ad503eec22e1c518e52c00d8ab..ce1296026d63b963c1297f01582a657359f00e25 100644 (file)
@@ -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))
index d87c5c92dbc7d9849fc945937e8ec3872c23040b..7bd81e9a1097da7bb0041c8c56b8cf6e94a4b32c 100644 (file)
@@ -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 += "</ul>"
         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")