]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/auxiliar/translations-status.py
translations-status.py: Replace if top: by polymorphism.
[lilypond.git] / scripts / auxiliar / translations-status.py
index c6b2557e41366be858c4b77b6177d22e25bf6178..9e5970fd31446e1349b8ef3385045a2a592cf00f 100755 (executable)
@@ -49,10 +49,9 @@ title_re = re.compile ('^@(settitle|chapter|top|(?:sub){0,2}section|'
 include_re = re.compile ('^@include (.*?)$', re.M)
 
 # allow multiple lines
-translators_re = re.compile (r'^@c[ ]+[tT]ranslators?[ ]*:[ ]*(.*?)$', re.M | re.I)
-checkers_re = re.compile (r'^@c[ ]+Translation[ ]*checkers?[ ]*:[ ]*(.*?)$',
-                          re.M | re.I)
-status_re = re.compile (r'^@c[ ]+Translation[ ]*status[ ]*:[ ]*(.*?)$', re.M | re.I)
+translators_re = re.compile (r'^@c[ ]+[Tt]ranslators?[ ]*:[ ]*(.*?)$', re.M)
+checkers_re = re.compile (r'^@c[ ]+[Tt]ranslation[ ]*[Cc]heckers?[ ]*:[ ]*(.*?)$', re.M)
+status_re = re.compile (r'^@c[ ]+[Tt]ranslation[ ]*[Ss]tatus[ ]*:[ ]*(.*?)$', re.M)
 post_gdp_re = re.compile ('post.GDP', re.I)
 untranslated_node_str = '@untranslated'
 skeleton_str = '-- SKELETON FILE --'
@@ -185,7 +184,6 @@ class TelyDocument (object):
         self.contents = 'GIT committish: 0'
         if os.path.exists (filename):
             self.contents = open (filename).read ()
-        self.top = os.path.splitext (filename)[1] in ['.tely', '.texi']
         ## record title and sectionning level of first Texinfo section
         self.sectioning = 'unnumbered'
         self.title = 'Untitled'
@@ -193,13 +191,6 @@ class TelyDocument (object):
         if m:
             self.sectioning = m.group (1)
             self.title = m.group (2)
-        # This is all quite ugly and hairy.  The original code worked
-        # with @top node detection and each manual had its own @top
-        # node.  Not any more.  Declaring .tely / .texi files to be
-        # @top sort of works...
-        if self.top:
-            self.sectioning = 'top'
-        self.level = texi_level [self.sectioning]
 
         if not hasattr (self, 'language'):
             self.language = ''
@@ -216,7 +207,12 @@ class TelyDocument (object):
                           for t in include_re.findall (self.contents)]
         self.included_files = [p for p in included_files if os.path.exists (p)]
 
+    def get_level (self):
+        return texi_level [self.sectioning]
+
     def print_title (self, section_number):
+        if not hasattr (self, 'level'):
+            self.level = self.get_level ()
         return section_number.increase (self.level) + self.title
 
 
@@ -248,12 +244,15 @@ class TranslatedTelyDocument (TelyDocument):
         if m:
             self.translators = [n.strip () for n in
                                 reduce (operator.add, [n.split (',') for n in m])]
+        if self.language != self.filename[:2]:
+            print 'Barf:', self.filename
+            barf
         if (not isinstance (self, UntranslatedTelyDocument)
             and (not self.translators or not self.translators[0])
             and not 'macros.itexi' in self.filename):
-            error ('%s: no translator name found, \nplease \
-specify at least one in the master file as a line containing\n\
-@c Translators: FirstName1 LastName1, FirstName2 LastName2' % self.filename)
+            error (self.filename + ''': error: no translator name found
+please specify one ore more lines in the master file
+@c Translator: FirstName LastName[, FirstName LastName]..''')
         self.checkers = []
         m = checkers_re.findall (self.contents)
         if m:
