/usr/share/pyshared/translate/tools/pocount.py is in translate-toolkit 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2003-2009 Zuza Software Foundation
#
# This file is part of the Translate Toolkit.
#
# 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, see <http://www.gnu.org/licenses/>.
"""Count strings and words for supported localization files.
These include: XLIFF, TMX, Gettex PO and MO, Qt .ts and .qm, Wordfast TM, etc
See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/pocount.html
for examples and usage instructions.
"""
from optparse import OptionParser
import os
import sys
from translate.storage import factory
from translate.storage import statsdb
# define style constants
style_full, style_csv, style_short_strings, style_short_words = range(4)
# default output style
default_style = style_full
def calcstats_old(filename):
"""This is the previous implementation of calcstats() and is left for
comparison and debuging purposes."""
# ignore totally blank or header units
try:
store = factory.getobject(filename)
except ValueError, e:
print str(e)
return {}
units = filter(lambda unit: unit.istranslatable(), store.units)
translated = translatedmessages(units)
fuzzy = fuzzymessages(units)
review = filter(lambda unit: unit.isreview(), units)
untranslated = untranslatedmessages(units)
wordcounts = dict(map(lambda unit: (unit, statsdb.wordsinunit(unit)), units))
sourcewords = lambda elementlist: sum(map(lambda unit: wordcounts[unit][0], elementlist))
targetwords = lambda elementlist: sum(map(lambda unit: wordcounts[unit][1], elementlist))
stats = {}
#units
stats["translated"] = len(translated)
stats["fuzzy"] = len(fuzzy)
stats["untranslated"] = len(untranslated)
stats["review"] = len(review)
stats["total"] = stats["translated"] + \
stats["fuzzy"] + \
stats["untranslated"]
#words
stats["translatedsourcewords"] = sourcewords(translated)
stats["translatedtargetwords"] = targetwords(translated)
stats["fuzzysourcewords"] = sourcewords(fuzzy)
stats["untranslatedsourcewords"] = sourcewords(untranslated)
stats["reviewsourcewords"] = sourcewords(review)
stats["totalsourcewords"] = stats["translatedsourcewords"] + \
stats["fuzzysourcewords"] + \
stats["untranslatedsourcewords"]
return stats
def calcstats(filename):
statscache = statsdb.StatsCache()
return statscache.filetotals(filename, extended=True)
def summarize(title, stats, style=style_full, indent=8, incomplete_only=False):
"""Print summary for a .po file in specified format.
:param title: name of .po file
:param stats: array with translation statistics for the file specified
:param indent: indentation of the 2nd column (length of longest filename)
:param incomplete_only: omit fully translated files
:type incomplete_only: Boolean
:rtype: Boolean
:return: 1 if counting incomplete files (incomplete_only=True) and the
file is completely translated, 0 otherwise
"""
def percent(denominator, devisor):
if devisor == 0:
return 0
else:
return denominator * 100 / devisor
if incomplete_only and (stats["total"] == stats["translated"]):
return 1
if (style == style_csv):
print "%s, " % title,
print "%d, %d, %d," % (stats["translated"],
stats["translatedsourcewords"],
stats["translatedtargetwords"]),
print "%d, %d," % (stats["fuzzy"], stats["fuzzysourcewords"]),
print "%d, %d," % (stats["untranslated"],
stats["untranslatedsourcewords"]),
print "%d, %d" % (stats["total"], stats["totalsourcewords"]),
if stats["review"] > 0:
print ", %d, %d" % (stats["review"], stats["reviewsourdcewords"]),
print
elif (style == style_short_strings):
spaces = " " * (indent - len(title))
print "%s%s strings: total: %d\t| %dt\t%df\t%du\t| %d%%t\t%d%%f\t%d%%u" % (title, spaces, \
stats["total"], stats["translated"], stats["fuzzy"], stats["untranslated"], \
percent(stats["translated"], stats["total"]), \
percent(stats["fuzzy"], stats["total"]), \
percent(stats["untranslated"], stats["total"]))
elif (style == style_short_words):
spaces = " " * (indent - len(title))
print "%s%s source words: total: %d\t| %dt\t%df\t%du\t| %d%%t\t%d%%f\t%d%%u" % (title, spaces, \
stats["totalsourcewords"], stats["translatedsourcewords"], stats["fuzzysourcewords"], stats["untranslatedsourcewords"], \
percent(stats["translatedsourcewords"], stats["totalsourcewords"]), \
percent(stats["fuzzysourcewords"], stats["totalsourcewords"]), \
percent(stats["untranslatedsourcewords"], stats["totalsourcewords"]))
else: # style == style_full
print title
print "type strings words (source) words (translation)"
print "translated: %5d (%3d%%) %10d (%3d%%) %15d" % \
(stats["translated"], \
percent(stats["translated"], stats["total"]), \
stats["translatedsourcewords"], \
percent(stats["translatedsourcewords"], stats["totalsourcewords"]), \
stats["translatedtargetwords"])
print "fuzzy: %5d (%3d%%) %10d (%3d%%) n/a" % \
(stats["fuzzy"], \
percent(stats["fuzzy"], stats["total"]), \
stats["fuzzysourcewords"], \
percent(stats["fuzzysourcewords"], stats["totalsourcewords"]))
print "untranslated: %5d (%3d%%) %10d (%3d%%) n/a" % \
(stats["untranslated"], \
percent(stats["untranslated"], stats["total"]), \
stats["untranslatedsourcewords"], \
percent(stats["untranslatedsourcewords"], stats["totalsourcewords"]))
print "Total: %5d %17d %22d" % \
(stats["total"], \
stats["totalsourcewords"], \
stats["translatedtargetwords"])
if "extended" in stats:
print ""
for state, e_stats in stats["extended"].iteritems():
print "%s: %5d (%3d%%) %10d (%3d%%) %15d" % (
state, e_stats["units"], percent(e_stats["units"], stats["total"]),
e_stats["sourcewords"], percent(e_stats["sourcewords"], stats["totalsourcewords"]),
e_stats["targetwords"])
if stats["review"] > 0:
print "review: %5d %17d n/a" % \
(stats["review"], stats["reviewsourcewords"])
print
return 0
def fuzzymessages(units):
return filter(lambda unit: unit.isfuzzy() and unit.target, units)
def translatedmessages(units):
return filter(lambda unit: unit.istranslated(), units)
def untranslatedmessages(units):
return filter(lambda unit: not (unit.istranslated() or unit.isfuzzy()) and unit.source, units)
class summarizer:
def __init__(self, filenames, style=default_style, incomplete_only=False):
self.totals = {}
self.filecount = 0
self.longestfilename = 0
self.style = style
self.incomplete_only = incomplete_only
self.complete_count = 0
if (self.style == style_csv):
print "Filename, Translated Messages, Translated Source Words, Translated \
Target Words, Fuzzy Messages, Fuzzy Source Words, Untranslated Messages, \
Untranslated Source Words, Total Message, Total Source Words, \
Review Messages, Review Source Words"
if (self.style == style_short_strings or self.style == style_short_words):
for filename in filenames: # find longest filename
if (len(filename) > self.longestfilename):
self.longestfilename = len(filename)
for filename in filenames:
if not os.path.exists(filename):
print >> sys.stderr, "cannot process %s: does not exist" % filename
continue
elif os.path.isdir(filename):
self.handledir(filename)
else:
self.handlefile(filename)
if self.filecount > 1 and (self.style == style_full):
if self.incomplete_only:
summarize("TOTAL (incomplete only):", self.totals,
incomplete_only=True)
print "File count (incomplete): %5d" % (self.filecount - self.complete_count)
else:
summarize("TOTAL:", self.totals, incomplete_only=False)
print "File count: %5d" % (self.filecount)
print
def updatetotals(self, stats):
"""Update self.totals with the statistics in stats."""
for key in stats.keys():
if key == "extended":
#FIXME: calculate extended totals
continue
if not key in self.totals:
self.totals[key] = 0
self.totals[key] += stats[key]
def handlefile(self, filename):
try:
stats = calcstats(filename)
self.updatetotals(stats)
self.complete_count += summarize(filename, stats, self.style,
self.longestfilename,
self.incomplete_only)
self.filecount += 1
except: # This happens if we have a broken file.
print >> sys.stderr, sys.exc_info()[1]
def handlefiles(self, dirname, filenames):
for filename in filenames:
pathname = os.path.join(dirname, filename)
if os.path.isdir(pathname):
self.handledir(pathname)
else:
self.handlefile(pathname)
def handledir(self, dirname):
path, name = os.path.split(dirname)
if name in ["CVS", ".svn", "_darcs", ".git", ".hg", ".bzr"]:
return
entries = os.listdir(dirname)
self.handlefiles(dirname, entries)
def main():
parser = OptionParser(usage="usage: %prog [options] po-files")
parser.add_option("--incomplete", action="store_const", const=True,
dest="incomplete_only",
help="skip 100% translated files.")
# options controlling output format:
parser.add_option("--full", action="store_const", const=style_csv,
dest="style_full",
help="(default) statistics in full, verbose format")
parser.add_option("--csv", action="store_const", const=style_csv,
dest="style_csv",
help="statistics in CSV format")
parser.add_option("--short", action="store_const", const=style_csv,
dest="style_short_strings",
help="same as --short-strings")
parser.add_option("--short-strings", action="store_const",
const=style_csv, dest="style_short_strings",
help="statistics of strings in short format - one line per file")
parser.add_option("--short-words", action="store_const",
const=style_csv, dest="style_short_words",
help="statistics of words in short format - one line per file")
(options, args) = parser.parse_args()
if (options.incomplete_only == None):
options.incomplete_only = False
if (options.style_full and options.style_csv) or \
(options.style_full and options.style_short_strings) or \
(options.style_full and options.style_short_words) or \
(options.style_csv and options.style_short_strings) or \
(options.style_csv and options.style_short_words) or \
(options.style_short_strings and options.style_short_words):
parser.error("options --full, --csv, --short-strings and --short-words are mutually exclusive")
sys.exit(2)
style = default_style # default output style
if options.style_csv:
style = style_csv
if options.style_full:
style = style_full
if options.style_short_strings:
style = style_short_strings
if options.style_short_words:
style = style_short_words
try:
import psyco
psyco.full()
except Exception:
pass
summarizer(args, style, options.incomplete_only)
if __name__ == '__main__':
main()
|