File: po-checktrans.py

package info (click to toggle)
dia 0.88.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,772 kB
  • ctags: 9,541
  • sloc: ansic: 95,284; sh: 9,405; makefile: 1,521; python: 593; yacc: 318
file content (112 lines) | stat: -rwxr-xr-x 3,229 bytes parent folder | download
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
#!/usr/bin/python
#
# This quick hack gives translation statistics (from the core translation
# files).
#
# Copyright (C) 2001, Cyrille Chepelov <chepelov@calixo.net>
#
# This quick hack 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 quick hack 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 quick hack; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import string,os,sys,math

collen = 1

def NoneStr(n):
    if n is None: return (" " * collen)
    return n

def slurp_po(fname):
    """returns a tuple (language_name,ids,translations) of what has
    been translated."""
    base,ext = os.path.splitext(os.path.basename(fname))
    language_name = base

    idents = 0
    translations = 0
    
    st = ""
    id = ""
    
    fd = open(fname,"r")
    while 1:
        s = fd.readline()
        if not s: break

        s = string.strip(string.split(s,'#')[0])
        if not s: continue # empty line.
        
        sl = string.split(s)
        if sl[0] == "msgid":
            #print "id",id,"st",st
            if st:
                translations = translations + 1
            id = sl[0]
            st = ""
            del sl[0]
        elif sl[0] == "msgstr":
            #print "id",id,"st",st
            if st:
                idents = idents + 1
            id = sl[0]
            st = ""
            del sl[0]
            
        for k in sl:
            if k != '""':
                st = st + k
    #print "translations:",translations,"idents:",idents
    return (language_name, idents, translations)

if len(sys.argv)<3:
    print "Usage: %s <package.pot> <lang.po> ..." % sys.argv[0]
    print
    print " <package.pot>: file name of the identifier reference to check."
    print " <lang.po>: file name of the translation to check"
    sys.exit(1)

def maxlen(a,b):
    if len(a) > len(b):
        return a
    return b

(t,idents,n) = slurp_po(sys.argv[1])
del t,n

translations = map(slurp_po,sys.argv[2:])
trans = map(lambda (l,i,t),ti=idents: "%s:%3d%%(%d/%d)%s"%(l,
                                                           100*float(t)/idents,
                                                           t,idents,
                                                           "*" * (idents != i)),
            translations)
maxlanglen = len(reduce(maxlen,trans,""))
trans = map(lambda s,mll=maxlanglen: string.ljust(s,mll),trans)
lt = len(trans)

collen = maxlanglen + len("  ")

numcols = int(79 / collen)
ltnc = int(lt/numcols)
cols = []
while trans:
    c,trans = trans[:ltnc],trans[ltnc:]
    cols.append(c)

lines = apply(map,tuple([None]+cols))

result = string.join(map(lambda l:string.join(map(NoneStr,list(l)),"  "),lines),
                     "\n")
print result