@@ -311,6 +310,9 @@ setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
 setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
                 self.uptodate_percentage = alternative
 
+    def get_level (self):
+        return texi_level ['top']
+
     def completeness (self, formats=['long'], translated=False):
         if translated:
             translation = self.translation
@@ -359,7 +361,7 @@ setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
             return self.translation (format_table['pre-GDP'])
 
     def short_texi_status (self):
-        s = '  <td>'
+        s = '  <td title="%(filename)s">' % self.__dict__
         if self.partially_translated:
             s += '<br>\n   '.join (self.translators) + '<br>\n'
             if self.checkers:
@@ -386,52 +388,63 @@ setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
         return s
 
     def texi_status (self, numbering=SectionNumber ()):
-        if self.title == 'Untitled':
-            return ''
-
-        if self.top:
-            s = '''<table align="center" border="2">
+        s = '''<table align="center" border="2">
  <tr align="center">
   <th>%s</th>''' % self.print_title (numbering)
-            s += ''.join (['  <th>%s</th>\n' % self.translation (h)
-                           for h in detailed_status_heads])
-            s += ' </tr>\n'
-            s += ' <tr align="left">\n  <td>%s<br>(%d)</td>\n' \
-                % (self.translation (section_titles_string),
-                   sum (self.masterdocument.word_count))
+        s += ''.join (['  <th>%s</th>\n' % self.translation (h)
+                       for h in detailed_status_heads])
+        s += ' </tr>\n'
+        s += (' <tr align="left">\n  <td title="%%(filename)s">%s<br>(%d)</td>\n'
+              % (self.translation (section_titles_string),
+                 sum (self.masterdocument.word_count))) % self.__dict__
+        s += self.texi_body (numbering)
+        s += '</table>\n<p></p>\n'
+        return s
 
-        else:
-            s = ' <tr align="left">\n  <td>%s<br>(%d)</td>\n' \
-                % (self.print_title (numbering),
-                   sum (self.masterdocument.word_count))
+    def texi_body (self, numbering):
+        return (self.texi_translators ()
+                + self.texi_completeness ()
+                + self.texi_uptodateness ()
+                + self.texi_gdp ()
+                + self.texi_translations (numbering))
 
+    def texi_translators (self):
         if self.partially_translated:
-            s += '  <td>' + '<br>\n   '.join (self.translators) + '</td>\n'
-            s += '  <td>' + '<br>\n   '.join (self.checkers) + '</td>\n'
-        else:
-            s += '  <td></td>\n' * 2
+            return ('  <td>' + '<br>\n   '.join (self.translators) + '</td>\n'
+                    + '  <td>' + '<br>\n   '.join (self.checkers) + '</td>\n')
+        return '  <td></td>\n' * 2
 
+    def texi_completeness (self):
         c = self.completeness (['color', 'short'], translated=True)
-        s += '  <td><span style="background-color: #%(color)s">\
+        return '  <td><span style="background-color: #%(color)s">\
 %(short)s</span></td>\n' % {'color': c['color'],
                            'short': c['short']}
 
+    def texi_uptodateness (self):
         if self.partially_translated:
             u = self.uptodateness (['short', 'color'], translated=True)
-            s += '  <td><span style="background-color: #%(color)s">\
+            return '  <td><span style="background-color: #%(color)s">\
 %(short)s</span></td>\n' % {'color': u['color'],
                            'short': u['short']}
-        else:
-            s += '  <td></td>\n'
+        return '  <td></td>\n'
 
-        s += '  <td>' + self.gdp_status () + '</td>\n </tr>\n'
-        s += ''.join ([i.translations[self.language].texi_status (numbering)
-                       for i in self.masterdocument.includes
-                       if self.language in i.translations])
+    def texi_gdp (self):
+        return '  <td>' + self.gdp_status () + '</td>\n </tr>\n'
 
-        if self.top:
-            s += '</table>\n<p></p>\n'
-        return s
+    def texi_translations (self, numbering):
+        return ''.join ([i.translations[self.language].texi_status (numbering)
+                         for i in self.masterdocument.includes
+                         if self.language in i.translations])
+
+class IncludedTranslatedTelyDocument (TranslatedTelyDocument):
+    get_level = TelyDocument.get_level
+    def texi_status (self, numbering=SectionNumber ()):
+        if self.title != 'Untitled':
+            return ((' <tr align="left">\n  <td title="%%(filename)s">%s<br>(%d)</td>\n'
+                     % (self.print_title (numbering),
+                        sum (self.masterdocument.word_count))) % self.__dict__
+                    + self.texi_body (numbering))
+        return ''
 
 class UntranslatedTelyDocument (TranslatedTelyDocument):
     def __init__ (self, filename, masterdocument, parent_translation=None):
@@ -439,6 +452,9 @@ class UntranslatedTelyDocument (TranslatedTelyDocument):
             self.language = filename[:2]
         TranslatedTelyDocument.__init__ (self, filename, masterdocument, parent_translation)
 
+class IncludedUntranslatedTelyDocument (UntranslatedTelyDocument, IncludedTranslatedTelyDocument):
+    get_level = TelyDocument.get_level
+
 class MasterTelyDocument (TelyDocument):
     def __init__ (self,
                   filename,
@@ -448,31 +464,25 @@ class MasterTelyDocument (TelyDocument):
         self.size = len (self.contents)
         self.word_count = tely_word_count (self.contents)
         self.translations = {}
-        found = {}
-        translations = dict ([(lang, os.path.join (lang, filename))
-                              for lang in langdefs.LANGDICT])
+        self.includes = []
         if not self.language or self.language == 'en':
             languages = [x for x in parent_translations.keys () if x != 'en']
-            for lang in languages:
-                if os.path.exists (translations[lang]):
-                    found[lang] = True
-                    self.translations[lang] = TranslatedTelyDocument (translations[lang],
-                                                                      self,
-                                                                      parent_translations.get (lang))
-                else:
-                    self.translations[lang] = UntranslatedTelyDocument (translations[lang],
-                                                                        self,
-                                                                        parent_translations.get (lang))
-            if self.top:
-                for lang in [x for x in langdefs.LANGDICT if x and x != 'en']:
-                    if not found.has_key (lang):
-                        del self.translations[lang]
-
-        if self.translations:
-            self.includes = [MasterTelyDocument (f, self.translations)
-                             for f in self.included_files]
-        else:
-            self.includes = []
+            self.translations = dict ([x for x in
+                                       [(lang, self.translated_factory (os.path.join (lang, self.filename),
+                                                                        parent_translations.get (lang)))
+                                        for lang in languages]
+                                       if x[1]])
+            if self.translations:
+                self.includes = [IncludedMasterTelyDocument (f, self.translations)
+                                 for f in self.included_files]
+
+    def get_level (self):
+        return texi_level ['top']
+
+    def translated_factory (self, filename, parent):
+        if os.path.exists (filename):
+            return TranslatedTelyDocument (filename, self, parent)
+        return None
 
     def update_word_counts (self, s):
         s = update_word_count (s, self.filename, sum (self.word_count))
@@ -481,56 +491,62 @@ class MasterTelyDocument (TelyDocument):
         return s
 
     def texi_status (self, numbering=SectionNumber ()):
-        if self.title == 'Untitled' or not self.translations:
-            return ''
-        if self.top:
-            s = '''<table align="center" border="2">
+        s = '''<table align="center" border="2">
  <tr align="center">
   <th>%s</th>''' % self.print_title (numbering)
