python3: converting codebase to run under python3
authorFredrik Unger <fredrik.unger@openchip.com>
Mon, 2 Mar 2026 10:53:11 +0000 (11:53 +0100)
committerFredrik Unger <fredrik.unger@openchip.com>
Mon, 2 Mar 2026 10:53:11 +0000 (11:53 +0100)
Fixed print statements and unicode calls

bin/treecutter
treecutter/directory.py
treecutter/docbook.py
treecutter/image.py
treecutter/main.py
treecutter/page.py
treecutter/sitemap.py

index 5555cc4fa6734a3eaae186f8dc3ba67ff2d9af31..fc08a14bb0c6614c8dc9e14fdd581488960142ff 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 from treecutter import main
 
index 88390bb0ef9039a19f98878d2ecf121842edb51e..c3de285b770f5439659ea00c09a64267c6a13522 100644 (file)
@@ -13,7 +13,7 @@ class Directory():
         self._cwd = u'.'
         self._translations = []
         self._tree = []
-        self._basepath = re.compile('[/\w\._-]*/[\w-]+',re.UNICODE)
+        self._basepath = re.compile(r'[/\w\._-]*/[\w-]+')
 
     def translations(self, directory):
         paths = (self._cwd, directory)
index 9e970c98b679b2d8b81146e99e0e3141fac02d65..1a65873f4a5080732704e7ea3828298ee1dcec33 100644 (file)
@@ -24,7 +24,7 @@ class Docbook():
     def title(self):
         t = self._doc.xpath(u'/db:article/db:info/db:title',namespaces=const.XPATH)
         if t:
-            t = unicode(t[0].text)
+            t = t[0].text
         ta = self._doc.xpath(u'/db:article/db:info/db:titleabbrev',namespaces=const.XPATH)
         if ta:
            ta = unicode(ta[0].text)
@@ -33,19 +33,19 @@ class Docbook():
     def status(self):
         status = self._doc.xpath(u'/db:article[@status]',namespaces=const.XPATH)
         if status:
-            return unicode(status[0].get('status'))
+            return status[0].get('status')
         return None
 
     def role(self):
         art = self._doc.xpath(u'/db:article[@role]',namespaces=const.XPATH)
         if art:
-            return unicode(art[0].get('role'))
+            return art[0].get('role')
         return 'index'
 
     def userlevel(self):
         lvl = self._doc.xpath(u'/db:article[@userlevel]',namespaces=const.XPATH)
         if lvl:
