"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.70/tools/import-launchpad-translations.py" (14 Mar 2019, 2288 Bytes) of package /linux/privat/zim-0.70.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "import-launchpad-translations.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 #!/usr/bin/python3
    2 
    3 import sys
    4 import tarfile
    5 import re
    6 
    7 MIN_TRANLATIONS = 300
    8 
    9 def count_messages(file):
   10     return count('msgid', file) + 1
   11     # The +1 is for the first message, which is empty and translates
   12     # with all meta data
   13 
   14 def count_translations(file):
   15     return count('msgstr', file)
   16 
   17 def count(prefix, file):
   18     count = 0
   19     l = len(prefix)
   20     check_multiline = False # track multiline messages
   21     for line in file:
   22         if line.startswith(prefix):
   23             string = line[l:].strip().strip('"')
   24             if string:
   25                 count += 1
   26                 check_multiline = False
   27             else:
   28                 check_multiline = True
   29                 # Can still be start of multiline block ..
   30         elif check_multiline \
   31         and line.startswith('"'):
   32             count += 1
   33             check_multiline = False
   34         else:
   35             check_multiline = False
   36     return count
   37 
   38 def get_lang(name):
   39     match = re.search('zim-(.*?).po$', name)
   40     assert match, 'Could not parse LANG from %s !?' % name
   41     return match.group(1)
   42 
   43 def import_translations_from(archive):
   44     tfile = tarfile.open(archive, 'r:gz')
   45     names = tfile.getnames()
   46     def read_file(name):
   47         return [l.decode('UTF-8') for l in tfile.extractfile(name)]
   48 
   49     potfiles = [n for n in names if n.endswith('.pot')]
   50     assert len(potfiles) == 1, 'Multiple template files in this archive !?'
   51     total = count_messages(read_file(potfiles[0]))
   52     print('%i messages in catalogue' % total)
   53 
   54     pofiles = []
   55     for name in [n for n in names if n.endswith('.po')]:
   56         lang = get_lang(name)
   57         file = read_file(name)
   58         n = count_translations(file)
   59         pofiles.append((n, lang, file))
   60 
   61     files = []
   62     for n, lang, file in pofiles:
   63         perc = float(n) / total * 100
   64         if n >= MIN_TRANLATIONS:
   65             status = 'OK'
   66             files.append(('translations/%s.po' % lang, file))
   67         else:
   68             status = ''
   69         print('%-6s %i translated (%i%%) %s' % (lang, n, perc, status))
   70 
   71     for path, file in files:
   72         print('Writing %s' % path)
   73         open(path, 'w').writelines(file)
   74 
   75     print('\nPlease check `git status` for newly added translations and update CHANGELOG.md')
   76     print('You need to run `./setup.py build_trans` to use the newly imported po files')
   77 
   78 if __name__ == '__main__':
   79     assert len(sys.argv) == 2 and sys.argv[1].endswith('.tar.gz')
   80     import_translations_from(sys.argv[1])
   81 
   82     import sys
   83     sys.path.insert(0, '.')
   84     from tests.translations import TestTranslations
   85     TestTranslations().runTest(verbose=True)