]> git.lyx.org Git - features.git/blob - po/postats.py
Merge branch 'master' of git.lyx.org:lyx
[features.git] / po / postats.py
1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007 Michael Gerz <michael.gerz@teststep.org>
4 # Copyright (C) 2007 José Matos <jamatos@lyx.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 """
21 This script extracts some information from the po file headers (last
22 translator, revision date), generates the corresponding gmo files
23 to retrieve the number of translated/fuzzy/untranslated messages,
24 and generates a PHP web page.
25
26 Invocation:
27    postats.py lyx_version po_files > "pathToWebPages"/i18n.inc
28 """
29 from __future__ import print_function
30
31 # modify this when you change branch
32 # Note that an empty lyx_branch variable (ie svn trunk)
33 # will "do the right thing".
34 lyx_branch=""
35 # these po-files will be skipped:
36 ommitted = ('en.po')
37
38 import os
39 import sys
40 import codecs
41 from subprocess import Popen, PIPE
42
43 # Reset the locale
44 import locale
45 locale.setlocale(locale.LC_ALL, 'C')
46 os.environ['LC_ALL'] = 'C'
47
48 def extract_number(line, issues, prop):
49     """
50     line is a string like
51     '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
52     Any one of these substrings may not appear if the associated number is 0.
53
54     issues is the set of words following the number to be extracted,
55     ie, 'translated', 'fuzzy', or 'untranslated'.
56
57     extract_number returns a list with those numbers, or sets it to
58     zero if the word is not found in the string.
59     """
60
61     for issue in issues:
62         i = line.find(issue)
63
64         if i == -1:
65             prop[issue] = 0
66         else:
67             prop[issue] = int(line[:i].split()[-1])
68
69
70 def read_pofile(pofile):
71     """ Read the header of the pofile and return it as a dictionary"""
72     header = {}
73     read_header = False
74     for line in codecs.open(pofile, encoding='utf8'):
75         line = line[:-1]
76         if line[:5] == 'msgid':
77             if read_header:
78                 break
79             read_header = True
80             continue
81
82         if not line or line[0] == '#' or line == 'msgstr ""' or not read_header:
83             continue
84
85         line = line.strip('"')
86         args = line.split(': ')
87         if len(args) == 1:
88             continue
89         header[args[0]] = args[1].strip()[:-2]
90
91     return header
92
93
94 def run_msgfmt(pofile):
95     """ pofile is the name of the po file.
96  The function runs msgfmt on it and returns corresponding php code.
97 """
98     if not pofile.endswith('.po'):
99         print("%s is not a po file" % pofile, file=sys.stderr)
100         sys.exit(1)
101
102     dirname = os.path.dirname(pofile)
103     gmofile = pofile.replace('.po', '.gmo')
104
105     header = read_pofile(pofile)
106     charset= header['Content-Type'].split('charset=')[1]
107
108     # po file properties
109     prop = {}
110     prop["langcode"] = os.path.basename(pofile)[:-3]
111     prop["date"] = header['PO-Revision-Date'].split()[0]
112     prop["email"] = header['Last-Translator'].split('<')[1][:-1]
113     prop["email"] = prop["email"].replace("@", " () ")
114     prop["email"] = prop["email"].replace(".", " ! ")
115     translator = header['Last-Translator'].split('<')[0].strip()
116     try:
117         prop["translator"] = translator.encode('ascii','xmlcharrefreplace')
118     except LookupError:
119         prop["translator"] = translator
120
121     P = Popen("msgfmt --statistics -o %s %s" % (gmofile, pofile),
122               shell=True, stdin=PIPE, stdout=PIPE, close_fds=True)
123     extract_number(P.stdout.readline().decode(),
124                    ('translated', 'fuzzy', 'untranslated'),
125                    prop)
126     return """
127 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
128 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
129 "translator" => "%(translator)s", "email" => "%(email)s")""" % prop
130
131
132 if __name__ == "__main__":
133     if lyx_branch:
134         branch_tag = lyx_branch
135     else:
136         branch_tag = "master"
137
138
139     print("""<?php
140 // The current version
141 $lyx_version = "%s";
142 // The branch tag
143 $branch_tag = "%s";
144
145 // The data itself
146 $podata = array (%s
147 )?>""" % (sys.argv[1], branch_tag, ",".join([run_msgfmt(po) for po in sys.argv[2:] if po not in ommitted])))