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

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
Loading
Loading
Loading
Loading
+41 −40
Original line number Diff line number Diff line
@@ -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
@@ -74,23 +77,17 @@ import textwrap


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):
@@ -1074,6 +1071,7 @@ class Transl:
        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.
@@ -1084,6 +1082,8 @@ class Transl:
                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.
@@ -1788,7 +1788,7 @@ class TrManager:
        doctpl = f.read()
        f.close()

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

@@ -1796,21 +1796,21 @@ class TrManager:
        # 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">
@@ -1833,9 +1833,9 @@ class TrManager:
            \\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.
@@ -1848,7 +1848,7 @@ class TrManager:
            if obj.readableStatus.startswith('1.4'):
                bkcolor = self.getBgcolorByReadableStatus('1.4')
            else:
                bkcolor = u'#ffffff'
                bkcolor = '#ffffff'

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

@@ -1863,8 +1863,8 @@ class TrManager:
                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.
@@ -1873,24 +1873,24 @@ class TrManager:
                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)
@@ -1907,7 +1907,7 @@ class TrManager:
        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|}
@@ -1919,9 +1919,9 @@ class TrManager:
            \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.
@@ -1956,9 +1956,9 @@ class TrManager:
            # 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
@@ -1969,14 +1969,14 @@ class TrManager:
                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')
@@ -1987,8 +1987,9 @@ if __name__ == '__main__':

    # The Python 2.6+ or 3.3+ is required.
    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('Python 2.6+ or Python 3.3+ are required for the script')
    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