fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / odf2po.py
blob4db41fbb0d3cc59aff45859bcd1f091e8e561315
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2004-2006 Zuza Software Foundation
5 #
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 """convert OpenDocument (ODF) files to Gettext PO localization files"""
25 from translate.storage import po
26 from translate.storage import odf
28 class odf2po:
29 def convertstore(self, inputfile):
30 """converts a file to .po format"""
31 thetargetfile = po.pofile()
32 filename = getattr(inputfile, "name", "unkown")
33 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit")
34 targetheader.addnote("extracted from %s\n" % filename, "developer")
35 thetargetfile.addunit(targetheader)
36 odfdoc = odf.ODFFile(inputfile)
37 blocknum = 0
38 for unit in odfdoc.getunits():
39 if not unit: continue
40 blocknum += 1
41 newunit = thetargetfile.addsourceunit(unit.source)
42 newunit.addlocations("%s:%d" % (filename, blocknum))
43 return thetargetfile
45 def convertodf(inputfile, outputfile, templates):
46 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
47 convertor = odf2po()
48 outputstore = convertor.convertstore(inputfile)
49 if outputstore.isempty():
50 return 0
51 outputfile.write(str(outputstore))
52 return 1
54 def main(argv=None):
55 from translate.convert import convert
56 formats = {"sxw":("po", convertodf), "odt":("po", convertodf), "ods":("po", convertodf), "odp":("po", convertodf)}
57 parser = convert.ConvertOptionParser(formats, description=__doc__)
58 parser.run(argv)
61 if __name__ == '__main__':
62 main()