]> git.lyx.org Git - lyx.git/blob - po/postats.py
Backport changes to postats.py
[lyx.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 import subprocess
42 from subprocess import Popen, PIPE
43
44 # Reset the locale
45 import locale
46 locale.setlocale(locale.LC_ALL, 'C')
47 os.environ['LC_ALL'] = 'C'
48
49 def extract_number(line, issues, prop):
50     """
51     line is a string like
52     '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
53     Any one of these substrings may not appear if the associated number is 0.
54
55     issues is the set of words following the number to be extracted,
56     ie, 'translated', 'fuzzy', or 'untranslated'.
57
58     extract_number returns a list with those numbers, or sets it to
59     zero if the word is not found in the string.
60     """
61
62     for issue in issues:
63         i = line.find(issue)
64
65         if i == -1:
66             prop[issue] = 0
67         else:
68             prop[issue] = int(line[:i].split()[-1])
69
70
71 def read_pofile(pofile):
72     """ Read the header of the pofile and return it as a dictionary"""
73     header = {}
74     read_header = False
75     for line in codecs.open(pofile, encoding='utf8'):
76         line = line[:-1]
77         if line[:5] == 'msgid':
78             if read_header:
79                 break
80             read_header = True
81             continue
82
83         if not line or line[0] == '#' or line == 'msgstr ""' or not read_header:
84             continue
85
86         line = line.strip('"')
87         args = line.split(': ')
88         if len(args) == 1:
89             continue
90         header[args[0]] = args[1].strip()[:-2]
91
92     return header
93
94
95 def run_msgfmt(pofile):
96     """ pofile is the name of the po file.
97  The function runs msgfmt on it and returns corresponding php code.
98 """
99     if not pofile.endswith('.po'):
100         print("%s is not a po file" % pofile, file=sys.stderr)
101         sys.exit(1)
102
103     dirname = os.path.dirname(pofile)
104     gmofile = pofile.replace('.po', '.gmo')
105
106     header = read_pofile(pofile)
107     charset= header['Content-Type'].split('charset=')[1]
108
109     # po file properties
110     prop = {}
111     prop["langcode"] = os.path.basename(pofile)[:-3]
112     prop["date"] = header['PO-Revision-Date'].split()[0]
113     prop["email"] = header['Last-Translator'].split('<')[1][:-1]
114     prop["email"] = prop["email"].replace("@", " () ")
115     prop["email"] = prop["email"].replace(".", " ! ")
116     prop["translator"] = header['Last-Translator'].split('<')[0].strip()
117
118     msg = subprocess.check_output(["msgfmt", "--statistics",
119             "-o", gmofile, # FIXME: do we really want a gmofile as side-effect?
120             pofile], stderr=subprocess.STDOUT)
121     if sys.version_info[0] > 2:
122         msg = msg.decode('utf8')
123     extract_number(msg, ('translated', 'fuzzy', 'untranslated'), prop)
124     return """
125 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
126 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
127 "translator" => "%(translator)s", "email" => "%(email)s")""" % prop
128
129
130 if __name__ == "__main__":
131     if lyx_branch:
132         branch_tag = lyx_branch
133     else:
134         branch_tag = "master"
135
136
137     print("""<?php
138 // The current version
139 $lyx_version = "%s";
140 // The branch tag
141 $branch_tag = "%s";
142
143 // The data itself
144 $podata = array (%s
145 )?>""" % (sys.argv[1], branch_tag, 
146           ",".join([run_msgfmt(po) for po in sys.argv[2:] 
147                                    if po not in ommitted])))