Commit 6212c2d2 authored by Petr Prikryl's avatar Petr Prikryl

doc/translator.py unified for Python 2.6+ and Python 3.0+

- Open() replaced by xopen() that internally uses the encoding argument
  only for Python 3. The earlier usage of the codecs module and of the
  prefixed unicode string literals was removed.
- Some lists of prototypes were sorted to get the same translator report
  for different versions of Python.
- The local dedent() definition was replaced by textwrap.dedent().
  (The older versions of Python did not have the module.)
- Tested on Windows for Python 2.6.0, 2.7.7, 3.0.1, and 3.4.1.
parent 962ad74f
...@@ -64,8 +64,11 @@ ...@@ -64,8 +64,11 @@
2013/06/25 - TranslatorDecoder checks removed after removing the class. 2013/06/25 - TranslatorDecoder checks removed after removing the class.
2013/09/04 - Coloured status in langhowto. *ALMOST up-to-date* category 2013/09/04 - Coloured status in langhowto. *ALMOST up-to-date* category
of translators introduced. of translators introduced.
2014/06/16 - unified for Python 2.6+ and 3.0+
""" """
from __future__ import print_function
import os import os
import platform import platform
import re import re
...@@ -74,23 +77,17 @@ import textwrap ...@@ -74,23 +77,17 @@ import textwrap
def xopen(fname, mode='r', encoding='utf-8-sig'): def xopen(fname, mode='r', encoding='utf-8-sig'):
'''Unified open of text files with UTF-8 default encoding. '''Unified file opening for Python 2 an Python 3.
The 'utf-8-sig' skips the BOM automatically. Python 2 does not have the encoding argument. Python 3 has one, and
the default 'utf-8-sig' is used (skips the BOM automatically).
''' '''
# Use UTF-8 without BOM when writing to a text file.
if encoding == 'utf-8-sig' and mode == 'w':
encoding = 'utf-8'
major, minor, patch = (int(e) for e in platform.python_version_tuple()) major, minor, patch = (int(e) for e in platform.python_version_tuple())
if major == 2: if major == 2:
if mode == 'w': return open(fname, mode=mode) # Python 2 without encoding
mode = 'wU'
import codecs
return codecs.open(fname, mode=mode, encoding=encoding) # Python 2
else: else:
return open(fname, mode=mode, encoding=encoding) # Python 3 return open(fname, mode=mode, encoding=encoding) # Python 3 with encoding
def fill(s): def fill(s):
...@@ -1074,6 +1071,7 @@ class Transl: ...@@ -1074,6 +1071,7 @@ class Transl:
for p in myDic: for p in myDic:
if p not in reqDic: if p not in reqDic:
self.obsoleteMethods.append(p) self.obsoleteMethods.append(p)
self.obsoleteMethods.sort()
# Build the list of missing methods and the list of implemented # Build the list of missing methods and the list of implemented
# required methods. # required methods.
...@@ -1084,6 +1082,8 @@ class Transl: ...@@ -1084,6 +1082,8 @@ class Transl:
self.implementedMethods.append(p) self.implementedMethods.append(p)
else: else:
self.missingMethods.append(p) self.missingMethods.append(p)
self.missingMethods.sort()
self.implementedMethods.sort()
# Check whether adapter must be used or suggest the newest one. # Check whether adapter must be used or suggest the newest one.
# Change the status and set the note accordingly. # Change the status and set the note accordingly.
...@@ -1788,7 +1788,7 @@ class TrManager: ...@@ -1788,7 +1788,7 @@ class TrManager:
doctpl = f.read() doctpl = f.read()
f.close() f.close()
pos = doctpl.find(u'/***') pos = doctpl.find('/***')
assert pos != -1 assert pos != -1
doctpl = doctpl[pos:] doctpl = doctpl[pos:]
...@@ -1796,21 +1796,21 @@ class TrManager: ...@@ -1796,21 +1796,21 @@ class TrManager:
# document template. # document template.
tplDic = {} tplDic = {}
s = u'Do not edit this file. It was generated by the %s script. * Instead edit %s and %s' % (self.script_name, self.languageTplFileName, self.maintainersFileName) s = 'Do not edit this file. It was generated by the %s script. * Instead edit %s and %s' % (self.script_name, self.languageTplFileName, self.maintainersFileName)
tplDic['editnote'] = s tplDic['editnote'] = s
tplDic['doxVersion'] = self.doxVersion tplDic['doxVersion'] = self.doxVersion
tplDic['supportedLangReadableStr'] = self.supportedLangReadableStr tplDic['supportedLangReadableStr'] = self.supportedLangReadableStr
tplDic['translatorReportFileName'] = self.translatorReportFileName tplDic['translatorReportFileName'] = self.translatorReportFileName
ahref = u'<a href="../doc/' + self.translatorReportFileName ahref = '<a href="../doc/' + self.translatorReportFileName
ahref += u'"\n><code>doxygen/doc/' + self.translatorReportFileName ahref += '"\n><code>doxygen/doc/' + self.translatorReportFileName
ahref += u'</code></a>' ahref += '</code></a>'
tplDic['translatorReportLink'] = ahref tplDic['translatorReportLink'] = ahref
tplDic['numLangStr'] = str(self.numLang) tplDic['numLangStr'] = str(self.numLang)
# Define templates for HTML table parts of the documentation. # Define templates for HTML table parts of the documentation.
htmlTableTpl = u'''\ htmlTableTpl = '''\
\\htmlonly \\htmlonly
<table align="center" cellspacing="0" cellpadding="0" border="0"> <table align="center" cellspacing="0" cellpadding="0" border="0">
<tr bgcolor="#000000"> <tr bgcolor="#000000">
...@@ -1833,9 +1833,9 @@ class TrManager: ...@@ -1833,9 +1833,9 @@ class TrManager:
\\endhtmlonly \\endhtmlonly
''' '''
htmlTableTpl = textwrap.dedent(htmlTableTpl) htmlTableTpl = textwrap.dedent(htmlTableTpl)
htmlTrTpl = u'\n <tr bgcolor="#ffffff">%s\n </tr>' htmlTrTpl = '\n <tr bgcolor="#ffffff">%s\n </tr>'
htmlTdTpl = u'\n <td>%s</td>' htmlTdTpl = '\n <td>%s</td>'
htmlTdStatusColorTpl = u'\n <td bgcolor="%s">%s</td>' htmlTdStatusColorTpl = '\n <td bgcolor="%s">%s</td>'
# Loop through transl objects in the order of sorted readable names # Loop through transl objects in the order of sorted readable names
# and add generate the content of the HTML table. # and add generate the content of the HTML table.
...@@ -1848,7 +1848,7 @@ class TrManager: ...@@ -1848,7 +1848,7 @@ class TrManager:
if obj.readableStatus.startswith('1.4'): if obj.readableStatus.startswith('1.4'):
bkcolor = self.getBgcolorByReadableStatus('1.4') bkcolor = self.getBgcolorByReadableStatus('1.4')
else: else:
bkcolor = u'#ffffff' bkcolor = '#ffffff'
lst = [ htmlTdStatusColorTpl % (bkcolor, obj.langReadable) ] lst = [ htmlTdStatusColorTpl % (bkcolor, obj.langReadable) ]
...@@ -1863,8 +1863,8 @@ class TrManager: ...@@ -1863,8 +1863,8 @@ class TrManager:
classId = obj.classId[:-2] classId = obj.classId[:-2]
if classId in self.__translDic: if classId in self.__translDic:
lang = self.__translDic[classId].langReadable lang = self.__translDic[classId].langReadable
mm = u'see the %s language' % lang mm = 'see the %s language' % lang
ee = u'&nbsp;' ee = '&nbsp;'
if not mm and obj.classId in self.__maintainersDic: if not mm and obj.classId in self.__maintainersDic:
# Build a string of names separated by the HTML break element. # Build a string of names separated by the HTML break element.
...@@ -1873,24 +1873,24 @@ class TrManager: ...@@ -1873,24 +1873,24 @@ class TrManager:
for maintainer in self.__maintainersDic[obj.classId]: for maintainer in self.__maintainersDic[obj.classId]:
name = maintainer[0] name = maintainer[0]
if name.startswith('--'): if name.startswith('--'):
name = u'<span style="color: red; background-color: yellow">'\ name = '<span style="color: red; background-color: yellow">'\
+ name + u'</span>' + name + '</span>'
lm.append(name) lm.append(name)
mm = '<br/>'.join(lm) mm = '<br/>'.join(lm)
# The marked adresses (they start with the mark '[unreachable]', # The marked adresses (they start with the mark '[unreachable]',
# '[resigned]', whatever '[xxx]') will not be displayed at all. # '[resigned]', whatever '[xxx]') will not be displayed at all.
# Only the mark will be used instead. # Only the mark will be used instead.
rexMark = re.compile(u'(?P<mark>\\[.*?\\])') rexMark = re.compile('(?P<mark>\\[.*?\\])')
le = [] le = []
for maintainer in self.__maintainersDic[obj.classId]: for maintainer in self.__maintainersDic[obj.classId]:
address = maintainer[1] address = maintainer[1]
m = rexMark.search(address) m = rexMark.search(address)
if m is not None: if m is not None:
address = u'<span style="color: brown">'\ address = '<span style="color: brown">'\
+ m.group('mark') + u'</span>' + m.group('mark') + '</span>'
le.append(address) le.append(address)
ee = u'<br/>'.join(le) ee = '<br/>'.join(le)
# Append the maintainer and e-mail elements. # Append the maintainer and e-mail elements.
lst.append(htmlTdTpl % mm) lst.append(htmlTdTpl % mm)
...@@ -1907,7 +1907,7 @@ class TrManager: ...@@ -1907,7 +1907,7 @@ class TrManager:
htmlTable = htmlTableTpl % (''.join(trlst)) htmlTable = htmlTableTpl % (''.join(trlst))
# Define templates for LaTeX table parts of the documentation. # Define templates for LaTeX table parts of the documentation.
latexTableTpl = b''' latexTableTpl = r'''
\latexonly \latexonly
\footnotesize \footnotesize
\begin{longtable}{|l|l|l|l|} \begin{longtable}{|l|l|l|l|}
...@@ -1919,9 +1919,9 @@ class TrManager: ...@@ -1919,9 +1919,9 @@ class TrManager:
\end{longtable} \end{longtable}
\normalsize \normalsize
\endlatexonly \endlatexonly
'''.decode('utf_8') '''
latexTableTpl = textwrap.dedent(latexTableTpl) latexTableTpl = textwrap.dedent(latexTableTpl)
latexLineTpl = u'\n %s & %s & {\\tt\\tiny %s} & %s \\\\' latexLineTpl = '\n' + r' %s & %s & {\tt\tiny %s} & %s \\'
# Loop through transl objects in the order of sorted readable names # Loop through transl objects in the order of sorted readable names
# and add generate the content of the LaTeX table. # and add generate the content of the LaTeX table.
...@@ -1956,9 +1956,9 @@ class TrManager: ...@@ -1956,9 +1956,9 @@ class TrManager:
# Use the template to produce the line of the table and insert # Use the template to produce the line of the table and insert
# the hline plus the constructed line into the table content. # the hline plus the constructed line into the table content.
# The underscore character must be escaped. # The underscore character must be escaped.
trlst.append(u'\n \\hline') trlst.append('\n \\hline')
s = latexLineTpl % (lang, maintainer, email, status) s = latexLineTpl % (lang, maintainer, email, status)
s = s.replace(u'_', u'\\_') s = s.replace('_', '\\_')
trlst.append(s) trlst.append(s)
# List the other maintainers for the language. Do not set # List the other maintainers for the language. Do not set
...@@ -1969,14 +1969,14 @@ class TrManager: ...@@ -1969,14 +1969,14 @@ class TrManager:
maintainer = m[0] maintainer = m[0]
email = m[1] email = m[1]
s = latexLineTpl % (lang, maintainer, email, status) s = latexLineTpl % (lang, maintainer, email, status)
s = s.replace(u'_', u'\\_') s = s.replace('_', '\\_')
trlst.append(s) trlst.append(s)
# Join the table lines and insert into the template. # Join the table lines and insert into the template.
latexTable = latexTableTpl % (u''.join(trlst)) latexTable = latexTableTpl % (''.join(trlst))
# Put the HTML and LaTeX parts together and define the dic item. # Put the HTML and LaTeX parts together and define the dic item.
tplDic['informationTable'] = htmlTable + u'\n' + latexTable tplDic['informationTable'] = htmlTable + '\n' + latexTable
# Insert the symbols into the document template and write it down. # Insert the symbols into the document template and write it down.
f = xopen(fDocName, 'w') f = xopen(fDocName, 'w')
...@@ -1986,9 +1986,10 @@ class TrManager: ...@@ -1986,9 +1986,10 @@ class TrManager:
if __name__ == '__main__': if __name__ == '__main__':
# The Python 2.6+ or 3.3+ is required. # The Python 2.6+ or 3.3+ is required.
major, minor, patch = (int(e) for e in platfor m.python_version_tuple()) major, minor, patch = (int(e) for e in platform.python_version_tuple())
if (major == 2 and minor < 6) or (major == 3 and minor < 3): print(major, minor, patch)
print('Python 2.6+ or Python 3.3+ are required for the script') if (major == 2 and minor < 6) or (major == 3 and minor < 0):
print('Python 2.6+ or Python 3.0+ are required for the script')
sys.exit(1) sys.exit(1)
# The translator manager builds the transl objects, parses the related # The translator manager builds the transl objects, parses the related
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment