]> git.lyx.org Git - lyx.git/blob - po/postats.py
Move Lexer to support/ directory (and lyx::support namespace)
[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 (e.g. stats for stable branch).
32 # Note that an empty lyx_branch variable will "do the right thing" for master.
33 lyx_branch=""
34 # these po-files will be skipped:
35 ommitted = ('en.po')
36
37 import os
38 import sys
39 import codecs
40 import subprocess
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     prop["translator"] = header['Last-Translator'].split('<')[0].strip()
116
117     msg = subprocess.check_output(["msgfmt", "--statistics",
118             "-o", gmofile, # FIXME: do we really want a gmofile as side-effect?
119             pofile], stderr=subprocess.STDOUT)
120     if sys.version_info[0] > 2:
121         msg = msg.decode('utf8')
122     extract_number(msg, ('translated', 'fuzzy', 'untranslated'), prop)
123     return """
124 array ( 'langcode' => '%(langcode)s', "date" => "%(date)s",
125 "msg_tr" => %(translated)d, "msg_fu" => %(fuzzy)d, "msg_nt" => %(untranslated)d,
126 "translator" => "%(translator)s", "email" => "%(email)s")""" % prop
127
128
129 if __name__ == "__main__":
130     if lyx_branch:
131         branch_tag = lyx_branch
132     else:
133         branch_tag = "master"
134
135
136     print("""<?php
137 // The current version
138 $lyx_version = "%s";
139 // The branch tag
140 $branch_tag = "%s";
141
142 // The data itself
143 $podata = array (%s
144 )?>""" % (sys.argv[1], branch_tag, 
145           ",".join([run_msgfmt(po) for po in sys.argv[2:] 
146                                    if po not in ommitted])))