| 1 |
#! /usr/bin/env python |
| 2 |
# |
| 3 |
# check_po - a gramps tool to check validity of po files |
| 4 |
# |
| 5 |
# Copyright (C) 2006-2006 Kees Bakker |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License along |
| 18 |
# with this program; if not, see <https://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
# |
| 21 |
# TODO |
| 22 |
# |
| 23 |
# * Check for HTML text in msgstr when there is none in msgid |
| 24 |
# * Check for matching HTML tag/endtag in msgstr |
| 25 |
# |
| 26 |
|
| 27 |
import sys |
| 28 |
import re |
| 29 |
import os |
| 30 |
import io |
| 31 |
from argparse import ArgumentParser |
| 32 |
|
| 33 |
all_total = {} |
| 34 |
all_fuzzy = {} |
| 35 |
all_untranslated = {} |
| 36 |
all_percent_s = {} |
| 37 |
all_named_s = {} |
| 38 |
all_bnamed_s = {} |
| 39 |
all_context = {} |
| 40 |
all_coverage = {} |
| 41 |
all_template_coverage = {} |
| 42 |
|
| 43 |
def strip_quotes(st): |
| 44 |
if len(st) >= 2 and st[0] == '"' and st[len(st)-1] == '"': |
| 45 |
st = st.strip()[1:-1] |
| 46 |
return st |
| 47 |
|
| 48 |
class CheckException( Exception ): |
| 49 |
pass |
| 50 |
|
| 51 |
# This is a base class for all checks |
| 52 |
class Check: |
| 53 |
def __init__( self ): |
| 54 |
self.msgs = [] |
| 55 |
def diag( self ): |
| 56 |
if len( self.msgs ): |
| 57 |
print |
| 58 |
print(self.diag_header) |
| 59 |
for m in self.msgs: |
| 60 |
m.diag() |
| 61 |
def summary( self ): |
| 62 |
print("%-20s%d" % ( self.summary_text, len(self.msgs) )) |
| 63 |
|
| 64 |
class Check_fmt( Check ): |
| 65 |
def __init__( self, fmt ): |
| 66 |
Check.__init__( self ) |
| 67 |
self.diag_header = "-------- %s mismatches --------------" % fmt |
| 68 |
self.summary_text = "%s mismatches:" % fmt |
| 69 |
self.fmt = fmt |
| 70 |
|
| 71 |
def __process( self, msg, msgid, msgstr ): |
| 72 |
cnt1 = msgid.count( self.fmt ) |
| 73 |
cnt2 = msgstr.count( self.fmt ) |
| 74 |
if cnt1 != cnt2: |
| 75 |
self.msgs.append( msg ) |
| 76 |
|
| 77 |
def process( self, msg ): |
| 78 |
msgid = msg.msgid |
| 79 |
msgstr = msg.msgstr[0] |
| 80 |
self.__process( msg, msgid, msgstr ) |
| 81 |
|
| 82 |
if msg.msgidp and len(msg.msgstr) >= 2: |
| 83 |
msgid = msg.msgidp |
| 84 |
msgstr = msg.msgstr[1] |
| 85 |
self.__process( msg, msgid, msgstr ) |
| 86 |
|
| 87 |
class Check_named_fmt( Check ): |
| 88 |
# A pattern to find all %() |
| 89 |
find_named_fmt_pat = re.compile(r'% \( \w+ \) \d* \D', re.VERBOSE) |
| 90 |
|
| 91 |
def __init__( self ): |
| 92 |
Check.__init__( self ) |
| 93 |
self.diag_header = "-------- %() name mismatches --------------" |
| 94 |
self.summary_text = "%() name mismatches:" |
| 95 |
|
| 96 |
def __process( self, msg, msgid, msgstr ): |
| 97 |
# Same number of named formats? |
| 98 |
fmts1 = self.find_named_fmt_pat.findall( msgid ) |
| 99 |
fmts2 = self.find_named_fmt_pat.findall( msgstr ) |
| 100 |
if len( fmts1 ) != len( fmts2 ): |
| 101 |
self.msgs.append( msg ) |
| 102 |
else: |
| 103 |
# Do we have the same named formats? |
| 104 |
fmts1.sort() |
| 105 |
fmts2.sort() |
| 106 |
if fmts1 != fmts2: |
| 107 |
self.msgs.append( msg ) |
| 108 |
|
| 109 |
def process( self, msg ): |
| 110 |
msgid = msg.msgid |
| 111 |
msgstr = msg.msgstr[0] |
| 112 |
self.__process( msg, msgid, msgstr ) |
| 113 |
|
| 114 |
if msg.msgidp and len(msg.msgstr) >= 2: |
| 115 |
msgid = msg.msgidp |
| 116 |
msgstr = msg.msgstr[1] |
| 117 |
self.__process( msg, msgid, msgstr ) |
| 118 |
|
| 119 |
class Check_mapping_fmt( Check ): |
| 120 |
# A pattern to find all {} |
| 121 |
find_map_pat = re.compile(r'\{\w+\.?f?\[?.*?\]?\}', |
| 122 |
re.UNICODE) # needed for Russian index letters |
| 123 |
|
| 124 |
def __init__( self ): |
| 125 |
Check.__init__( self ) |
| 126 |
self.diag_header = "-------- {} name mismatches --------------" |
| 127 |
self.summary_text = "{} name mismatches:" |
| 128 |
|
| 129 |
def __process( self, msg, msgid, msgstr ): |
| 130 |
# Same number of named formats? |
| 131 |
fmts1 = self.find_map_pat.findall( msgid ) |
| 132 |
fmts2 = self.find_map_pat.findall( msgstr ) |
| 133 |
if len( fmts1 ) != len( fmts2 ): |
| 134 |
self.msgs.append( msg ) |
| 135 |
else: |
| 136 |
# Do we have the same named formats? |
| 137 |
fmts1.sort() |
| 138 |
fmts2.sort() |
| 139 |
if fmts1 != fmts2: |
| 140 |
for idx in range(len(fmts2)): |
| 141 |
fmts2[idx] = re.sub( r'\.f\[.+\]', '', fmts2[idx] ) |
| 142 |
if fmts1 != fmts2: |
| 143 |
self.msgs.append( msg ) |
| 144 |
|
| 145 |
def process( self, msg ): |
| 146 |
msgid = msg.msgid |
| 147 |
msgstr = msg.msgstr[0] |
| 148 |
self.__process( msg, msgid, msgstr ) |
| 149 |
|
| 150 |
if msg.msgidp and len(msg.msgstr) >= 2: |
| 151 |
msgid = msg.msgidp |
| 152 |
msgstr = msg.msgstr[1] |
| 153 |
self.__process( msg, msgid, msgstr ) |
| 154 |
|
| 155 |
class Check_missing_sd( Check ): |
| 156 |
# A pattern to find %() without s or d |
| 157 |
# Here is a command to use for testing |
| 158 |
# print(re.compile('% \( \w+ \) \d* (\D|$)', re.VERBOSE).findall( '%(event_name)s: %(place)s%(endnotes)s. ' )) |
| 159 |
find_named_fmt_pat2 = re.compile(r'% \( \w+ \) \d* (\D|$)', re.VERBOSE) |
| 160 |
|
| 161 |
def __init__( self ): |
| 162 |
Check.__init__( self ) |
| 163 |
self.diag_header = "-------- %() without 's' or 'd' mismatches --------------" |
| 164 |
self.summary_text = "%() missing s/d:" |
| 165 |
def process( self, msg ): |
| 166 |
for msgstr in msg.msgstr: |
| 167 |
fmts = self.find_named_fmt_pat2.findall( msgstr ) |
| 168 |
for f in fmts: |
| 169 |
if not f in ('s', 'd'): |
| 170 |
self.msgs.append( msg ) |
| 171 |
break |
| 172 |
|
| 173 |
class Check_runaway( Check ): |
| 174 |
def __init__( self ): |
| 175 |
Check.__init__( self ) |
| 176 |
self.diag_header = "-------- Runaway context in translation ---------" |
| 177 |
self.summary_text = "Runaway context:" |
| 178 |
|
| 179 |
def __process( self, msg, msgid, msgstr ): |
| 180 |
# Runaway context. In the translated part we only want to see |
| 181 |
# the translation of the word after the | |
| 182 |
if (msgid.count('|') > 0 and msgstr.count('|') > 0 |
| 183 |
and msgid != msgstr |
| 184 |
and not (' lexeme' in msgid) # _datestrings.py keyword |
| 185 |
and not ('alternative month names ' in msgid) # this one too |
| 186 |
): |
| 187 |
self.msgs.append( msg ) |
| 188 |
|
| 189 |
def process( self, msg ): |
| 190 |
msgid = msg.msgid |
| 191 |
msgstr = msg.msgstr[0] |
| 192 |
self.__process( msg, msgid, msgstr ) |
| 193 |
|
| 194 |
if msg.msgidp and len(msg.msgstr) >= 2: |
| 195 |
msgid = msg.msgidp |
| 196 |
msgstr = msg.msgstr[1] |
| 197 |
self.__process( msg, msgid, msgstr ) |
| 198 |
|
| 199 |
class Check_xml_chars( Check ): |
| 200 |
# Special XML characters |
| 201 |
# It is not allowed to have a quote, an ampersand or an angle bracket |
| 202 |
xml_chars_pat = re.compile( r'<(?!(b>|/b>|i>|/i>|br/>)) | (?<=!(<b|/b|<i|/i|r/))> | " | & (?!(quot|nbsp|gt|amp);)', re.VERBOSE ) |
| 203 |
|
| 204 |
def __init__( self ): |
| 205 |
Check.__init__( self ) |
| 206 |
self.diag_header = "-------- unescaped XML special characters ---------" |
| 207 |
self.summary_text = "XML special chars:" |
| 208 |
|
| 209 |
def process( self, msg ): |
| 210 |
msgid = msg.msgid |
| 211 |
msgstr = msg.msgstr[0] |
| 212 |
|
| 213 |
# XML errors |
| 214 |
# Only look at messages in the tips.xml |
| 215 |
if msg.is_tips_xml: |
| 216 |
if self.xml_chars_pat.search( msgstr ): |
| 217 |
self.msgs.append( msg ) |
| 218 |
|
| 219 |
class Check_last_char( Check ): |
| 220 |
def __init__( self ): |
| 221 |
Check.__init__( self ) |
| 222 |
self.diag_header = "-------- last character not identical ---------" |
| 223 |
self.summary_text = "Last character:" |
| 224 |
|
| 225 |
def __process( self, msg, msgid, msgstr ): |
| 226 |
msgid_last = msgid[-1:] |
| 227 |
msgstr_last = msgstr[-1:] |
| 228 |
if msgid_last.isspace() != msgstr_last.isspace(): |
| 229 |
self.msgs.append( msg ) |
| 230 |
elif (msgid_last == '.') != (msgstr_last == '.' or |
| 231 |
msgstr_last == '\u3002' ): # Chinese |
| 232 |
self.msgs.append( msg ) |
| 233 |
elif (msgid_last == ':') != (msgstr_last == ':' or |
| 234 |
msgstr_last == '\uff1a' ): # Chinese |
| 235 |
self.msgs.append( msg ) |
| 236 |
elif (msgid_last == ')') != (msgstr_last == ')' or |
| 237 |
msgstr_last == '\uff09' ): # Chinese |
| 238 |
self.msgs.append( msg ) |
| 239 |
|
| 240 |
|
| 241 |
def process( self, msg ): |
| 242 |
# Last character of msgid? White space? Period? |
| 243 |
if msg.is_fuzzy: |
| 244 |
return |
| 245 |
msgid = msg.msgid |
| 246 |
msgstr = msg.msgstr[0] |
| 247 |
self.__process( msg, msgid, msgstr ) |
| 248 |
|
| 249 |
if msg.msgidp and len(msg.msgstr) >= 2: |
| 250 |
msgid = msg.msgidp |
| 251 |
msgstr = msg.msgstr[1] |
| 252 |
self.__process( msg, msgid, msgstr ) |
| 253 |
|
| 254 |
class Check_shortcut_trans( Check ): |
| 255 |
def __init__( self ): |
| 256 |
Check.__init__( self ) |
| 257 |
self.diag_header = "-------- shortcut key in translation ---------" |
| 258 |
self.summary_text = "Shortcut in msgstr:" |
| 259 |
|
| 260 |
def __process( self, msg, msgid, msgstr ): |
| 261 |
if msgid.count('_') == 0 and msgstr.count('_') > 0: |
| 262 |
self.msgs.append( msg ) |
| 263 |
|
| 264 |
def process( self, msg ): |
| 265 |
msgid = msg.msgid |
| 266 |
msgstr = msg.msgstr[0] |
| 267 |
self.__process( msg, msgid, msgstr ) |
| 268 |
|
| 269 |
if msg.msgidp and len(msg.msgstr) >= 2: |
| 270 |
msgid = msg.msgidp |
| 271 |
msgstr = msg.msgstr[1] |
| 272 |
self.__process( msg, msgid, msgstr ) |
| 273 |
|
| 274 |
class Msgid: |
| 275 |
fuzzy_pat = re.compile( 'fuzzy' ) |
| 276 |
tips_xml_pat = re.compile( r'tips\.xml' ) |
| 277 |
def __init__( self, msgnr, lineno ): |
| 278 |
self._msgctxt = [] # For debugging purpose the original text |
| 279 |
self._msgid = [] # For debugging purpose the original text |
| 280 |
self._msgidp = [] # For debugging purpose the original text |
| 281 |
self._msgstr = [] # For debugging purpose the original text |
| 282 |
self.msgctxt = '' |
| 283 |
self.msgid = '' |
| 284 |
self.msgidp = '' |
| 285 |
self.msgstr = [] # This is a list to support plural |
| 286 |
self._cmnt = [] |
| 287 |
self.nr = msgnr |
| 288 |
self.lineno = lineno |
| 289 |
self.is_fuzzy = 0 |
| 290 |
self.is_tips_xml = 0 |
| 291 |
|
| 292 |
def diag( self ): |
| 293 |
print |
| 294 |
print("msg nr: %d, lineno: %d%s" % |
| 295 |
( self.nr, self.lineno, self.is_fuzzy and " (fuzzy)" or "" )) |
| 296 |
sys.stdout.write( ''.join( self._msgid ) ) |
| 297 |
sys.stdout.write( ''.join( self._msgidp ) ) |
| 298 |
if sys.version_info[0] < 3: |
| 299 |
sys.stdout.write(''.join(self._msgstr).encode('utf-8')) |
| 300 |
else: |
| 301 |
sys.stdout.write( ''.join( self._msgstr ) ) |
| 302 |
|
| 303 |
def add_msgctxt( self, line, lineno ): |
| 304 |
self._msgctxt.append( line ) |
| 305 |
line = re.sub( r'msgctxt\s+', '', line ) |
| 306 |
line = line.strip() |
| 307 |
if line[0] != '"' or line[-1:] != '"': |
| 308 |
print("ERROR at line %d: Missing quote." % lineno) |
| 309 |
line = strip_quotes( line ) |
| 310 |
self.msgctxt += line |
| 311 |
|
| 312 |
def add_msgid( self, line, lineno ): |
| 313 |
self._msgid.append( line ) |
| 314 |
line = re.sub( r'msgid\s+', '', line ) |
| 315 |
line = line.strip() |
| 316 |
if line[0] != '"' or line[-1:] != '"': |
| 317 |
print("ERROR at line %d: Missing quote." % lineno) |
| 318 |
line = strip_quotes( line ) |
| 319 |
self.msgid += line |
| 320 |
|
| 321 |
def add_msgidp( self, line, lineno ): |
| 322 |
self._msgidp.append( line ) |
| 323 |
line = re.sub( r'msgid_plural\s+', '', line ) |
| 324 |
line = line.strip() |
| 325 |
if line[0] != '"' or line[-1:] != '"': |
| 326 |
print("ERROR at line %d: Missing quote." % lineno) |
| 327 |
line = strip_quotes( line ) |
| 328 |
self.msgidp += line |
| 329 |
|
| 330 |
def add_new_msgstr( self, line, lineno ): |
| 331 |
self.msgstr.append( '' ) # Start a new msgstr |
| 332 |
self.add_msgstr( line, lineno ) |
| 333 |
|
| 334 |
def add_msgstr( self, line, lineno ): |
| 335 |
self._msgstr.append( line ) |
| 336 |
line = re.sub( r'msgstr(\[\d\])?\s+', '', line ) |
| 337 |
line = line.strip() |
| 338 |
if line[0] != '"' or line[-1:] != '"': |
| 339 |
print("ERROR at line %d: Missing quote." % lineno) |
| 340 |
line = strip_quotes( line ) |
| 341 |
self.msgstr[-1] += line |
| 342 |
|
| 343 |
def add_cmnt( self, line ): |
| 344 |
self._cmnt.append( line ) |
| 345 |
if not self.is_fuzzy and self.fuzzy_pat.search( line ): |
| 346 |
self.is_fuzzy = 1 |
| 347 |
if not self.is_tips_xml and self.tips_xml_pat.search( line ): |
| 348 |
self.is_tips_xml = 1 |
| 349 |
|
| 350 |
def create_new_Msgid( msgs, lineno ): |
| 351 |
msg = Msgid( len(msgs), lineno ) |
| 352 |
msgs.append( msg ) |
| 353 |
return msg |
| 354 |
|
| 355 |
def read_msgs( fname ): |
| 356 |
empty_pat = re.compile( r'^ \s* $', re.VERBOSE ) |
| 357 |
comment_pat = re.compile( r'\#', re.VERBOSE ) |
| 358 |
msgctxt_pat = re.compile( r'msgctxt \s+ "', re.VERBOSE ) |
| 359 |
msgid_pat = re.compile( r'msgid \s+ "', re.VERBOSE ) |
| 360 |
msgid_plural_pat = re.compile( r'msgid_plural \s+ "', re.VERBOSE ) |
| 361 |
msgstr_pat = re.compile( r'msgstr (\[\d\])? \s+ "', re.VERBOSE ) |
| 362 |
str_pat = re.compile( r'"', re.VERBOSE ) |
| 363 |
old_pat = re.compile( r'\#~ \s+ ', re.VERBOSE ) |
| 364 |
|
| 365 |
f = io.open( fname, encoding='utf-8' ) |
| 366 |
lines = f.readlines() |
| 367 |
|
| 368 |
# parse it like a statemachine |
| 369 |
NONE = 'NONE' # Nothing detected, yet |
| 370 |
CMNT = 'CMNT' # Inside comment part |
| 371 |
MSGCTXT = 'msgctxt' # Inside msgctxt part |
| 372 |
MSGID = 'msgid' # Inside msgid part |
| 373 |
MSGIDP = 'msgid_plural' # Inside msgid_plural part |
| 374 |
MSGSTR = 'msgstr' # Inside msgstr part |
| 375 |
STR = 'STR' # A continuation string |
| 376 |
OLD = 'OLD' # An old pattern with #~ |
| 377 |
|
| 378 |
global msgs |
| 379 |
state = NONE |
| 380 |
msg = None |
| 381 |
|
| 382 |
msgs = [] |
| 383 |
for ix, line in enumerate( lines ): # Use line numbers for messages |
| 384 |
lineno = ix + 1 |
| 385 |
|
| 386 |
m = empty_pat.match( line ) |
| 387 |
if m: |
| 388 |
continue # Empty lines are not interesting |
| 389 |
|
| 390 |
# What's the next state? |
| 391 |
if old_pat.match( line ): |
| 392 |
next_state = OLD |
| 393 |
elif comment_pat.match( line ): |
| 394 |
next_state = CMNT |
| 395 |
elif msgctxt_pat.match( line ): |
| 396 |
next_state = MSGCTXT |
| 397 |
elif msgid_pat.match( line ): |
| 398 |
next_state = MSGID |
| 399 |
elif msgid_plural_pat.match( line ): |
| 400 |
next_state = MSGIDP |
| 401 |
elif msgstr_pat.match( line ): |
| 402 |
next_state = MSGSTR |
| 403 |
elif str_pat.match( line ): |
| 404 |
next_state = STR |
| 405 |
else: |
| 406 |
print('WARNING: Unexpected input at %(fname)s:%(lineno)d' % vars()) |
| 407 |
next_state = NONE |
| 408 |
|
| 409 |
#print("%(state)d->%(next_state)d\t%(line)s" % vars()) |
| 410 |
if state == NONE: |
| 411 |
# expect msgctxt, msgid, comment or old stuff |
| 412 |
if next_state == CMNT: |
| 413 |
state = CMNT |
| 414 |
# Start with an empty new item |
| 415 |
msg = create_new_Msgid( msgs, lineno ) |
| 416 |
msg.add_cmnt( line ) |
| 417 |
|
| 418 |
elif next_state == MSGCTXT: |
| 419 |
state = MSGCTXT |
| 420 |
# Start with an empty new item |
| 421 |
msg = create_new_Msgid( msgs, lineno ) |
| 422 |
msg.add_msgctxt( line, lineno ) |
| 423 |
|
| 424 |
elif next_state == MSGID: |
| 425 |
state = MSGID |
| 426 |
# Start with an empty new item |
| 427 |
msg = create_new_Msgid( msgs, lineno ) |
| 428 |
msg.add_msgid( line, lineno ) |
| 429 |
|
| 430 |
elif next_state == MSGIDP: |
| 431 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 432 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 433 |
|
| 434 |
elif next_state == MSGSTR: |
| 435 |
print('WARNING: Wild msgstr at %(fname)s:%(lineno)d' % vars()) |
| 436 |
state = MSGSTR |
| 437 |
# Start with an empty new item |
| 438 |
msg = create_new_Msgid( msgs, lineno ) |
| 439 |
msg.add_new_msgstr( line, lineno ) |
| 440 |
|
| 441 |
elif next_state == STR: |
| 442 |
print('WARNING: Wild string at %(fname)s:%(lineno)d' % vars()) |
| 443 |
|
| 444 |
elif next_state == OLD: |
| 445 |
pass # Just skip |
| 446 |
|
| 447 |
else: |
| 448 |
raise CheckException( 'Unexpected state in po parsing ' |
| 449 |
'(state = %(state)s)' % vars() ) |
| 450 |
|
| 451 |
elif state == CMNT: |
| 452 |
# Expect more comment, msgctxt, or msgid. |
| 453 |
# If msgstr or string it is flagged as error. |
| 454 |
if next_state == CMNT: |
| 455 |
if msg: |
| 456 |
msg.add_cmnt( line ) |
| 457 |
else: |
| 458 |
# Note. We may need to do something about these comments |
| 459 |
# Skip for now |
| 460 |
pass |
| 461 |
|
| 462 |
elif next_state == MSGCTXT: |
| 463 |
state = MSGCTXT |
| 464 |
if not msg: |
| 465 |
# Start with an empty new item |
| 466 |
msg = create_new_Msgid( msgs, lineno ) |
| 467 |
msg.add_msgctxt( line, lineno ) |
| 468 |
|
| 469 |
elif next_state == MSGID: |
| 470 |
state = MSGID |
| 471 |
if not msg: |
| 472 |
# Start with an empty new item |
| 473 |
msg = create_new_Msgid( msgs, lineno ) |
| 474 |
msg.add_msgid( line, lineno ) |
| 475 |
|
| 476 |
elif next_state == MSGIDP: |
| 477 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 478 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 479 |
|
| 480 |
elif next_state == MSGSTR: |
| 481 |
print('WARNING: Wild msgstr at %(fname)s:%(lineno)d' % vars()) |
| 482 |
state = MSGSTR |
| 483 |
# Start with an empty new item |
| 484 |
msg = create_new_Msgid( msgs, lineno ) |
| 485 |
msg.add_new_msgstr( line, lineno ) |
| 486 |
|
| 487 |
elif next_state == STR: |
| 488 |
print('WARNING: Wild string at %(fname)s:%(lineno)d' % vars()) |
| 489 |
|
| 490 |
elif next_state == OLD: |
| 491 |
msg = None |
| 492 |
pass # Just skip |
| 493 |
|
| 494 |
else: |
| 495 |
raise CheckException( 'Unexpected state in po parsing ' |
| 496 |
'(state = %(state)s)' % vars() ) |
| 497 |
elif state == MSGCTXT: |
| 498 |
# Expect more msgctxt or msgid. |
| 499 |
# If msgstr or string it is flagged as error. |
| 500 |
if next_state == CMNT: |
| 501 |
# Hmmm. A comment here? |
| 502 |
print('WARNING: Unexpted comment ' |
| 503 |
'at %(fname)s:%(lineno)d' % vars()) |
| 504 |
|
| 505 |
elif next_state == MSGCTXT: |
| 506 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 507 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 508 |
|
| 509 |
elif next_state == MSGID: |
| 510 |
state = MSGID |
| 511 |
msg.add_msgid( line, lineno ) |
| 512 |
|
| 513 |
elif next_state == MSGIDP: |
| 514 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 515 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 516 |
|
| 517 |
elif next_state == MSGSTR: |
| 518 |
print('WARNING: Wild msgstr at %(fname)s:%(lineno)d' % vars()) |
| 519 |
state = MSGSTR |
| 520 |
# Start with an empty new item |
| 521 |
msg = create_new_Msgid( msgs, lineno ) |
| 522 |
msg.add_new_msgstr( line, lineno ) |
| 523 |
|
| 524 |
elif next_state == STR: |
| 525 |
print('WARNING: Wild string at %(fname)s:%(lineno)d' % vars()) |
| 526 |
|
| 527 |
elif next_state == STR: |
| 528 |
# Continuation of msgctxt, stay in state MSGCTXT |
| 529 |
msg.add_msgctxt( line, lineno ) |
| 530 |
|
| 531 |
else: |
| 532 |
raise CheckException( 'Unexpected state in po parsing ' |
| 533 |
'(state = %(state)s)' % vars() ) |
| 534 |
|
| 535 |
elif state == MSGID: |
| 536 |
# Expect msgstr or msgid_plural or string |
| 537 |
if next_state == CMNT: |
| 538 |
# Hmmm. A comment here? |
| 539 |
print('WARNING: Unexpted comment ' |
| 540 |
'at %(fname)s:%(lineno)d' % vars()) |
| 541 |
|
| 542 |
elif next_state == MSGCTXT: |
| 543 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 544 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 545 |
|
| 546 |
elif next_state == MSGID: |
| 547 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 548 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 549 |
|
| 550 |
elif next_state == MSGIDP: |
| 551 |
state = MSGIDP |
| 552 |
msg.add_msgidp( line, lineno ) |
| 553 |
|
| 554 |
elif next_state == MSGSTR: |
| 555 |
state = MSGSTR |
| 556 |
msg.add_new_msgstr( line, lineno ) |
| 557 |
|
| 558 |
elif next_state == STR: |
| 559 |
# Continuation of msgid, stay in state MSGID |
| 560 |
msg.add_msgid( line, lineno ) |
| 561 |
|
| 562 |
elif next_state == OLD: |
| 563 |
msg = None |
| 564 |
pass # Just skip |
| 565 |
|
| 566 |
else: |
| 567 |
raise CheckException( 'Unexpected state in po parsing ' |
| 568 |
'(state = %(state)s)' % vars() ) |
| 569 |
|
| 570 |
elif state == MSGIDP: |
| 571 |
# Expect msgstr or string or comment |
| 572 |
if next_state == CMNT: |
| 573 |
# Hmmm. A comment here? |
| 574 |
print('WARNING: Unexpected comment ' |
| 575 |
'at %(fname)s:%(lineno)d' % vars()) |
| 576 |
|
| 577 |
elif next_state == MSGCTXT: |
| 578 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 579 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 580 |
|
| 581 |
elif next_state == MSGID: |
| 582 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 583 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 584 |
|
| 585 |
elif next_state == MSGIDP: |
| 586 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 587 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 588 |
|
| 589 |
elif next_state == MSGSTR: |
| 590 |
state = MSGSTR |
| 591 |
msg.add_new_msgstr( line, lineno ) |
| 592 |
|
| 593 |
elif next_state == STR: |
| 594 |
# Continuation of msgid_plural, stay in state MSGIDP |
| 595 |
msg.add_msgidp( line, lineno ) |
| 596 |
|
| 597 |
elif next_state == OLD: |
| 598 |
msg = None |
| 599 |
pass # Just skip |
| 600 |
|
| 601 |
else: |
| 602 |
raise CheckException( 'Unexpected state in po parsing ' |
| 603 |
'(state = %(state)s)' % vars() ) |
| 604 |
|
| 605 |
elif state == MSGSTR: |
| 606 |
# Expect comment, or msgid, or string. |
| 607 |
if next_state == CMNT: |
| 608 |
# A comment probably starts a new item |
| 609 |
state = CMNT |
| 610 |
msg = create_new_Msgid( msgs, lineno ) |
| 611 |
msg.add_cmnt( line ) |
| 612 |
|
| 613 |
elif next_state == MSGCTXT: |
| 614 |
state = MSGCTXT |
| 615 |
msg = create_new_Msgid( msgs, lineno ) |
| 616 |
msg.add_msgctxt( line, lineno ) |
| 617 |
|
| 618 |
elif next_state == MSGID: |
| 619 |
state = MSGID |
| 620 |
msg = create_new_Msgid( msgs, lineno ) |
| 621 |
msg.add_msgid( line, lineno ) |
| 622 |
|
| 623 |
elif next_state == MSGIDP: |
| 624 |
raise CheckException( 'Unexpected %(next_state)s ' |
| 625 |
'at %(fname)s:%(lineno)d' % vars() ) |
| 626 |
|
| 627 |
elif next_state == MSGSTR: |
| 628 |
# New msgstr, probably for plural form |
| 629 |
# Stay in MSGSTR state |
| 630 |
msg.add_new_msgstr( line, lineno ) |
| 631 |
|
| 632 |
elif next_state == STR: |
| 633 |
msg.add_msgstr( line, lineno ) |
| 634 |
|
| 635 |
elif next_state == OLD: |
| 636 |
msg = None |
| 637 |
pass # Just skip |
| 638 |
|
| 639 |
else: |
| 640 |
raise CheckException( 'Unexpected state in po parsing ' |
| 641 |
'(state = %(state)s)' % vars() ) |
| 642 |
|
| 643 |
else: |
| 644 |
raise CheckException( 'Unexpected state in po parsing ' |
| 645 |
'(state = %(state)s)' % vars() ) |
| 646 |
|
| 647 |
# Strip items with just comments. (Can this happen?) |
| 648 |
msgs1 = [] |
| 649 |
for m in msgs: |
| 650 |
if not m.msgid and not m.msgstr: |
| 651 |
#print("INFO: No msgid or msgstr at %s:%s" % ( fname, m.lineno )) |
| 652 |
pass |
| 653 |
else: |
| 654 |
msgs1.append( m ) |
| 655 |
msgs = msgs1 |
| 656 |
return msgs |
| 657 |
|
| 658 |
def analyze_msgs( args, fname, msgs, nr_templates = None, nth = 0 ): |
| 659 |
nr_fuzzy = 0 |
| 660 |
nr_untranslated = 0 |
| 661 |
|
| 662 |
checks = [] |
| 663 |
checks.append( Check_fmt( '%s' ) ) |
| 664 |
checks.append( Check_fmt( '%d' ) ) |
| 665 |
checks.append( Check_named_fmt() ) |
| 666 |
checks.append( Check_mapping_fmt() ) |
| 667 |
checks.append( Check_missing_sd() ) |
| 668 |
checks.append( Check_runaway() ) |
| 669 |
checks.append( Check_xml_chars() ) |
| 670 |
checks.append( Check_last_char() ) |
| 671 |
checks.append( Check_shortcut_trans() ) |
| 672 |
|
| 673 |
for msg in msgs: |
| 674 |
msgid = msg.msgid |
| 675 |
msgstr = msg.msgstr |
| 676 |
#print |
| 677 |
#print("msgid: %(msgid)s" % vars()) |
| 678 |
#print("msgstr: %(msgstr)s" % vars()) |
| 679 |
|
| 680 |
if ''.join(msgstr) == '': |
| 681 |
nr_untranslated += 1 |
| 682 |
continue |
| 683 |
|
| 684 |
if msg.is_fuzzy: |
| 685 |
nr_fuzzy += 1 |
| 686 |
continue |
| 687 |
|
| 688 |
for c in checks: |
| 689 |
c.process( msg ) |
| 690 |
|
| 691 |
nr_msgs = len(msgs) |
| 692 |
if nth > 0: |
| 693 |
print |
| 694 |
print("=====================================") |
| 695 |
print("%-20s%s" % ( "File:", fname )) |
| 696 |
print("%-20s%d" % ( "Template total:", nr_templates )) |
| 697 |
print("%-20s%d" % ( "PO total:", nr_msgs )) |
| 698 |
print("%-20s%d" % ( "Fuzzy:", nr_fuzzy )) |
| 699 |
print("%-20s%d" % ( "Untranslated:", nr_untranslated )) |
| 700 |
|
| 701 |
for c in checks: |
| 702 |
c.summary() |
| 703 |
|
| 704 |
po_coverage = (1.0 - (float(nr_untranslated) / float(nr_msgs))) * 100 |
| 705 |
print("%-20s%5.2f%%" % ( "PO Coverage:", po_coverage )) |
| 706 |
|
| 707 |
template_coverage = po_coverage * float(nr_msgs) / float(nr_templates) |
| 708 |
print("%-20s%5.2f%%" % ( "Template Coverage:", template_coverage )) |
| 709 |
|
| 710 |
not_displayed = nr_untranslated + nr_fuzzy |
| 711 |
translation = (1.0 - (float(not_displayed) / float(nr_templates))) * 100 |
| 712 |
text = "%-20s%5.2f%%" % ( "Localized at:", translation) |
| 713 |
|
| 714 |
if int(template_coverage*1000) == int(po_coverage*1000): |
| 715 |
print(text) |
| 716 |
else: |
| 717 |
print(text + ' (previous gramps.pot)') |
| 718 |
|
| 719 |
for c in checks: |
| 720 |
c.diag() |
| 721 |
|
| 722 |
def main(): |
| 723 |
|
| 724 |
parser = ArgumentParser(description='This program validates ' |
| 725 |
'a PO file for GRAMPS.') |
| 726 |
|
| 727 |
parser.add_argument("-s", dest="summary", |
| 728 |
choices=[file for file in os.listdir('.') |
| 729 |
if file.endswith('.po')], |
| 730 |
default=False, |
| 731 |
help="the summary of check, " |
| 732 |
"and if need, it gives details") |
| 733 |
|
| 734 |
args = parser.parse_args() |
| 735 |
|
| 736 |
if args.summary: |
| 737 |
files = sys.argv[2:] |
| 738 |
|
| 739 |
try: |
| 740 |
pot_msgs = read_msgs( 'gramps.pot' ) |
| 741 |
nr_templates = len( pot_msgs ) |
| 742 |
nth = 0 |
| 743 |
for fname in files: |
| 744 |
msgs = read_msgs( fname ) |
| 745 |
analyze_msgs( files, fname, msgs, nr_templates, nth ) |
| 746 |
nth += 1 |
| 747 |
|
| 748 |
except CheckException as e: |
| 749 |
print('Oops.', e) |
| 750 |
print('Bailing out') |
| 751 |
|
| 752 |
if __name__ == "__main__": |
| 753 |
main() |