]> git.lyx.org Git - lyx.git/blob - po/postats.py
Unify the handling of converters that specify the resultfile= flag.
[lyx.git] / po / postats.py
1 #! /usr/bin/env python
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
30 # modify this when you change branch
31 # Note that an empty lyx_branch variable (ie svn trunk)
32 # will "do the right thing".
33 lyx_branch=""
34 # these po-files will be skipped:
35 ommitted = ('en.po')
36
37 import os
38 import sys
39
40 # Reset the locale
41 import locale
42 locale.setlocale(locale.LC_ALL, 'C') 
43 os.environ['LC_ALL'] = 'C'
44
45 def extract_number(line, issues, prop):
46     """
47     line is a string like
48     '588 translated messages, 1248 fuzzy translations, 2 untranslated messages.'
49     Any one of these substrings may not appear if the associated number is 0.
50
51     issues is the set of words following the number to be extracted,
52     ie, 'translated', 'fuzzy', or 'untranslated'.
53
54     extract_number returns a list with those numbers, or sets it to
55     zero if the word is not found in the string.
56     """
57
58     for issue in issues:
59         i = line.find(issue)
60
61         if i == -1:
62             prop[issue] = 0
63         else:
64             prop[issue] = int(line[:i].split()[-1])
65
66
67 def read_pofile(pofile):
68     """ Read the header of the pofile and return it as a dictionary"""
69     header = {}
70     read_header = False
71     for line in open(pofile):
72         line = line[:-1]
73         if line[:5] == 'msgid':
74             if read_header:
75                 break
76             read_header = True
77             continue
78
79         if not line or line[0] == '#' or line == 'msgstr ""' or not read_header:
80             continue
81
82         line = line.strip('"')
83         args = line.split(': ')
84         if len(args) == 1:
85             continue
86         header[args[0]] = args[1].strip()[:-2]
87
88     return header
89
90
91 def run_msgfmt(pofile):
92     """ pofile is the name of the po file.
93  The function runs msgfmt on it and returns corresponding php code.
94 """
95     if not pofile.endswith('.po'):
96         print >> sys.stderr, "%s is not a po file" % pofile
97         sys.exit(1)
98
99     dirname = os.path.dirname(pofile)
100     gmofile = pofile.replace('.po', '.gmo')
101
102     header = read_pofile(pofile)
103     charset= header['Content-Type'].split('charset=')[1]
104
105     # po file properties
106     prop = {}
107     prop["langcode"] = os.path.basename(pofile)[:-3]
108     prop["date"] = header['PO-Revision-Date'].split()[0]
109     prop["email"] = header['Last-Translator'].split('<')[1][:-1]
110     prop["email"] = prop["email"].replace("@", " () ")
111     prop["email"] = prop["email"].replace(".", " ! ")
112     translator = header['Last-Translator'].split('<')[0].strip()
113     try:
114         prop["translator"] = translator.decode(charset).encode('ascii','xmlcharrefreplace')
115     except LookupError:
116         prop["translator"] = translator
117
118     p_in, p_out = os.popen4("msgfmt --statistics -o %s %s" % (gmofile, pofile))
119     extract_number(p_out.readline(),
120                    ('translated', 'fuzzy', 'untranslated'),
121                    prop)
122     return """
123 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
124 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
125 "translator" => "%(translator)s", "email" => "%(email)s")""" % prop
126
127
128 if __name__ == "__main__":
129     if lyx_branch:
130         branch_tag = "branches/%s" % lyx_branch
131     else:
132         branch_tag = "trunk"
133
134
135     print """<?php
136 // The current version
137 $lyx_version = "%s";
138 // The branch tag
139 $branch_tag = "%s";
140
141 // The data itself
142 $podata = array (%s
143 )?>""" % (sys.argv[1], branch_tag, ",".join([run_msgfmt(po) for po in sys.argv[2:] if po not in ommitted]))