-            s += ''.join (['  <th>%s</th>\n' % l for l in self.translations])
-            s += ' </tr>\n'
-            s += ' <tr align="left">\n  <td>Section titles<br>(%d)</td>\n' \
-                % sum (self.word_count)
-
-        else:  # if self is an included file
-           s = ' <tr align="left">\n  <td>%s<br>(%d)</td>\n' \
-                % (self.print_title (numbering), sum (self.word_count))
-
-        s += ''.join ([t.short_texi_status ()
-                       for t in self.translations.values ()])
+        s += ''.join (['  <th>%s</th>\n' % l for l in sorted (self.translations.keys ())])
         s += ' </tr>\n'
-        s += ''.join ([i.texi_status (numbering) for i in self.includes])
-
-        if self.top:
-            s += '</table>\n<p></p>\n'
+        s += (' <tr align="left">\n  <td title="%%(filename)s">Section titles<br>(%d)</td>\n'
+              % sum (self.word_count)) % self.__dict__
+        s += self.texi_body (numbering)
+        s += '</table>\n<p></p>\n'
         return s
 
+    def texi_body (self, numbering):
+        return (''.join ([self.translations[k].short_texi_status ()
+                          for k in sorted (self.translations.keys ())])
+                + ' </tr>\n'
+                + ''.join ([i.texi_status (numbering) for i in self.includes]))
+
     def text_status (self, numbering=SectionNumber (), colspec=[48,12]):
