]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/translations-status.py
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scripts / auxiliar / translations-status.py
1 #!/usr/bin/env python
2
3 '''
4 USAGE: cd Documentation && translations-status.py
5
6   Write:
7     translations.itexi
8     <LANG>/translations.itexi
9     out/translations-status.txt
10
11   Update word counts in:
12     contributor/doc-translation-list.itexi
13
14 TODO:
15    * using markup = TexiMarkup (), html tables (columns)
16      are evenly spaced and bit too wide.  This can
17      be fixed by using
18         @multitable @columnfractions 0 0 0 0 0 0 0 0,
19      but with that, PDF and info output get borked.
20    * in info and PDF, columns have too little separation
21    * using markup = HTMLMarkup (), we get nice
22         <td title="FILENAME">
23      popups -- do we want that with texi output? -- how?
24      or possibly links to the git archive?
25
26 '''
27
28 import sys
29 import re
30 import string
31 import operator
32 import os
33 #
34 import langdefs
35 import buildlib
36
37 def progress (str):
38     sys.stderr.write (str + '\n')
39
40 exit_code = 0
41
42 def error (str, update_status=1):
43     global exit_code
44     sys.stderr.write ('translations-status.py: %s\n' % str)
45     exit_code = max (exit_code, update_status)
46
47 progress ("translations-status.py")
48
49 _doc = lambda s: s
50
51 # load gettext messages catalogs
52 translation = langdefs.translation
53
54
55 language_re = re.compile (r'^@documentlanguage (.+)', re.M)
56 comments_re = re.compile (r'^@ignore\n(.|\n)*?\n@end ignore$|@c .*?$', re.M)
57 space_re = re.compile (r'\s+', re.M)
58 lilypond_re = re.compile (r'@lilypond({.*?}|(.|\n)*?\n@end lilypond$)', re.M)
59 node_re = re.compile ('^@node .*?$', re.M)
60 title_re = re.compile ('^@(settitle|chapter|top|(?:sub){0,2}section|'
61                            '(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?) (.*?)$', re.M)
62 include_re = re.compile ('^@include (.*?)$', re.M)
63
64 # allow multiple lines
65 translators_re = re.compile (r'^@c[ ]+[Tt]ranslators?[ ]*:[ ]*(.*?)$', re.M)
66 checkers_re = re.compile (r'^@c[ ]+[Tt]ranslation[ ]*[Cc]heckers?[ ]*:[ ]*(.*?)$', re.M)
67 status_re = re.compile (r'^@c[ ]+[Tt]ranslation[ ]*[Ss]tatus[ ]*:[ ]*(.*?)$', re.M)
68 post_gdp_re = re.compile ('post.GDP', re.I)
69 untranslated_node_str = '@untranslated'
70 skeleton_str = '-- SKELETON FILE --'
71
72 section_titles_string = _doc ('Section titles')
73 last_updated_string = _doc ('Last updated %s')
74 detailed_status_heads = [_doc ('Translators'), _doc ('Translation checkers'),
75                          _doc ('Translated'), _doc ('Up to date'),
76                          _doc ('Other info')]
77 format_table = {
78     'not translated': {'color':'d0f0f8', 'short':_doc ('no'), 'abbr':'NT',
79                        'long':_doc ('not translated')},
80     'partially translated': {'color':'dfef77',
81                              'short':_doc ('partially (%(p)d %%)'),
82                              'abbr':'%(p)d%%',
83                              'long':_doc ('partially translated (%(p)d %%)')},
84     'fully translated': {'color':'1fff1f', 'short':_doc ('yes'), 'abbr':'FT',
85                          'long': _doc ('translated')},
86     'up to date': {'short':_doc ('yes'), 'long':_doc ('up to date'),
87                    'abbr':'100%%', 'vague':_doc ('up to date')},
88     'outdated': {'short':_doc ('partially'), 'abbr':'%(p)d%%',
89                  'vague':_doc ('partially up to date')},
90     'N/A': {'short':_doc ('N/A'), 'abbr':'N/A', 'color':'d587ff', 'vague':''},
91     'pre-GDP':_doc ('pre-GDP'),
92     'post-GDP':_doc ('post-GDP')
93 }
94
95 texi_level = {
96 # (Unumbered/Numbered/Lettered, level)
97     'top': ('u', 0),
98     'unnumbered': ('u', 1),
99     'unnumberedsec': ('u', 2),
100     'unnumberedsubsec': ('u', 3),
101     'chapter': ('n', 1),
102     'section': ('n', 2),
103     'subsection': ('n', 3),
104     'appendix': ('l', 1),
105     'appendixsec': ('l', 2),
106 }
107
108 appendix_number_trans = string.maketrans ('@ABCDEFGHIJKLMNOPQRSTUVWXY',
109                                           'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
110
111 class SectionNumber (object):
112     def __init__ (self):
113         self.__data = [[0,'u']]
114
115     def __increase_last_index (self):
116         type = self.__data[-1][1]
117         if type == 'l':
118             self.__data[-1][0] = \
119                 self.__data[-1][0].translate (appendix_number_trans)
120         elif type == 'n':
121             self.__data[-1][0] += 1
122
123     def format (self):
124         if self.__data[-1][1] == 'u':
125             return ''
126         return '.'.join ([str (i[0]) for i in self.__data if i[1] != 'u']) + ' '
127
128     def increase (self, (type, level)):
129         if level == 0:
130             self.__data = [[0,'u']]
131         while level + 1 < len (self.__data):
132             del self.__data[-1]
133         if level + 1 > len (self.__data):
134             self.__data.append ([0, type])
135             if type == 'l':
136                 self.__data[-1][0] = '@'
137         if type == self.__data[-1][1]:
138             self.__increase_last_index ()
139         else:
140             self.__data[-1] = ([0, type])
141             if type == 'l':
142                 self.__data[-1][0] = 'A'
143             elif type == 'n':
144                 self.__data[-1][0] = 1
145         return self.format ()
146
147
148 def percentage_color (percent):
149     p = percent / 100.0
150     if p < 0.33:
151         c = [hex (int (3 * p * b + (1 - 3 * p) * a))[2:]
152              for (a, b) in [(0xff, 0xff), (0x5c, 0xa6), (0x5c, 0x4c)]]
153     elif p < 0.67:
154         c = [hex (int ((3 * p - 1) * b + (2 - 3 * p) * a))[2:]
155              for (a, b) in [(0xff, 0xff), (0xa6, 0xff), (0x4c, 0x3d)]]
156     else:
157         c = [hex (int ((3 * p - 2) * b + 3 * (1 - p) * a))[2:]
158              for (a, b) in [(0xff, 0x1f), (0xff, 0xff), (0x3d, 0x1f)]]
159     return ''.join (c)
160
161
162 def update_word_count (text, filename, word_count):
163     return re.sub (r'(?m)^(\d+) *' + filename,
164                    str (word_count).ljust (6) + filename,
165                    text)
166
167 po_msgid_re = re.compile (r'^msgid "(.*?)"(?:\n"(.*?)")*', re.M)
168
169 def po_word_count (po_content):
170     s = ' '.join ([''.join (t) for t in po_msgid_re.findall (po_content)])
171     return len (space_re.split (s))
172
173 sgml_tag_re = re.compile (r'<.*?>', re.S)
174
175 def sgml_word_count (sgml_doc):
176     s = sgml_tag_re.sub ('', sgml_doc)
177     return len (space_re.split (s))
178
179 def tely_word_count (tely_doc):
180     '''
181     Calculate word count of a Texinfo document node by node.
182
183     Take string tely_doc as an argument.
184     Return a list of integers.
185
186     Texinfo comments and @lilypond blocks are not included in word counts.
187     '''
188     tely_doc = comments_re.sub ('', tely_doc)
189     tely_doc = lilypond_re.sub ('', tely_doc)
190     nodes = node_re.split (tely_doc)
191     return [len (space_re.split (n)) for n in nodes]
192
193 class HTMLMarkup (object):
194     texi_header = '''@c -*- coding: utf-8; mode: texinfo; -*-
195 @c This file was generated by translation-status.py -- DO NOT EDIT!
196 @ignore
197     Translation of GIT committish: 0
198 @end ignore
199
200 '''
201     texi_footer = '''
202 '''
203     def texi (self, string):
204         return (self.texi_header
205                 + '''
206 @ifnothtml
207 Translation status currently only available in HTML.
208 @end ifnothtml
209 '''
210                 + string
211                 + self.texi_footer)
212     def entity (self, name, string='', attributes=[]):
213         attr_list = ''.join ([' %s="%s"' % x for x in attributes])
214         return '<%(name)s%(attr_list)s>%(string)s</%(name)s>' % locals ()
215     def paragraph (self, string=''):
216         return self.entity ('p', string)
217     def table (self, string):
218         return self.entity ('table', string, [('align', 'center'), ('border', '2')])
219     def row (self, string, attributes=[]):
220         return self.entity ('tr', string, attributes)
221     headrow = row
222     def headcell (self, string, attributes=[]):
223         return self.entity ('th', string, attributes)
224     def cell (self, string='', attributes=[]):
225         return self.entity ('td', string, attributes)
226     def newline (self, attributes=[]):
227         return self.entity ('br', '', attributes)[:-5]
228     def span (self, string, attributes=[]):
229         return self.entity ('span', string, attributes)
230     def small (self, string, attributes=[]):
231         return self.entity ('small', string, attributes)
232     def emph (self, string, attributes=[]):
233         return self.entity ('em', string, attributes)
234
235 class TexiMarkup (HTMLMarkup):
236     def texi (self, string):
237         return (self.texi_header
238                 + self.html ('''
239 <style type="text/css"><!--
240 th { border: 1px solid black; text-align: center; }
241 td { border: 1px solid black; text-align: center; }
242 !--></style>
243 ''')
244                 + self.columnfraction_disaster (self.itemtab_disaster (string))
245                 + self.texi_footer)
246     def itemtab_disaster (self, string):
247         return string.replace ('''item \n@tab ''', '''item
248 ''')
249     def columnfraction_disaster (self, string):
250         if False:
251             # nice trick for html-only
252             return string.replace ('@multitable', '@multitable @columnfractions 0 0 0 0 0 0 0 0 0 0')
253         tables = re.findall ('(?s)(@multitable)(.*?)(@item)', string)
254         for t in tables:
255             columns = len (re.findall ('(?s)(\n@tab)', t[1])) + 1
256             columnfractions = '@columnfractions ' + (' ' + str (1.0/columns)) * columns
257             string = string.replace ('@multitable\n',
258                                      '@multitable %(columnfractions)s\n' % locals (), 1)
259         return string
260     def entity (self, name, string='', attributes=[]):
261         return '''
262 @%(name)s
263 %(string)s
264 @end %(name)s''' % locals ()
265     def paragraph (self, string=''):
266         return '''
267 %(string)s''' % locals ()
268     def table (self, string):
269         # Ugh, makeinfo is fine without @columnfractions
270         # but texi2html 1.82 barfs: `empty multicolumn'
271         return (self.entity ('multitable', string))
272     def headrow (self, string, attributes=[]):
273         return '''
274 @headitem ''' + string
275     def row (self, string, attributes=[]):
276         return '''
277 @item ''' + string
278     def cell (self, string='', attributes=[]):
279         return '''
280 @tab ''' + string
281     headcell = cell
282     def newline (self):
283         return '''
284 @*
285 '''
286     def html (self, string):
287         return self.entity ('ifhtml', self.entity ('html', string))
288     def nothtml (self, string):
289         return self.entity ('ifnothtml', string)
290     def span (self, string, attributes=[]):
291         return (self.html (HTMLMarkup ().span (string, attributes))
292                 + self.nothtml (string))
293     def small (self, string, attributes=[]):
294         return (self.html (HTMLMarkup ().small (string, attributes))
295                 + self.nothtml (string))
296     def command (self, name, string):
297         return '@%(name)s{%(string)s}' % locals ()
298     def emph (self, string, attributes=[]):
299         return self.command ('emph', string)
300
301 class TelyDocument (object):
302     def __init__ (self, filename):
303         self.filename = filename
304         self.contents = 'GIT committish: 0'
305         if os.path.exists (filename):
306             self.contents = open (filename).read ()
307         ## record title and sectionning level of first Texinfo section
308         self.sectioning = 'unnumbered'
309         self.title = 'Untitled'
310         m = title_re.search (self.contents)
311         if m:
312             self.sectioning = m.group (1)
313             self.title = m.group (2)
314
315         if not hasattr (self, 'language'):
316             self.language = ''
317         m = language_re.search (self.contents)
318         if m:
319             self.language = m.group (1)
320
321         dir = os.path.dirname (filename).split ('/')[0]
322         if len (dir) == 2:
323             dir += '/'
324         else:
325             dir = ''
326         included_files = [dir + t
327                           for t in include_re.findall (self.contents)]
328         self.included_files = [p for p in included_files if os.path.exists (p)]
329
330     def get_level (self):
331         return texi_level [self.sectioning]
332
333     def print_title (self, section_number):
334         if not hasattr (self, 'level'):
335             self.level = self.get_level ()
336         return section_number.increase (self.level) + self.title
337
338
339 class TranslatedTelyDocument (TelyDocument):
340     def __init__ (self, filename, masterdocument, parent_translation=None):
341         TelyDocument.__init__ (self, filename)
342         self.masterdocument = masterdocument
343         if not hasattr (self, 'language'):
344             self.language = ''
345         if not self.language and parent_translation:
346             self.language = parent_translation.__dict__.get ('language', '')
347         if self.language == 'en':
348             print filename + ': language en specified: set @documentlanguage', self.filename[:2]
349             self.language = ''
350         if not self.language and filename[2] == '/':
351             print filename + ': no language specified: add @documentlanguage', self.filename[:2]
352             self.language = filename[:2]
353         if self.language:
354             self.translation = translation[self.language]
355         else:
356             self.translation = lambda x: x
357         self.title = self.translation (self.title)
358
359         ## record authoring information
360         self.translators = ['']
361         if parent_translation:
362             self.translators = parent_translation.__dict__.get ('translators', [''])
363         m = translators_re.findall (self.contents)
364         if m:
365             self.translators = [n.strip () for n in
366                                 reduce (operator.add, [n.split (',') for n in m])]
367         if self.language != self.filename[:2]:
368             print 'Barf:', self.filename
369             barf
370         if (not isinstance (self, UntranslatedTelyDocument)
371             and (not self.translators or not self.translators[0])
372             and not 'macros.itexi' in self.filename):
373             error (self.filename + ''': error: no translator name found
374     please specify one ore more lines in the master file
375     @c Translator: FirstName LastName[, FirstName LastName]..''')
376         self.checkers = []
377         m = checkers_re.findall (self.contents)
378         if m:
379             self.checkers = [n.strip () for n in
380                              reduce (operator.add, [n.split (',') for n in m])]
381         if not self.checkers and isinstance (parent_translation, TranslatedTelyDocument):
382             self.checkers = parent_translation.checkers
383
384         ## check whether translation is pre- or post-GDP
385         m = status_re.search (self.contents)
386         if m:
387             self.post_gdp = bool (post_gdp_re.search (m.group (1)))
388         else:
389             self.post_gdp = False
390
391         ## record which parts (nodes) of the file are actually translated
392         self.partially_translated = not skeleton_str in self.contents
393         nodes = node_re.split (self.contents)
394         self.translated_nodes = [not untranslated_node_str in n for n in nodes]
395
396         ## calculate translation percentage
397         master_total_word_count = sum (masterdocument.word_count)
398         translation_word_count = \
399             sum ([masterdocument.word_count[k] * self.translated_nodes[k]
400                   for k in range (min (len (masterdocument.word_count),
401                                        len (self.translated_nodes)))])
402         self.translation_percentage = \
403             100 * translation_word_count / master_total_word_count
404
405         ## calculate how much the file is outdated
406         (diff_string, git_error) = \
407             buildlib.check_translated_doc (masterdocument.filename, self.filename, self.contents)
408         if git_error:
409             sys.stderr.write ('warning: %s: %s' % (self.filename, git_error))
410             self.uptodate_percentage = None
411         else:
412             diff = diff_string.splitlines ()
413             insertions = sum ([len (l) - 1 for l in diff
414                                if l.startswith ('+')
415                                and not l.startswith ('+++')])
416             deletions = sum ([len (l) - 1 for l in diff
417                               if l.startswith ('-')
418                               and not l.startswith ('---')])
419             outdateness_percentage = 50.0 * (deletions + insertions) / \
420                 (masterdocument.size + 0.5 * (deletions - insertions))
421             self.uptodate_percentage = 100 - int (outdateness_percentage)
422             if self.uptodate_percentage > 100:
423                 alternative = 50
424                 progress ("%s: strange uptodateness percentage %d %%, \
425 setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
426                 self.uptodate_percentage = alternative
427             elif self.uptodate_percentage < 1:
428                 alternative = 1
429                 progress ("%s: strange uptodateness percentage %d %%, \
430 setting to %d %%" % (self.filename, self.uptodate_percentage, alternative))
431                 self.uptodate_percentage = alternative
432
433     def get_level (self):
434         return texi_level ['top']
435
436     def completeness (self, formats=['long'], translated=False):
437         if translated:
438             translation = self.translation
439         else:
440             translation = lambda x: x
441
442         if isinstance (formats, str):
443             formats = [formats]
444         p = self.translation_percentage
445         if p == 0:
446             status = 'not translated'
447         elif p == 100:
448             status = 'fully translated'
449         else:
450             status = 'partially translated'
451         return dict ([(f, translation (format_table[status][f]) % locals())
452                       for f in formats])
453
454     def uptodateness (self, formats=['long'], translated=False):
455         if translated:
456             translation = self.translation
457         else:
458             translation = lambda x: x
459
460         if isinstance (formats, str):
461             formats = [formats]
462         p = self.uptodate_percentage
463         if p == None:
464             status = 'N/A'
465         elif p == 100:
466             status = 'up to date'
467         else:
468             status = 'outdated'
469         l = {}
470         for f in formats:
471             if f == 'color' and p != None:
472                 l['color'] = percentage_color (p)
473             else:
474                 l[f] = translation (format_table[status][f]) % locals ()
475         return l
476
477     def gdp_status (self):
478         if self.post_gdp:
479             return self.translation (format_table['post-GDP'])
480         else:
481             return self.translation (format_table['pre-GDP'])
482
483     def short_texi_status (self, markup):
484         s = ''
485         if self.partially_translated:
486             s += markup.newline ().join (self.translators + [''])
487             if self.checkers:
488                 s += markup.newline ().join ([markup.small (x) for x in self.checkers + ['']])
489         c = self.completeness (['color', 'long'])
490         s += markup.span ('%(long)s' % c, [('style', 'background-color: #%(color)s' % c)])
491         s += markup.newline ()
492         if self.partially_translated:
493             u = self.uptodateness (['vague', 'color'])
494             s += markup.span ('%(vague)s' % u, [('style', 'background-color: #%(color)s' % u)])
495         return markup.cell (s, [('title', filename)])
496
497     def text_status (self):
498         s = self.completeness ('abbr')['abbr'] + ' '
499         if self.partially_translated:
500             s += self.uptodateness ('abbr')['abbr'] + ' '
501         return s
502
503     def texi_status (self, markup, numbering=SectionNumber ()):
504         return (markup.table (
505                 markup.headrow (
506                     (markup.headcell (self.print_title (numbering))
507                      + ''.join ([markup.headcell (self.translation (h))
508                                  for h in detailed_status_heads])),
509                     [('align', 'center')])
510                 + markup.row (
511                     (markup.cell ((self.translation (section_titles_string)
512                                    + markup.newline ()
513                                    + '%d' % sum (self.masterdocument.word_count)),
514                                   [('title',filename)])
515                      + self.texi_body (markup, numbering)),
516                     [('align','left')])
517                 + self.texi_translations (markup, numbering))
518                 ) + markup.paragraph ()
519
520     def texi_body (self, markup, numbering):
521         return (self.texi_translators (markup)
522                 + self.texi_completeness (markup)
523                 + self.texi_uptodateness (markup)
524                 + self.texi_gdp (markup))
525
526     def texi_translators (self, markup):
527         if self.partially_translated:
528             return (markup.cell (markup.newline ().join (self.translators))
529                     + markup.cell (markup.newline ().join (self.checkers)))
530         return markup.cell () + markup.cell ()
531
532     def texi_completeness (self, markup):
533         c = self.completeness (['color', 'short'], translated=True)
534         return markup.cell (markup.span (c['short'],
535                                          [('style', 'background-color: #' + c['color'])]))
536
537     def texi_uptodateness (self, markup):
538         if self.partially_translated:
539             u = self.uptodateness (['short', 'color'], translated=True)
540             return markup.cell (markup.span (u['short'],
541                                              [('style', 'background-color: #' + u['color'])]))
542         return markup.cell ()
543
544     def texi_gdp (self, markup):
545         return markup.cell (self.gdp_status ())
546
547     def texi_translations (self, markup, numbering):
548         return ''.join ([i.translations[self.language].texi_status (markup, numbering)
549                          for i in self.masterdocument.includes
550                          if self.language in i.translations])
551
552 class IncludedTranslatedTelyDocument (TranslatedTelyDocument):
553     get_level = TelyDocument.get_level
554     def texi_status (self, markup, numbering=SectionNumber ()):
555         if self.title != 'Untitled':
556             return (markup.row (
557                     (markup.cell ((
558                                 self.print_title (numbering)
559                                 + markup.newline ()
560                                 + '%d' % sum (self.masterdocument.word_count)),
561                                   [('title',filename)])
562                      + self.texi_body (markup, numbering)),
563                     [('align','left')])
564                     + self.texi_translations (markup, numbering))
565         return ''
566
567 class UntranslatedTelyDocument (TranslatedTelyDocument):
568     def __init__ (self, filename, masterdocument, parent_translation=None):
569         if filename[2] == '/':
570             self.language = filename[:2]
571         TranslatedTelyDocument.__init__ (self, filename, masterdocument, parent_translation)
572
573 class IncludedUntranslatedTelyDocument (UntranslatedTelyDocument, IncludedTranslatedTelyDocument):
574     get_level = TelyDocument.get_level
575
576 class MasterTelyDocument (TelyDocument):
577     def __init__ (self,
578                   filename,
579                   parent_translations=dict ([(lang, None)
580                                              for lang in langdefs.LANGDICT])):
581         TelyDocument.__init__ (self, filename)
582         self.size = len (self.contents)
583         self.word_count = tely_word_count (self.contents)
584         self.translations = {}
585         self.includes = []
586         if not self.language or self.language == 'en':
587             languages = [x for x in parent_translations.keys () if x != 'en']
588             self.translations = dict ([x for x in
589                                        [(lang, self.translated_factory (os.path.join (lang, self.filename),
590                                                                         parent_translations.get (lang)))
591                                         for lang in languages]
592                                        if x[1]])
593             if self.translations:
594                 self.includes = [IncludedMasterTelyDocument (f, self.translations)
595                                  for f in self.included_files]
596
597     def get_level (self):
598         return texi_level ['top']
599
600     def translated_factory (self, filename, parent):
601         if os.path.exists (filename):
602             return TranslatedTelyDocument (filename, self, parent)
603         return None
604
605     def update_word_counts (self, s):
606         s = update_word_count (s, self.filename, sum (self.word_count))
607         for i in self.includes:
608             s = i.update_word_counts (s)
609         return s
610
611     def texi_status (self, markup, numbering=SectionNumber ()):
612         return markup.table (
613             (markup.headrow (
614                     (markup.headcell (self.print_title (numbering))
615                      + ''.join ([markup.headcell (l) for l in sorted (self.translations.keys ())])),
616                     [('align','center')])
617              + markup.row (
618                     (markup.cell (('Section titles'
619                                    + markup.newline ()
620                                    + '(%d)' % sum (self.word_count)),
621                                   [('title',filename)])
622                      + self.texi_body (markup, numbering)),
623                     [('align','left')])
624              + self.texi_includes (markup, numbering)
625              )) + markup.paragraph ()
626
627     def texi_includes (self, markup, numbering):
628         return ''.join ([i.texi_status (markup, numbering) for i in self.includes])
629
630     def texi_body (self, markup, numbering):
631         return ''.join ([self.translations[k].short_texi_status (markup)
632                           for k in sorted (self.translations.keys ())])
633
634     def text_status (self, markup, numbering=SectionNumber (), colspec=[48,12]):
635         s = (self.print_title (numbering) + ' ').ljust (colspec[0])
636         s += ''.join (['%s'.ljust (colspec[1]) % l
637                        for l in sorted (self.translations.keys ())])
638         s += '\n'
639         s += ('Section titles (%d)' % \
640                   sum (self.word_count)).ljust (colspec[0])
641         s += self.text_body (markup, numbering, colspec)
642         s += '\n'
643         return s
644
645     def text_body (self, markup, numbering, colspec):
646         return (''.join ([self.translations[k].text_status ().ljust(colspec[1])
647                           for k in sorted (self.translations.keys ())])
648                 + '\n\n'
649                 + ''.join ([i.text_status (markup, numbering) for i in self.includes]))
650
651 class IncludedMasterTelyDocument (MasterTelyDocument):
652     get_level = TelyDocument.get_level
653
654     def translated_factory (self, filename, parent):
655         if os.path.exists (filename):
656             return IncludedTranslatedTelyDocument (filename, self, parent)
657         return IncludedUntranslatedTelyDocument (filename, self, parent)
658
659     def texi_status (self, markup, numbering=SectionNumber ()):
660         if self.title != 'Untitled':
661             return (markup.row (
662                     (markup.cell ((self.print_title (numbering)
663                                    + markup.newline ()
664                                    + '(%d)' % sum (self.word_count)),
665                                   [('title',filename)])
666                      + self.texi_body (markup, numbering)),
667                     [('align','left')])
668                     + self.texi_includes (markup, numbering))
669         return ''
670
671     def text_status (self, markup, numbering=SectionNumber (), colspec=[48,12]):
672         if self.title != 'Untitled':
673             return (self.print_title (numbering)
674                      + '(%d)' % sum (self.word_count)
675                      + self.text_body (markup, numbering, colspec)
676                     ).ljust (colspec[0])
677         return ''
678
679
680 update_category_word_counts_re = re.compile (r'(?ms)^-(\d+)-(.*?\n)\d+ *total')
681
682 counts_re = re.compile (r'(?m)^(\d+) ')
683
684 def update_category_word_counts_sub (m):
685     return ('-' + m.group (1) + '-' + m.group (2)
686             + str (sum ([int (c)
687                          for c in counts_re.findall (m.group (2))])).ljust (6)
688             + 'total')
689
690 # urg 
691 # main () starts here-abouts
692
693 progress ("Reading documents...")
694
695 master_files = \
696     buildlib.read_pipe ("git ls-files | grep -E '[^/]*/?[^/]*[.](tely|texi)$'")[0].splitlines ()
697 master_files.sort ()
698 master_docs = [MasterTelyDocument (os.path.normpath (filename))
699                for filename in master_files]
700 master_docs = [doc for doc in master_docs if doc.translations]
701
702 enabled_languages = [l for l in langdefs.LANGDICT
703                      if langdefs.LANGDICT[l].enabled
704                      and l != 'en']
705
706 progress ("Generating status pages...")
707
708 date_time = buildlib.read_pipe ('LANG= date -u')[0]
709
710 # TEXI output sort of works
711 # TODO: table border, td-titles :-)
712 # markup = HTMLMarkup ()
713 #sys.stderr.write ('''translations-status.py:713: warning: using markup = HTMLMarkup (): HTML only\n''')
714 markup = TexiMarkup ()
715 sys.stderr.write ('''translations-status.py:717: warning: using markup = TexiMarkup (): ugly HTML
716     output, questionable PDF and info output.
717     Consider using HTML-only markup = HTMLMarkup ()\n''')
718
719 main_status_body = markup.paragraph (markup.emph (last_updated_string % date_time))
720 main_status_body += '\n'.join ([doc.texi_status (markup) for doc in master_docs])
721 main_status_page = markup.texi (main_status_body)
722
723 open ('translations.itexi', 'w').write (main_status_page)
724
725 for l in enabled_languages:
726     date_time = buildlib.read_pipe ('LANG=%s date -u' % l)[0]
727     updated = markup.paragraph (markup.emph (translation[l] (last_updated_string) % date_time))
728     texi_status = '\n'.join ([doc.translations[l].texi_status (markup)
729                               for doc in master_docs
730                               if l in doc.translations])
731     lang_status_page = markup.texi (updated + texi_status)
732     open (os.path.join (l, 'translations.itexi'), 'w').write (lang_status_page)
733
734 main_status_txt = '''Documentation translations status
735 Generated %s
736 NT = not translated
737 FT = fully translated
738
739 ''' % date_time
740
741 main_status_txt += '\n'.join ([doc.text_status (markup) for doc in master_docs])
742
743 status_txt_file = 'out/translations-status.txt'
744 progress ("Writing %s..." % status_txt_file)
745 open (status_txt_file, 'w').write (main_status_txt)
746
747 translation_instructions_file = 'contributor/doc-translation-list.itexi'
748 progress ("Updating %s..." % translation_instructions_file)
749 translation_instructions = open (translation_instructions_file).read ()
750
751 for doc in master_docs:
752     translation_instructions = doc.update_word_counts (translation_instructions)
753
754 for html_file in re.findall (r'(?m)^\d+ *(\S+?\.html\S*?)(?: |$)',
755                              translation_instructions):
756     word_count = sgml_word_count (open (html_file).read ())
757     translation_instructions = update_word_count (translation_instructions,
758                                                   html_file,
759                                                   word_count)
760
761 for po_file in re.findall (r'(?m)^\d+ *(\S+?\.po\S*?)(?: |$)',
762                            translation_instructions):
763     word_count = po_word_count (open (po_file).read ())
764     translation_instructions = update_word_count (translation_instructions,
765                                                   po_file,
766                                                   word_count)
767
768 translation_instructions = \
769     update_category_word_counts_re.sub (update_category_word_counts_sub,
770                                         translation_instructions)
771
772 open (translation_instructions_file, 'w').write (translation_instructions)
773 sys.exit (exit_code)