Lines 1-7
Link Here
|
1 |
#!/usr/bin/python |
1 |
#!/usr/bin/env python |
2 |
# |
2 |
# |
3 |
# This quick hack gives translation statistics about the various sheets. |
3 |
# This quick hack gives translation statistics about the various sheets. |
4 |
# |
4 |
# |
|
|
5 |
# Copyright (C) 2016, Ilia Skalozubov <freebsd@skinc.ru> |
5 |
# Copyright (C) 2001, Cyrille Chepelov <chepelov@calixo.net> |
6 |
# Copyright (C) 2001, Cyrille Chepelov <chepelov@calixo.net> |
6 |
# |
7 |
# |
7 |
# This quick hack is free software; you can redistribute it and/or modify |
8 |
# This quick hack is free software; you can redistribute it and/or modify |
Lines 19-195
Link Here
|
19 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
20 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
20 |
# |
21 |
# |
21 |
|
22 |
|
|
|
23 |
import os |
24 |
import sys |
25 |
import xml.sax |
22 |
|
26 |
|
23 |
import os,sys,string |
|
|
24 |
try : |
25 |
from xml.sax import saxexts |
26 |
from xml.sax import saxlib |
27 |
except ImportError : |
28 |
print "Missing dependencies: no translation report" |
29 |
sys.exit(0) |
30 |
|
27 |
|
31 |
class CounterHandler(saxlib.DocumentHandler): |
28 |
class CounterHandler(xml.sax.handler.ContentHandler): |
32 |
|
29 |
|
33 |
def __init__(self): |
30 |
def __init__(self): |
34 |
self.elemstk = [] |
31 |
self.elemstk = [] |
35 |
self.langstk = [] |
32 |
self.langstk = [] |
36 |
self.namestk = [] |
33 |
self.namestk = [] |
37 |
|
34 |
|
38 |
def setDocumentLocator(self,locator): |
35 |
def setDocumentLocator(self, locator): |
39 |
self.locator = locator |
36 |
self.locator = locator |
40 |
saxlib.DocumentHandler.setDocumentLocator(self,locator) |
37 |
xml.sax.handler.ContentHandler.setDocumentLocator(self, locator) |
41 |
self.langs = {} |
38 |
self.langs = {} |
42 |
self.namelangs = {} |
39 |
self.namelangs = {} |
43 |
|
40 |
|
44 |
def warning(self,message): |
41 |
def warning(self, message): |
45 |
print "W:%s:L%d:C%d: %s" % (self.locator.getSystemId(), |
42 |
print "W:%s:L%d:C%d: %s" % (self.locator.getSystemId(), |
46 |
self.locator.getLineNumber(), |
43 |
self.locator.getLineNumber(), |
47 |
self.locator.getColumnNumber(), |
44 |
self.locator.getColumnNumber(), |
48 |
message) |
45 |
message) |
49 |
def _countlang(self,name): |
46 |
|
|
|
47 |
def _countlang(self, name): |
50 |
locdct = self.langstk[-1] |
48 |
locdct = self.langstk[-1] |
51 |
if locdct.has_key(name): |
49 |
if name in locdct: |
52 |
self.warning("duplicate description for %s, language code %s" % \ |
50 |
self.warning("duplicate description for %s, language code %s" % |
53 |
(self.namestk[-1],name)) |
51 |
(self.namestk[-1], name)) |
54 |
locdct[name] = locdct[name] + 1 |
52 |
locdct[name] = locdct[name] + 1 |
55 |
else: |
53 |
else: |
56 |
locdct[name] = 1 |
54 |
locdct[name] = 1 |
57 |
|
55 |
|
58 |
if self.langs.has_key(name): |
56 |
if name in self.langs: |
59 |
self.langs[name] = self.langs[name] + 1 |
57 |
self.langs[name] = self.langs[name] + 1 |
60 |
else: |
58 |
else: |
61 |
self.langs[name] = 1 |
59 |
self.langs[name] = 1 |
62 |
|
60 |
|
63 |
def _countnamelang(self,name): |
61 |
def _countnamelang(self, name): |
64 |
if self.namelangs.has_key(name): |
62 |
if name in self.namelangs: |
65 |
self.warning("duplicate name for sheet, language code %s" % name) |
63 |
self.warning("duplicate name for sheet, language code %s" % name) |
66 |
self.namelangs[name] = self.namelangs[name] + 1 |
64 |
self.namelangs[name] = self.namelangs[name] + 1 |
67 |
else: |
65 |
else: |
68 |
self.namelangs[name] = 1 |
66 |
self.namelangs[name] = 1 |
69 |
|
67 |
|
70 |
if self.langs.has_key(name): |
68 |
if name in self.langs: |
71 |
self.langs[name] = self.langs[name] + 1 |
69 |
self.langs[name] = self.langs[name] + 1 |
72 |
else: |
70 |
else: |
73 |
self.langs[name] = 1 |
71 |
self.langs[name] = 1 |
74 |
|
72 |
|
75 |
def startElement(self,name,attrs): |
73 |
def startElement(self, name, attrs): |
76 |
#print "start of ",name,attrs,attrs.map |
74 |
attmap = dict(attrs.items()) |
77 |
attmap = attrs.map |
|
|
78 |
self.elemstk.append(name) |
75 |
self.elemstk.append(name) |
79 |
if (name == "sheet") or (name == "object"): |
76 |
if (name == "sheet") or (name == "object"): |
80 |
self.langstk.append({}) |
77 |
self.langstk.append({}) |
81 |
if attmap.has_key('name'): |
78 |
if "name" in attmap: |
82 |
name = 'Object "%s"' % attmap['name'] |
79 |
name = 'Object "%s"' % attmap["name"] |
83 |
else: |
80 |
else: |
84 |
name = 'Sheet "%s"' % self.locator.getSystemId() |
81 |
name = 'Sheet "%s"' % self.locator.getSystemId() |
85 |
self.namestk.append(name) |
82 |
self.namestk.append(name) |
86 |
elif (name == "name"): |
83 |
elif (name == "name"): |
87 |
if attmap.has_key("xml:lang"): |
84 |
if "xml:lang" in attmap: |
88 |
lang = attmap["xml:lang"] |
85 |
lang = attmap["xml:lang"] |
89 |
else: |
86 |
else: |
90 |
lang = "" |
87 |
lang = "" |
91 |
self._countnamelang(lang) |
88 |
self._countnamelang(lang) |
92 |
elif (name == "description"): |
89 |
elif (name == "description"): |
93 |
if attmap.has_key("xml:lang"): |
90 |
if "xml:lang" in attmap: |
94 |
lang = attmap["xml:lang"] |
91 |
lang = attmap["xml:lang"] |
95 |
else: |
92 |
else: |
96 |
lang = "" |
93 |
lang = "" |
97 |
self._countlang(lang) |
94 |
self._countlang(lang) |
98 |
|
95 |
|
99 |
def endElement(self,name): |
96 |
def endElement(self, name): |
100 |
popped = self.elemstk.pop() |
97 |
popped = self.elemstk.pop() |
101 |
if popped != name: |
98 |
if popped != name: |
102 |
raise Exception("stack error somewhere...") |
99 |
raise Exception("stack error somewhere...") |
103 |
if (name == "sheet") or (name == "object"): |
100 |
if (name == "sheet") or (name == "object"): |
104 |
res = self.langstk.pop() |
101 |
res = self.langstk.pop() |
105 |
self.namestk.pop() |
102 |
self.namestk.pop() |
106 |
#print "end of",name, res |
|
|
107 |
else: |
103 |
else: |
108 |
#print "end of ",name |
|
|
109 |
pass |
104 |
pass |
110 |
|
105 |
|
111 |
## class BasicEntityResolver(saxlib.EntityResolver): |
106 |
if len(sys.argv) < 2: |
112 |
## def resolveEntity(name,publicId,systemId): |
107 |
print "Usage: %s <sheet.sheet>" % sys.argv[0] |
113 |
## print "D:resolveEntity(%s,%s,%s)" % (name,publicId,systemId), |
|
|
114 |
## if publicId == "-//FOO//BAR//EN": |
115 |
## res = "file:../doc/sheet.dtd" |
116 |
## else: |
117 |
## res = saxlib.EntityResolver.resolveEntity(name,publicId,systemId) |
118 |
|
119 |
## print "--> res",res,type(res) |
120 |
|
121 |
## return res |
122 |
|
123 |
if len(sys.argv)<2: |
124 |
print "Usage: %s <sheet.sheet>" % sys.argv[0] |
125 |
print |
108 |
print |
126 |
print " <sheet.sheet>: file name of the sheet to check" |
109 |
print " <sheet.sheet>: file name of the sheet to check" |
127 |
sys.exit(1) |
110 |
sys.exit(1) |
128 |
|
111 |
|
129 |
# Load parser and driver |
112 |
# Load parser and driver |
|
|
113 |
p = xml.sax.make_parser() |
114 |
ch = CounterHandler() |
115 |
p.setContentHandler(ch) |
116 |
|
117 |
fnames = sys.argv[1:] |
130 |
|
118 |
|
131 |
p=saxexts.make_parser() |
119 |
|
132 |
#p=saxexts.XMLValParserFactory.make_parser() |
120 |
def make_langresult(langdict, colsize): |
133 |
ch=CounterHandler() |
121 |
langres = map(lambda s, colsize=colsize: s.ljust(colsize), |
134 |
p.setDocumentHandler(ch) |
122 |
map(lambda (cc, count), total=langdict['']: |
135 |
#p.setEntityResolver(BasicEntityResolver()) |
123 |
("%s:%d%%" % (cc, 100*count/total)), |
136 |
|
124 |
filter(lambda (cc, count): cc, langdict.items()))) |
137 |
fnames =sys.argv[1:] |
|
|
138 |
|
139 |
def make_langresult(langdict,colsize): |
140 |
langres = map(lambda s,colsize=colsize: string.ljust(s,colsize), |
141 |
map(lambda (cc,count),total=langdict['']: |
142 |
("%s:%d%%" % (cc,100*count/total)), |
143 |
filter(lambda (cc,count): cc, langdict.items()))) |
144 |
langres.sort() |
125 |
langres.sort() |
145 |
return langres |
126 |
return langres |
146 |
|
127 |
|
147 |
def maxlen(a,b): |
128 |
|
148 |
if len(a)>len(b): return a |
129 |
def maxlen(a, b): |
|
|
130 |
if len(a) > len(b): |
131 |
return a |
149 |
return b |
132 |
return b |
150 |
namelen = len(reduce(maxlen,fnames,"")) |
133 |
namelen = len(reduce(maxlen, fnames, "")) |
151 |
|
134 |
|
152 |
globlangs = {} |
135 |
globlangs = {} |
153 |
|
136 |
|
154 |
for name in fnames: |
137 |
for name in fnames: |
155 |
OK=0 |
138 |
OK = 0 |
156 |
try: |
139 |
try: |
157 |
p.parse("file://localhost" + os.getcwd() + "/" + name) |
140 |
p.parse("file://localhost" + os.getcwd() + "/" + name) |
158 |
OK=1 |
141 |
OK = 1 |
159 |
|
142 |
|
160 |
maxlanglen = len(reduce(maxlen,ch.langs.keys(),"")) |
143 |
maxlanglen = len(reduce(maxlen, ch.langs.keys(), "")) |
161 |
langres = make_langresult(ch.langs,maxlanglen+5) |
144 |
langres = make_langresult(ch.langs, maxlanglen + 5) |
162 |
columns = (79 - (4 + namelen)) / (maxlanglen+5) |
145 |
columns = (79 - (4 + namelen)) / (maxlanglen + 5) |
163 |
#print "maxlanglen = ",maxlanglen,"columns=",columns |
|
|
164 |
|
146 |
|
165 |
langres1,langrest = langres[:columns],langres[columns:] |
147 |
langres1, langrest = langres[:columns], langres[columns:] |
166 |
sys.stdout.write(("I: %%%ds %%s\n" % namelen) % |
148 |
sys.stdout.write(("I: %%%ds %%s\n" % namelen) % |
167 |
(name,string.join(langres1))) |
149 |
(name, " ".join(langres1))) |
168 |
while langrest: |
150 |
while langrest: |
169 |
langresn,langrest = langrest[:columns],langrest[columns:] |
151 |
langresn, langrest = langrest[:columns], langrest[columns:] |
170 |
sys.stdout.write("I: %s %s\n" % (' ' * namelen, |
152 |
sys.stdout.write("I: %s %s\n" % (' ' * namelen, |
171 |
string.join(langresn))) |
153 |
" ".join(langresn))) |
172 |
|
154 |
|
173 |
for (k,v) in ch.langs.items(): |
155 |
for (k, v) in ch.langs.items(): |
174 |
if globlangs.has_key(k): |
156 |
if k in globlangs: |
175 |
globlangs[k] = globlangs[k] + v |
157 |
globlangs[k] = globlangs[k] + v |
176 |
else: |
158 |
else: |
177 |
globlangs[k] = v |
159 |
globlangs[k] = v |
178 |
|
160 |
|
179 |
except IOError,e: |
161 |
except IOError, e: |
180 |
sys.stderr.write("E: %s: %s\n" % (name,str(e))) |
162 |
sys.stderr.write("E: %s: %s\n" % (name, str(e))) |
181 |
except saxlib.SAXException,e: |
163 |
except xml.sax.SAXException, e: |
182 |
sys.stderr.write("E: %s\n" % str(e)) |
164 |
sys.stderr.write("E: %s\n" % str(e)) |
183 |
|
165 |
|
184 |
maxlanglen = len(reduce(maxlen,globlangs.keys(),"")) |
166 |
maxlanglen = len(reduce(maxlen, globlangs.keys(), "")) |
185 |
langres = make_langresult(globlangs,maxlanglen+5) |
167 |
langres = make_langresult(globlangs, maxlanglen + 5) |
186 |
columns = (79 - 12) / (maxlanglen+5) |
168 |
columns = (79 - 12) / (maxlanglen + 5) |
187 |
|
169 |
|
188 |
langres1,langrest = langres[:columns],langres[columns:] |
170 |
langres1, langrest = langres[:columns], langres[columns:] |
189 |
sys.stdout.write("\nI: OVERALL: %s\n" % string.join(langres1)) |
171 |
sys.stdout.write("\nI: OVERALL: %s\n" % " ".join(langres1)) |
190 |
while langrest: |
172 |
while langrest: |
191 |
langresn,langrest = langrest[:columns],langrest[columns:] |
173 |
langresn, langrest = langrest[:columns], langrest[columns:] |
192 |
sys.stdout.write("I: %s %s\n" % (' ' * 8, |
174 |
sys.stdout.write("I: %s %s\n" % (' ' * 8, |
193 |
string.join(langresn))) |
175 |
" ".join(langresn))) |
194 |
|
|
|
195 |
|