Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pepr committed Jun 16, 2014
1 parent 962ad74 commit 6212c2d
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions doc/translator.py
Expand Up @@ -64,8 +64,11 @@
2013/06/25 - TranslatorDecoder checks removed after removing the class.
2013/09/04 - Coloured status in langhowto. *ALMOST up-to-date* category
of translators introduced.
2014/06/16 - unified for Python 2.6+ and 3.0+
"""

from __future__ import print_function

import os
import platform
import re
Expand All @@ -74,23 +77,17 @@


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())
if major == 2:
if mode == 'w':
mode = 'wU'
import codecs
return codecs.open(fname, mode=mode, encoding=encoding) # Python 2
return open(fname, mode=mode) # Python 2 without encoding
else:
return open(fname, mode=mode, encoding=encoding) # Python 3
return open(fname, mode=mode, encoding=encoding) # Python 3 with encoding


def fill(s):
Expand Down Expand Up @@ -1074,6 +1071,7 @@ def processing(self):
for p in myDic:
if p not in reqDic:
self.obsoleteMethods.append(p)
self.obsoleteMethods.sort()

# Build the list of missing methods and the list of implemented
# required methods.
Expand All @@ -1084,6 +1082,8 @@ def processing(self):
self.implementedMethods.append(p)
else:
self.missingMethods.append(p)
self.missingMethods.sort()
self.implementedMethods.sort()

# Check whether adapter must be used or suggest the newest one.
# Change the status and set the note accordingly.
Expand Down Expand Up @@ -1788,29 +1788,29 @@ def generateLanguageDoc(self):
doctpl = f.read()
f.close()

pos = doctpl.find(u'/***')
pos = doctpl.find('/***')
assert pos != -1
doctpl = doctpl[pos:]

# Fill the tplDic by symbols that will be inserted into the
# document template.
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['doxVersion'] = self.doxVersion
tplDic['supportedLangReadableStr'] = self.supportedLangReadableStr
tplDic['translatorReportFileName'] = self.translatorReportFileName

ahref = u'<a href="../doc/' + self.translatorReportFileName
ahref += u'"\n><code>doxygen/doc/' + self.translatorReportFileName
ahref += u'</code></a>'
ahref = '<a href="../doc/' + self.translatorReportFileName
ahref += '"\n><code>doxygen/doc/' + self.translatorReportFileName
ahref += '</code></a>'
tplDic['translatorReportLink'] = ahref
tplDic['numLangStr'] = str(self.numLang)

# Define templates for HTML table parts of the documentation.
htmlTableTpl = u'''\
htmlTableTpl = '''\
\\htmlonly
<table align="center" cellspacing="0" cellpadding="0" border="0">
<tr bgcolor="#000000">
Expand All @@ -1833,9 +1833,9 @@ def generateLanguageDoc(self):
\\endhtmlonly
'''
htmlTableTpl = textwrap.dedent(htmlTableTpl)
htmlTrTpl = u'\n <tr bgcolor="#ffffff">%s\n </tr>'
htmlTdTpl = u'\n <td>%s</td>'
htmlTdStatusColorTpl = u'\n <td bgcolor="%s">%s</td>'
htmlTrTpl = '\n <tr bgcolor="#ffffff">%s\n </tr>'
htmlTdTpl = '\n <td>%s</td>'
htmlTdStatusColorTpl = '\n <td bgcolor="%s">%s</td>'

# Loop through transl objects in the order of sorted readable names
# and add generate the content of the HTML table.
Expand All @@ -1848,7 +1848,7 @@ def generateLanguageDoc(self):
if obj.readableStatus.startswith('1.4'):
bkcolor = self.getBgcolorByReadableStatus('1.4')
else:
bkcolor = u'#ffffff'
bkcolor = '#ffffff'

lst = [ htmlTdStatusColorTpl % (bkcolor, obj.langReadable) ]

Expand All @@ -1863,8 +1863,8 @@ def generateLanguageDoc(self):
classId = obj.classId[:-2]
if classId in self.__translDic:
lang = self.__translDic[classId].langReadable
mm = u'see the %s language' % lang
ee = u'&nbsp;'
mm = 'see the %s language' % lang
ee = '&nbsp;'

if not mm and obj.classId in self.__maintainersDic:
# Build a string of names separated by the HTML break element.
Expand All @@ -1873,24 +1873,24 @@ def generateLanguageDoc(self):
for maintainer in self.__maintainersDic[obj.classId]:
name = maintainer[0]
if name.startswith('--'):
name = u'<span style="color: red; background-color: yellow">'\
+ name + u'</span>'
name = '<span style="color: red; background-color: yellow">'\
+ name + '</span>'
lm.append(name)
mm = '<br/>'.join(lm)

# The marked adresses (they start with the mark '[unreachable]',
# '[resigned]', whatever '[xxx]') will not be displayed at all.
# Only the mark will be used instead.
rexMark = re.compile(u'(?P<mark>\\[.*?\\])')
rexMark = re.compile('(?P<mark>\\[.*?\\])')
le = []
for maintainer in self.__maintainersDic[obj.classId]:
address = maintainer[1]
m = rexMark.search(address)
if m is not None:
address = u'<span style="color: brown">'\
+ m.group('mark') + u'</span>'
address = '<span style="color: brown">'\
+ m.group('mark') + '</span>'
le.append(address)
ee = u'<br/>'.join(le)
ee = '<br/>'.join(le)

# Append the maintainer and e-mail elements.
lst.append(htmlTdTpl % mm)
Expand All @@ -1907,7 +1907,7 @@ def generateLanguageDoc(self):
htmlTable = htmlTableTpl % (''.join(trlst))

# Define templates for LaTeX table parts of the documentation.
latexTableTpl = b'''
latexTableTpl = r'''
\latexonly
\footnotesize
\begin{longtable}{|l|l|l|l|}
Expand All @@ -1919,9 +1919,9 @@ def generateLanguageDoc(self):
\end{longtable}
\normalsize
\endlatexonly
'''.decode('utf_8')
'''
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
# and add generate the content of the LaTeX table.
Expand Down Expand Up @@ -1956,9 +1956,9 @@ def generateLanguageDoc(self):
# Use the template to produce the line of the table and insert
# the hline plus the constructed line into the table content.
# The underscore character must be escaped.
trlst.append(u'\n \\hline')
trlst.append('\n \\hline')
s = latexLineTpl % (lang, maintainer, email, status)
s = s.replace(u'_', u'\\_')
s = s.replace('_', '\\_')
trlst.append(s)

# List the other maintainers for the language. Do not set
Expand All @@ -1969,14 +1969,14 @@ def generateLanguageDoc(self):
maintainer = m[0]
email = m[1]
s = latexLineTpl % (lang, maintainer, email, status)
s = s.replace(u'_', u'\\_')
s = s.replace('_', '\\_')
trlst.append(s)

# 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.
tplDic['informationTable'] = htmlTable + u'\n' + latexTable
tplDic['informationTable'] = htmlTable + '\n' + latexTable

# Insert the symbols into the document template and write it down.
f = xopen(fDocName, 'w')
Expand All @@ -1986,9 +1986,10 @@ def generateLanguageDoc(self):
if __name__ == '__main__':

# The Python 2.6+ or 3.3+ is required.
major, minor, patch = (int(e) for e in platfor m.python_version_tuple())
if (major == 2 and minor < 6) or (major == 3 and minor < 3):
print('Python 2.6+ or Python 3.3+ are required for the script')
major, minor, patch = (int(e) for e in platform.python_version_tuple())
print(major, minor, patch)
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)

# The translator manager builds the transl objects, parses the related
Expand Down

0 comments on commit 6212c2d

Please sign in to comment.