-            lvlstr = unicode(lvl[0].get('userlevel'))
+            lvlstr = lvl[0].get('userlevel')
             return {
                 'Level 1': 1,
                 'Level 2': 2,
@@ -96,14 +96,14 @@ class Docbook():
                         script = resource_filename('xinclude', href)
                         exe.append(script)
                     else:
-                        print "Script "+href+" in "+self._filename+" missing"
+                        print("Script "+href+" in "+self._filename+" missing")
                 if alang:
                     exe.append("lang="+alang)
                 if xpointer:
                     exe.append("xptr="+xpointer)
                 if exe == []:
                     continue
-                print "  executing %15s" % (href),
+                print("  executing %15s" % (href)),
                 ts = time()
                 os.chdir(self._dirname)
                 xml = subprocess.Popen(exe,stdout=subprocess.PIPE,
@@ -116,7 +116,7 @@ class Docbook():
 #                    exit
                 os.chdir(cwd)
                 te = time()
-                print " [%5.2f s]  (%s)" % (round(te-ts,2),xpointer)
+                print(" [%5.2f s]  (%s)" % (round(te-ts,2),xpointer))
                 xstr = etree.fromstring(stdout)
 # inserting the generated code and remove the xinclude reference
                 idp = c.getparent()
@@ -149,7 +149,7 @@ class Docbook():
             if os.path.isfile(im):
                 res.append(im)
             else:
-                print "WARNING: File "+im+" is missing!"
+                print("WARNING: File "+im+" is missing!")
         return res
 
     def collect_videos(self):
@@ -159,7 +159,7 @@ class Docbook():
             if os.path.isfile(im):
                 res.append(im)
             else:
-                print "WARNING: File "+im+" is missing!"
+                print("WARNING: File "+im+" is missing!")
         return res
 
     def collect_forms(self):
index 6136a0d62fcd81a5ad65da48c7db84027ba4f3e7..184d387f82e27994af2a645bf71065fc8be18fe9 100644 (file)
@@ -49,13 +49,13 @@ class Image():
                     cmd = ['exiftool', '-XMP:CreatorTool=treecutter','-quiet','-overwrite_original', outfile]
                     retcode = subprocess.call(cmd)
                     if retcode:
-                        print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
+                        print('Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']')
                         exit
             else:
                 cmd = ['exiftool', '-XMP:CreatorTool=treecutter','-quiet','-overwrite_original', outfile]
                 retcode = subprocess.call(cmd)
                 if retcode:
-                    print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
+                    print('Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']')
                     exit
             xmpfile.close_file()
             self._format[size] = outfile
index c12184d3fd309471f5cecc6345c0ab6fed6c0610..b126c1d60c4b5ecde089f9c060f448889f65e715 100644 (file)
@@ -21,15 +21,15 @@ def main():
     args = parser.parse_args()
 
     ts = time()
-    print "--= Treecutter =--"
+    print("--= Treecutter =--")
     dir_ = Directory()
     t1 = time()
     totrans = dir_.translations(args.style)
-    print "Translate [%d] : [" % (len(totrans)),
+    print("Translate [%d] : [" % (len(totrans))),
     translate(totrans)
-    print "]"
+    print("]")
     t2 = time()
-    print "Translate[%5.2f s]" % (round(t2-t1,2))
+    print("Translate[%5.2f s]" % (round(t2-t1,2)))
 
 
     sitemap = Sitemap(args)
@@ -44,12 +44,12 @@ def main():
     missing = dirset - sitemap.set()
     removed = sitemap.set() - dirset
     for page in removed:
-        print page+' page not availible in this config'
+        print(page+' page not availible in this config')
     for page in missing:
-        print 'adding missing page '+page
+        print('adding missing page '+page)
         sitemap.add_link(page)
     if len(missing) != 0:
-        print 'writing new sitemap - please adjust if needed'
+        print('writing new sitemap - please adjust if needed')
         sitemap.write_map()
 
     dirsitemap = Sitemap(args)
@@ -67,8 +67,8 @@ def main():
     t1 = time()
     dirsitemap.publish()
     t2 = time()
-    print "Publish  [%5.2f s]" % (round(t2-t1,2))
-    print "Total    [%5.2f s]" % (round(t2-ts,2))
+    print("Publish  [%5.2f s]" % (round(t2-t1,2)))
+    print("Total    [%5.2f s]" % (round(t2-ts,2)))
     return 0
 
 if __name__ == "__main__":
index c05ef90b4573efd07aeac697e5f3b44770dbd659..fe1dc5eb646c89bbbecf2a9b90225d4a3b821ec1 100644 (file)
@@ -74,12 +74,12 @@ class Page():
                         'langmen':langmenu,
                         'subdir':subdir}
 
-        s = unicode(style, "utf-8")
-        t = s+self._template+'.'+self._lang+'.xhtml'
+        s = style
+        t = s+self._template+'.'+self._lang+'.html.tmpl'
         template = templateEnv.get_template( t )
         templateout = template.render( templateVars )
 
-        outfile = tdir+'xhtml'.join(self._file.rsplit('xml',1))
+        outfile = tdir+'html'.join(self._file.rsplit('xml',1))
         mkdir_p(os.path.dirname(outfile))
         out = codecs.open(outfile, 'w', 'utf-8')
         out.write(templateout)
index b6d0fecf3630ce1c1fcb6310e6c9664d2c6a2f24..d645b1875244e0677b72b83a12917350821b0f68 100644 (file)
@@ -33,7 +33,7 @@ class Sitemap():
     # representing the text on the site.
     # A link can have several pages in different languages.
     def add_link(self, link):
-        tokens = filter(None,re.split(r'(^/[\w\.:-]*$|^/[\w\.:-]*/|[\w\.:-]*/)',link,flags=re.UNICODE))
+        tokens = list(filter(None,re.split(r'(^/[\w\.:-]*$|^/[\w\.:-]*/|[\w\.:-]*/)',link,flags=re.UNICODE)))
         self._tree.add(tokens,Link(link))
 
     def write_map(self):
@@ -49,8 +49,8 @@ class Sitemap():
             f.close()
             for line in sml:
                 self.add_link(line)
-        except IOError, what_error:
-            print 'INFO: Could not read sitemap.txt - one will be created'
+        except (IOError):
+            print('INFO: Could not read sitemap.txt - one will be created')
 
     # Create a set of the current tree for comparison with the
     # directory scan
@@ -67,7 +67,7 @@ class Sitemap():
         for link in self._tree:
             link.prepare()
         t2 = time()
-        print "Prepare  [%5.2f s]" % (round(t2-t1,2))
+        print("Prepare  [%5.2f s]" % (round(t2-t1,2)))
         for link in self._tree:
             self._sitelang = self._sitelang.union(set(link.languages()))
         for tran in self._sitelang:
@@ -75,17 +75,17 @@ class Sitemap():
                 self._tranlang[tran] = gettext.translation('iso_639_3',
                                                            languages=[tran])
         t3 = time()
-        print "Language [%5.2f s]" % (round(t3-t2,2))
+        print("Language [%5.2f s]" % (round(t3-t2,2)))
         transform = {}
         transform['xhtml5'] = etree.XSLT(etree.parse(self._style+"docbook.xhtml5.xsl"))
         for link in self._tree:
             link.render(transform)
         t4 = time()
-        print "Render   [%5.2f s]" % (round(t4-t3,2))
+        print("Render   [%5.2f s]" % (round(t4-t3,2)))
         for link in self._tree:
             link.template(self, self._style, self._tmptarget,self._subdir)
         t5 = time()
-        print "Template [%5.2f s]" % (round(t5-t4,2))
+        print("Template [%5.2f s]" % (round(t5-t4,2)))
         t6 = time()
         res = set()
         # Collect all files used by the documents
@@ -95,7 +95,7 @@ class Sitemap():
             outfile = self._tmptarget+f
             mkdir_p(os.path.dirname(outfile))
             shutil.copyfile(f,outfile)
-        print "Resources[%5.2f s]" % (round(t6-t5,2))
+        print( "Resources[%5.2f s]" % (round(t6-t5,2)))
         # TODO: Improve the sitemap, it is a page that is generated from
         #       the ground up and added a bit adhoc.
         sitmaplink = Link('/sitemap')
@@ -109,7 +109,7 @@ class Sitemap():
             f.close()
             sitmaplink.page(l).template(self,self._style,self._tmptarget,self._subdir)
         t7 = time()
-        print "Sitemap  [%5.2f s]" % (round(t7-t6,2))
+        print("Sitemap  [%5.2f s]" % (round(t7-t6,2)))
 
     def graph(self):
         self._tree.graph()
@@ -125,7 +125,7 @@ class Sitemap():
             ln = self._isocode.xpath(isoxml)[0].get('name')
             if lang != 'en':
                 ln = self._tranlang[lang].ugettext(ln)
-            p = unicode(link.link())
+            p = link.link()
             if p[-1] == u'/':
                 p = p +u'index'
             p = p+u'.'+l
@@ -136,11 +136,11 @@ class Sitemap():
         return etree.tostring(menu,encoding='unicode',pretty_print=False)
 
     def publish(self):
-        print "Size [ %7s ]" % (sizeof_fmt(get_folder_size(self._tmptarget)))
+        print("Size [ %7s ]" % (sizeof_fmt(get_folder_size(self._tmptarget))))
         ssh_cmd(self._output,"mkdir -p")
         publish(self._tmptarget, self._output)
         for res in ["stylesheets","images","js","fonts","favicon.ico"]:
             if (os.path.exists(self._style+res)):
-                print "Size [ %7s ]" % (sizeof_fmt(get_folder_size(self._style+res)))
+                print("Size [ %7s ]" % (sizeof_fmt(get_folder_size(self._style+res))))
                 publish(self._style+res, self._output)
         ssh_cmd(self._output,"chmod a+rx")