-        if self.title == 'Untitled' or not self.translations:
-            return ''
+        s = (self.print_title (numbering) + ' ').ljust (colspec[0])
+        s += ''.join (['%s'.ljust (colspec[1]) % l
+                       for l in sorted (self.translations.keys ())])
+        s += '\n'
+        s += ('Section titles (%d)' % \
+                  sum (self.word_count)).ljust (colspec[0])
+        s += self.text_body (numbering, colspec)
+        s += '\n'
+        return s
 
-        s = ''
-        if self.top:
-            s += (self.print_title (numbering) + ' ').ljust (colspec[0])
-            s += ''.join (['%s'.ljust (colspec[1]) % l
-                           for l in self.translations])
-            s += '\n'
-            s += ('Section titles (%d)' % \
-                      sum (self.word_count)).ljust (colspec[0])
+    def text_body (self, numbering, colspec):
+        return (''.join ([self.translations[k].text_status ().ljust(colspec[1])
+                          for k in sorted (self.translations.keys ())])
+                + '\n\n'
+                + ''.join ([i.text_status (numbering) for i in self.includes]))
 
-        else:
-            s = '%s (%d) ' \
-                % (self.print_title (numbering), sum (self.word_count))
-            s = s.ljust (colspec[0])
+class IncludedMasterTelyDocument (MasterTelyDocument):
+    get_level = TelyDocument.get_level
 
-        s += ''.join ([t.text_status ().ljust(colspec[1])
-                       for t in self.translations.values ()])
-        s += '\n\n'
-        s += ''.join ([i.text_status (numbering) for i in self.includes])
+    def translated_factory (self, filename, parent):
+        if os.path.exists (filename):
+            return IncludedTranslatedTelyDocument (filename, self, parent)
+        return IncludedUntranslatedTelyDocument (filename, self, parent)
 
-        if self.top:
-            s += '\n'
-        return s
+    def texi_status (self, numbering=SectionNumber ()):
+        if self.title != 'Untitled':
+            return ((' <tr align="left">\n  <td title=%%(filename)s>%s<br>(%d)</td>\n'
+                     % (self.print_title (numbering), sum (self.word_count))) % self.__dict__
+                    + self.texi_body (numbering))
+        return ''
+
+    def text_status (self, numbering=SectionNumber (), colspec=[48,12]):
+        if self.title != 'Untitled':
+            return (('%s (%d) '
+                     % (self.print_title (numbering), sum (self.word_count)))
+                    + self.text_body (numbering, colspec)
+                    ).ljust (colspec[0])
+        return ''
 
 
 update_category_word_counts_re = re.compile (r'(?ms)^-(\d+)-(.*?\n)\d+ *total')