1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#!/usr/bin/python3
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENSE BLOCK *****
# <pep8 compliant>
# Check po’s in branches (or in trunk) for missing/unneeded messages.
import os
import sys
from codecs import open
try:
import settings
import utils
except:
from . import (settings, utils)
TRUNK_PO_DIR = settings.TRUNK_PO_DIR
BRANCHES_DIR = settings.BRANCHES_DIR
FILE_NAME_POT = settings.FILE_NAME_POT
def print_diff(ref_messages, messages, states):
# Remove comments from messages list!
messages = set(messages.keys()) - states["comm_msg"]
unneeded = (messages - ref_messages)
for msgid in unneeded:
print('\tUnneeded message id "{}"'.format(msgid))
missing = (ref_messages - messages)
for msgid in missing:
print('\tMissing message id "{}"'.format(msgid))
for msgid in states["comm_msg"]:
print('\tCommented message id "{}"'.format(msgid))
print("\t{} unneeded messages, {} missing messages, {} commented messages." \
"".format(len(unneeded), len(missing), len(states["comm_msg"])))
return 0
def process_po(ref_messages, po, glob_stats, do_stats, do_messages):
print("Checking {}...".format(po))
ret = 0
messages, states, stats = utils.parse_messages(po)
if do_messages:
t = print_diff(ref_messages, messages, states)
if t:
ret = t
if do_stats:
print("\tStats:")
t = utils.print_stats(stats, glob_stats, prefix=" ")
if t:
ret = t
if states["is_broken"]:
print("\tERROR! This .po is broken!")
ret = 1
return ret
def main():
import argparse
parser = argparse.ArgumentParser(description="Check po’s in branches " \
"(or in trunk) for missing" \
"/unneeded messages.")
parser.add_argument('-s', '--stats', action="store_true",
help="Print po’s stats.")
parser.add_argument('-m', '--messages', action="store_true",
help="Print po’s missing/unneeded/commented messages.")
parser.add_argument('-t', '--trunk', action="store_true",
help="Check po’s in /trunk/po rather than /branches.")
parser.add_argument('-p', '--pot',
help="Specify the .pot file used as reference.")
parser.add_argument('langs', metavar='ISO_code', nargs='*',
help="Restrict processed languages to those.")
args = parser.parse_args()
if args.pot:
global FILE_NAME_POT
FILE_NAME_POT = args.pot
glob_stats = {"nbr" : 0.0,
"lvl" : 0.0,
"lvl_ttips" : 0.0,
"lvl_trans_ttips" : 0.0,
"lvl_ttips_in_trans": 0.0,
"lvl_comm" : 0.0,
"nbr_signs" : 0,
"nbr_trans_signs" : 0,
"contexts" : set()}
ret = 0
pot_messages = None
if args.messages:
pot_messages, u1, pot_stats = utils.parse_messages(FILE_NAME_POT)
pot_messages = set(pot_messages.keys())
glob_stats["nbr_signs"] = pot_stats["nbr_signs"]
if args.langs:
for lang in args.langs:
if args.trunk:
po = os.path.join(TRUNK_PO_DIR, ".".join((lang, "po")))
else:
po = os.path.join(BRANCHES_DIR, lang, ".".join((lang, "po")))
if os.path.exists(po):
t = process_po(pot_messages, po, glob_stats,
args.stats, args.messages)
if t:
ret = t
elif args.trunk:
for po in os.listdir(TRUNK_PO_DIR):
if po.endswith(".po"):
po = os.path.join(TRUNK_PO_DIR, po)
t = process_po(pot_messages, po, glob_stats,
args.stats, args.messages)
if t:
ret = t
else:
for lang in os.listdir(BRANCHES_DIR):
for po in os.listdir(os.path.join(BRANCHES_DIR, lang)):
if po.endswith(".po"):
po = os.path.join(BRANCHES_DIR, lang, po)
t = process_po(pot_messages, po, glob_stats,
args.stats, args.messages)
if t:
ret = t
if args.stats and glob_stats["nbr"] != 0.0:
nbr_contexts = len(glob_stats["contexts"]-{""})
if nbr_contexts != 1:
if nbr_contexts == 0:
nbr_contexts = "No"
_ctx_txt = "s are"
else:
_ctx_txt = " is"
print("\nAverage stats for all {:.0f} processed files:\n" \
" {:>6.1%} done!\n" \
" {:>6.1%} of messages are tooltips.\n" \
" {:>6.1%} of tooltips are translated.\n" \
" {:>6.1%} of translated messages are tooltips.\n" \
" {:>6.1%} of messages are commented.\n" \
" The org msgids are currently made of {} signs.\n" \
" All processed translations are currently made of {} signs.\n" \
" {} specific context{} present:\n {}\n" \
"".format(glob_stats["nbr"], glob_stats["lvl"]/glob_stats["nbr"],
glob_stats["lvl_ttips"]/glob_stats["nbr"],
glob_stats["lvl_trans_ttips"]/glob_stats["nbr"],
glob_stats["lvl_ttips_in_trans"]/glob_stats["nbr"],
glob_stats["lvl_comm"]/glob_stats["nbr"], glob_stats["nbr_signs"],
glob_stats["nbr_trans_signs"], nbr_contexts, _ctx_txt,
"\n ".join(glob_stats["contexts"]-{""})))
return ret
if __name__ == "__main__":
print("\n\n *** Running {} *** \n".format(__file__))
print(" *** WARNING! Number of tooltips is only an estimation! ***\n")
sys.exit(main())
|