From ea0edb8d3e6b9100505f874d0bdd2f213a3b6689 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 23 May 2019 22:37:01 +0200 Subject: [PATCH] docs: use print function for compatibility with Python 3 Another quite visible change with Python 3 is that the old `print` statement has been replaced with a function. To ease migration to the newer way, Python 2.7 provides a compatibility module "print_function". Convert all code in docs to use the new function syntax. --- docs/cpp2markdown-1.py | 7 +++++-- docs/cpp2markdown.py | 6 ++++-- docs/make-doc.py | 24 ++++++++++++------------ docs/zzipdoc/docbookdocument.py | 27 +++++++++++++++------------ docs/zzipdoc/functionlisthtmlpage.py | 6 ++++-- docs/zzipdoc/functionlistreference.py | 4 +++- docs/zzipdoc/htm2dbk.py | 6 ++++-- docs/zzipdoc/htmldocument.py | 20 +++++++++++--------- docs/zzipdoc/match.py | 12 +++++++----- docs/zzipdoc/textfileheader.py | 4 +++- 10 files changed, 68 insertions(+), 48 deletions(-) diff --git a/docs/cpp2markdown-1.py b/docs/cpp2markdown-1.py index 60d28c4..317e9f8 100755 --- a/docs/cpp2markdown-1.py +++ b/docs/cpp2markdown-1.py @@ -1,4 +1,7 @@ #! /usr/bin/env python + +from __future__ import print_function + import pygments.lexers.compiled as lexer import optparse import re @@ -108,7 +111,7 @@ class CppToMarkdown: def run(self, filename): filetext = open(filename).read() for line in self.process(filetext, filename): - print line + print(line) def process(self, filetext, filename=""): section_ruler = "-----------------------------------------" copyright = "" @@ -136,7 +139,7 @@ class CppToMarkdown: else: if text: yield "#### NOTES" - print token, text.replace("\n", "\n ") + print(token + " " + text.replace("\n", "\n ")) if copyright: yield section_ruler yield "### COPYRIGHT" diff --git a/docs/cpp2markdown.py b/docs/cpp2markdown.py index 710bbdc..749de35 100644 --- a/docs/cpp2markdown.py +++ b/docs/cpp2markdown.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import pygments.lexers.compiled as lexer import optparse import re @@ -62,7 +64,7 @@ class CppToMarkdown: def run(self, filename): filetext = open(filename).read() for line in self.process(filetext, filename): - print line + print(line) def process(self, filetext, filename=""): for token, text in self.parse(filetext): if token == FileInclude: @@ -86,7 +88,7 @@ class CppToMarkdown: else: if text: yield "#### NOTES" - print token, text.replace("\n", "\n ") + print(token + " " + text.replace("\n", "\n ")) def isexported_function(self): function = self.function_text.strip().replace("\n"," ") logg.debug("@ --------------------------------------") diff --git a/docs/make-doc.py b/docs/make-doc.py index 1f8ff0c..50e579f 100644 --- a/docs/make-doc.py +++ b/docs/make-doc.py @@ -1,5 +1,8 @@ #! /usr/bin/python # -*- coding: UTF-8 -*- + +from __future__ import print_function + import sys import re import string @@ -376,7 +379,7 @@ def examine_head_anchors(func_list): function.head = s(function.head, r"(.*)also:(.*)", lambda x : set_seealso(function, x.group(2)) and x.group(1)) if function.seealso and None: - print "function[",function.name,"].seealso=",function.seealso + print("function[" + function.name + "].seealso=" + function.seealso) examine_head_anchors(function_list) # =============================================================== HTML ===== @@ -528,7 +531,7 @@ except IOError as error: warn(# ............. open(o.libhtmlfile, "w") .............. "can not open html output file: "+o.libhtmlfile, error) else: - print >> F, html.page_text() + print(html.page_text(), file=F) F.close() #fi @@ -990,19 +993,18 @@ try: except IOError as error: warn("can not open docbook output file: "+o.docbookfile, error) else: - print >> F, doctype, 'Manual Pages' + print(doctype + 'Manual Pages', file=F) for page in combined_pages: - print >> F, page.refentry_text() + print(page.refentry_text(), file=F) #od for page in header_refpages.values(): if not page.refentry: continue - print >> F, "\n", - print >> F, page.refentry_text() + print("\n" + page.refentry_text(), file=F) #od - print >> F, "\n",'',"\n" + print("\n\n", file=F) F.close() #fi @@ -1014,13 +1016,11 @@ except IOError as error: else: for func in function_list: name = func.name - print >> F, ""+"\n" + print(""+"\n", file=F) for H in sorted_keys(func.dict()): - print >> F, "<"+H+" name=\""+name+"\">", - print >> F, str(func.dict()[H]), - print >> F, "" + print("<"+H+" name=\""+name+"\">" + str(func.dict()[H]) + "", file=F) #od - print >> F, "\n\n"; + print("\n\n", file=F) #od F.close(); #fi diff --git a/docs/zzipdoc/docbookdocument.py b/docs/zzipdoc/docbookdocument.py index 906e7a9..d9a50fa 100644 --- a/docs/zzipdoc/docbookdocument.py +++ b/docs/zzipdoc/docbookdocument.py @@ -1,5 +1,8 @@ #! /usr/bin/env python # -*- coding: UTF-8 -*- + +from __future__ import print_function + from zzipdoc.match import Match class DocbookDocument: @@ -30,7 +33,7 @@ class DocbookDocument: def _xml_text(self, xml): """ accepts adapter objects with .xml_text() """ try: return xml.xml_text() - except Exception as e: print "DocbookDocument/text", e; pass + except Exception as e: print("DocbookDocument/text" + str(e)); pass return str(xml) def _fetch_rootnode(self, text): fetch = Match(r"^[^<>]*<(\w+)\b") @@ -47,7 +50,7 @@ class DocbookDocument: return filename def save(self, filename = None): filename = self._filename(filename) - print "writing '"+filename+"'" + print("writing '"+filename+"'") if len(self.text) > 1: self.save_all(filename) else: @@ -58,12 +61,12 @@ class DocbookDocument: xml_text = self._xml_text(text) rootnode = self._fetch_rootnode(xml_text) doctype = self._xml_doctype(rootnode) - print >>fd, doctype - print >>fd, xml_text + print(doctype, file=fd) + print(xml_text, file=fd) fd.close() return True except IOError as e: - print "could not open '"+filename+"'file", e + print("could not open '"+filename+"'file" + str(e)) return False def save_all(self, filename): assert len(self.text) > 1 @@ -76,20 +79,20 @@ class DocbookDocument: else: rootnode = self.rootnode doctype = self._xml_doctype(rootnode) - print >>fd, doctype + print(doctype, file=fd) title = self.get_title() if title and self.rootnode in self.has_title_child: - print >>fd, "<"+self.rootnode+'>'+title+'' + print("<"+self.rootnode+'>'+title+'', file=fd) elif title: - print >>fd, "<"+self.rootnode+' id="'+title+'">' + print("<"+self.rootnode+' id="'+title+'">', file=fd) else: - print >>fd, "<"+self.rootnode+'>' + print("<"+self.rootnode+'>', file=fd) for text in self.text: text = self._xml_text(text) - print >>fd, text - print >>fd, "" + print(text) + print("", file=fd) fd.close() return True except IOError as e: - print "could not open '"+filename+"'file", e + print("could not open '"+filename+"'file" + str(e)) return False diff --git a/docs/zzipdoc/functionlisthtmlpage.py b/docs/zzipdoc/functionlisthtmlpage.py index ec3cdaf..acabdd4 100644 --- a/docs/zzipdoc/functionlisthtmlpage.py +++ b/docs/zzipdoc/functionlisthtmlpage.py @@ -1,3 +1,5 @@ +from __future__ import print_function + from zzipdoc.options import * from zzipdoc.match import Match @@ -35,7 +37,7 @@ class FunctionListHtmlPage: head_text = entry.head_xml_text() body_text = entry.body_xml_text(name) if not head_text: - print "no head_text for", name + print("no head_text for " + name) return try: prespec = entry.head_get_prespec() @@ -102,7 +104,7 @@ class FunctionListHtmlPage: text &= (Match("(?s)(\w+)") >> (lambda x: self.resolve_internal(x.group(1)))) if len(self.not_found_in_anchors): - print "not found in anchors: ", self.not_found_in_anchors + print("not found in anchors: {}".format(self.not_found_in_anchors)) return (text & Match("(?s)([^<>]*)") >> "\\1") def resolve_external(self, func, sect): diff --git a/docs/zzipdoc/functionlistreference.py b/docs/zzipdoc/functionlistreference.py index 5053110..7b75603 100644 --- a/docs/zzipdoc/functionlistreference.py +++ b/docs/zzipdoc/functionlistreference.py @@ -1,5 +1,7 @@ #! /usr/bin/env python # -*- coding: UTF-8 -*- +from __future__ import print_function + from zzipdoc.match import Match from zzipdoc.htm2dbk import * @@ -19,7 +21,7 @@ class FunctionListReference: description = entry.body_xml_text(name) funcsynopsis = entry.head_xml_text() if not funcsynopsis: - print "no funcsynopsis for", name + print("no funcsynopsis for " + name) return if self.entry is None: self.entry = FunctionListRefEntry(entry, self.o) diff --git a/docs/zzipdoc/htm2dbk.py b/docs/zzipdoc/htm2dbk.py index 15f6f06..0a6d743 100644 --- a/docs/zzipdoc/htm2dbk.py +++ b/docs/zzipdoc/htm2dbk.py @@ -6,6 +6,8 @@ The mapping of markups and links is far from perfect. But all we want is the docbook-to-pdf converter and similar technology being present in the world of docbook-to-anything converters. """ +from __future__ import print_function + from datetime import date from zzipdoc.match import Match import sys @@ -147,7 +149,7 @@ def htm2dbk_files(args): doc.add(f.read()) f.close() except IOError: - print >> sys.stderr, "can not open "+filename + print("can not open "+filename, file=sys.stderr) return doc.value() def html2docbook(text): @@ -161,7 +163,7 @@ if __name__ == "__main__": opt, args = cmdline.parse_args() result = htm2dbk_files(args) if not opt.into: - print result + print(result) else: f = open(opt.into, "w") f.write(result) diff --git a/docs/zzipdoc/htmldocument.py b/docs/zzipdoc/htmldocument.py index 3f6b824..ca6141d 100644 --- a/docs/zzipdoc/htmldocument.py +++ b/docs/zzipdoc/htmldocument.py @@ -1,5 +1,7 @@ #! /usr/bin/env python # -*- coding: UTF-8 -*- +from __future__ import print_function + from zzipdoc.match import Match class HtmlDocument: @@ -42,18 +44,18 @@ class HtmlDocument: try: return style.html_style() except Exception as e: ee = e; pass try: return style.xml_style() - except Exception as e: print "HtmlDocument/style", ee, e; pass + except Exception as e: print("HtmlDocument/style {} {}".format(ee, e)); pass try: return str(style) - except Exception as e: print "HtmlDocument/style", e; return "" + except Exception as e: print("HtmlDocument/style {}".format(e)); return "" def _html_text(self, html): """ accepts adapter objects with .html_text() and .xml_text() """ ee = None try: return html.html_text() except Exception as e: ee = e; pass try: return html.xml_text() - except Exception as e: print "HtmlDocument/text", ee, e; pass + except Exception as e: print("HtmlDocument/text {} {}".format(ee, e)); pass try: return str(html) - except Exception as e: print "HtmlDocument/text", e; return " " + except Exception as e: print("HtmlDocument/text {}".format(e)); return " " def navigation(self): if self.navi: return self.navi @@ -103,15 +105,15 @@ class HtmlDocument: return filename def save(self, filename = None): filename = self._filename(filename) - print "writing '"+filename+"'" + print("writing '"+filename+"'") try: fd = open(filename, "w") - print >>fd, self.html_header() + print(self.html_header(), file=fd) for text in self.text: - print >>fd, self._html_text(text) - print >>fd, self.html_footer() + print(self._html_text(text), file=fd) + print(self.html_footer(), file=fd) fd.close() return True except IOError as e: - print "could not open '"+filename+"'file", e + print("could not open '"+filename+"'file {}".format(e)) return False diff --git a/docs/zzipdoc/match.py b/docs/zzipdoc/match.py index 47582a1..d1b2b0d 100644 --- a/docs/zzipdoc/match.py +++ b/docs/zzipdoc/match.py @@ -3,6 +3,8 @@ # @creator (C) 2003 Guido U. Draheim # @license http://creativecommons.org/licenses/by-nc-sa/2.0/de/ +from __future__ import print_function + import re try: @@ -95,14 +97,14 @@ class Match(str): if __name__ == "__main__": # matching: if "foo" & Match("oo"): - print "oo" + print("oo") x = Match() if "foo" & x("(o+)"): - print x[1] + print(x[1]) # replacing: y = "fooboo" & Match("oo") >> "ee" - print y + print(y) r = Match("oo") >> "ee" - print "fooboo" & r + print("fooboo" & r) s = MatchReplace("oo", "ee") - print "fooboo" & s + print("fooboo" & s) diff --git a/docs/zzipdoc/textfileheader.py b/docs/zzipdoc/textfileheader.py index c737243..c38c4be 100644 --- a/docs/zzipdoc/textfileheader.py +++ b/docs/zzipdoc/textfileheader.py @@ -1,3 +1,5 @@ +from __future__ import print_function + from zzipdoc.match import Match class TextFileHeader: @@ -17,7 +19,7 @@ class TextFileHeader: x = Match() text = self.textfile.get_src_text() if not text: - print "nonexistent file:", self.textfile.get_filename() + print("nonexistent file: " + self.textfile.get_filename()) return False if text & x(r"(?s)[/][*]+(\s(?:.(?!\*\/))*.)\*\/" r"(?:\s*\#(?:define|ifdef|endif)[ ]*\S*[ ]*\S*)*" -- 2.40.0