1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Perform quality checks on Gettext PO, XLIFF and TMX localization files
22
23 Snippet files whenever a test fails. These can be examined, corrected and
24 merged back into the originals using pomerge
25
26 See: http://translate.sourceforge.net/wiki/toolkit/pofilter for examples and
27 usage instructions and http://translate.sourceforge.net/wiki/toolkit/pofilter_tests
28 for full descriptions of all tests
29 """
30
31 from translate.storage import factory
32 from translate.filters import checks
33 from translate.filters import autocorrect
34 from translate.misc import optrecurse
35 import os
36
38 - def __init__(self, options, checkerclasses=None, checkerconfig=None):
50
52 """lists the docs for filters available on checker..."""
53 filterdict = self.checker.getfilters()
54 filterdocs = ["%s\t%s" % (name, filterfunc.__doc__) for (name, filterfunc) in filterdict.iteritems()]
55 filterdocs.sort()
56 return "\n".join(filterdocs)
57
74
76 """Runs filters on a translation store object.
77 Parameters:
78 - transfile. A translation store object.
79 Return value:
80 - A new translation store object with the results of the filter included."""
81 newtransfile = type(transfile)()
82 if self.options.includeheader and newtransfile.units > 0:
83 newtransfile.addunit(transfile.header())
84 for unit in transfile.units:
85 filterresult = self.filterunit(unit)
86 if filterresult:
87 if filterresult != autocorrect:
88 for filtername, filtermessage in filterresult.iteritems():
89 if self.options.addnotes:
90 unit.adderror(filtername, filtermessage)
91 if isinstance(filtermessage, checks.SeriousFilterFailure):
92 unit.markfuzzy()
93 newtransfile.addunit(unit)
94 return newtransfile
95
97 """a specialized Option Parser for filter tools..."""
99 """construct the specialized Option Parser"""
100 optrecurse.RecursiveOptionParser.__init__(self, formats)
101 self.set_usage()
102 self.add_option("-l", "--listfilters", action="callback", dest='listfilters',
103 default=False, callback_kwargs={'dest_value': True},
104 callback=self.parse_noinput, help="list filters available")
105
110
149
150 -def runfilter(inputfile, outputfile, templatefile, checkfilter=None):
151 """reads in inputfile, filters using checkfilter, writes to outputfile"""
152 fromfile = factory.getobject(inputfile)
153 tofile = checkfilter.filterfile(fromfile)
154 if tofile.isempty():
155 return 0
156 outputfile.write(str(tofile))
157 return 1
158
160 formats = {"po":("po", runfilter), "pot":("pot", runfilter),
161 "xliff":("xliff", runfilter), "xlf":("xlf", runfilter),
162 "tmx":("tmx", runfilter),
163 None:("po", runfilter)}
164
165 parser = FilterOptionParser(formats)
166 parser.add_option("", "--review", dest="includereview",
167 action="store_true", default=True,
168 help="include units marked for review (default)")
169 parser.add_option("", "--noreview", dest="includereview",
170 action="store_false", default=True,
171 help="exclude units marked for review")
172 parser.add_option("", "--fuzzy", dest="includefuzzy",
173 action="store_true", default=True,
174 help="include units marked fuzzy (default)")
175 parser.add_option("", "--nofuzzy", dest="includefuzzy",
176 action="store_false", default=True,
177 help="exclude units marked fuzzy")
178 parser.add_option("", "--header", dest="includeheader",
179 action="store_true", default=False,
180 help="include a PO header in the output")
181 parser.add_option("", "--nonotes", dest="addnotes",
182 action="store_false", default=True,
183 help="don't add notes about the errors")
184 parser.add_option("", "--autocorrect", dest="autocorrect",
185 action="store_true", default=False,
186 help="output automatic corrections where possible rather than describing issues")
187 parser.add_option("", "--language", dest="targetlanguage", default=None,
188 help="set target language code (e.g. af-ZA) [required for spell check and recommended in general]", metavar="LANG")
189 parser.add_option("", "--openoffice", dest="filterclass",
190 action="store_const", default=None, const=checks.OpenOfficeChecker,
191 help="use the standard checks for OpenOffice translations")
192 parser.add_option("", "--mozilla", dest="filterclass",
193 action="store_const", default=None, const=checks.MozillaChecker,
194 help="use the standard checks for Mozilla translations")
195 parser.add_option("", "--drupal", dest="filterclass",
196 action="store_const", default=None, const=checks.DrupalChecker,
197 help="use the standard checks for Drupal translations")
198 parser.add_option("", "--gnome", dest="filterclass",
199 action="store_const", default=None, const=checks.GnomeChecker,
200 help="use the standard checks for Gnome translations")
201 parser.add_option("", "--kde", dest="filterclass",
202 action="store_const", default=None, const=checks.KdeChecker,
203 help="use the standard checks for KDE translations")
204 parser.add_option("", "--wx", dest="filterclass",
205 action="store_const", default=None, const=checks.KdeChecker,
206 help="use the standard checks for wxWidgets translations")
207 parser.add_option("", "--excludefilter", dest="excludefilters",
208 action="append", default=[], type="string", metavar="FILTER",
209 help="don't use FILTER when filtering")
210 parser.add_option("-t", "--test", dest="limitfilters",
211 action="append", default=None, type="string", metavar="FILTER",
212 help="only use test FILTERs specified with this option when filtering")
213 parser.add_option("", "--notranslatefile", dest="notranslatefile",
214 default=None, type="string", metavar="FILE",
215 help="read list of untranslatable words from FILE (must not be translated)")
216 parser.add_option("", "--musttranslatefile", dest="musttranslatefile",
217 default=None, type="string", metavar="FILE",
218 help="read list of translatable words from FILE (must be translated)")
219 parser.add_option("", "--validcharsfile", dest="validcharsfile",
220 default=None, type="string", metavar="FILE",
221 help="read list of all valid characters from FILE (must be in UTF-8)")
222 parser.passthrough.append('checkfilter')
223 parser.description = __doc__
224 return parser
225
227 parser = cmdlineparser()
228 parser.run()
229
230 if __name__ == '__main__':
231 main()
232