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