1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """convert OpenDocument (ODF) files to XLIFF localization files"""
24
25 from translate.storage import factory
26 from translate.misc.contextlib import contextmanager
27 from translate.misc.context import with_
28 from translate.storage import odf_io
29
30
31 -def convertodf(inputfile, outputfile, templates, engine='toolkit'):
48
49 def itools_implementation(store):
50 from itools.handlers import get_handler
51 from itools.gettext.po import encode_source
52 import itools.odf
53
54 filename = getattr(inputfile, 'name', 'unkown')
55 handler = get_handler(filename)
56
57 try:
58 get_units = handler.get_units
59 except AttributeError:
60 raise AttributeError('error: the file "%s" could not be processed' % filename)
61
62
63 for source, context, line in get_units():
64 source = encode_source(source)
65 unit = store.UnitClass(source)
66 store.addunit(unit)
67
68 @contextmanager
69 def store_context():
70 store = factory.getobject(outputfile)
71 try:
72 store.setfilename(store.getfilenode('NoName'), inputfile.name)
73 except:
74 print "couldn't set origin filename"
75 yield store
76 store.save()
77
78 def with_block(store):
79 if engine == "toolkit":
80 translate_toolkit_implementation(store)
81 else:
82 itools_implementation(store)
83
84
85
86
87 inputfile.close()
88 inputfile = file(inputfile.name, mode='rb')
89 with_(store_context(), with_block)
90 return True
91
92
93
94 formats = {
95 "sxw": ("xlf", convertodf),
96 "odt": ("xlf", convertodf),
97 "ods": ("xlf", convertodf),
98 "odp": ("xlf", convertodf),
99 "odg": ("xlf", convertodf),
100 "odc": ("xlf", convertodf),
101 "odf": ("xlf", convertodf),
102 "odi": ("xlf", convertodf),
103 "odm": ("xlf", convertodf),
104 "ott": ("xlf", convertodf),
105 "ots": ("xlf", convertodf),
106 "otp": ("xlf", convertodf),
107 "otg": ("xlf", convertodf),
108 "otc": ("xlf", convertodf),
109 "otf": ("xlf", convertodf),
110 "oti": ("xlf", convertodf),
111 "oth": ("xlf", convertodf),
112 }
113
114
115 -def main(argv=None):
116
117 def add_options(parser):
118 parser.add_option("", "--engine", dest="engine", default="toolkit",
119 type="choice", choices=["toolkit", "itools"],
120 help="""Choose whether itools (--engine=itools) or the translate toolkit (--engine=toolkit)
121 should be used as the engine to convert an ODF file to an XLIFF file.""")
122 parser.passthrough = ['engine']
123 return parser
124
125 from translate.convert import convert
126 parser = convert.ConvertOptionParser(formats, description=__doc__)
127 add_options(parser)
128 parser.run(argv)
129
130
131 if __name__ == '__main__':
132 main()
133