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, nested
29 from translate.misc.context import with_
30 from translate.storage import odf_io
31
32 -def convertodf(inputfile, outputfile, templates, engine):
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 -def main(argv=None):
96 def add_options(parser):
97 parser.add_option("", "--engine", dest="engine", default="toolkit",
98 type="choice", choices=["toolkit", "itools"],
99 help="""Choose whether itools (--engine=itools) or the translate toolkit (--engine=toolkit)
100 should be used as the engine to convert an ODF file to an XLIFF file.""")
101 parser.passthrough = ['engine']
102 return parser
103
104 from translate.convert import convert
105
106 formats = {"sxw": ("xlf", convertodf),
107 "odt": ("xlf", convertodf),
108 "ods": ("xlf", convertodf),
109 "odp": ("xlf", convertodf),
110 "odg": ("xlf", convertodf),
111 "odc": ("xlf", convertodf),
112 "odf": ("xlf", convertodf),
113 "odi": ("xlf", convertodf),
114 "odm": ("xlf", convertodf),
115 "ott": ("xlf", convertodf),
116 "ots": ("xlf", convertodf),
117 "otp": ("xlf", convertodf),
118 "otg": ("xlf", convertodf),
119 "otc": ("xlf", convertodf),
120 "otf": ("xlf", convertodf),
121 "oti": ("xlf", convertodf),
122 "oth": ("xlf", convertodf),
123 }
124 parser = convert.ConvertOptionParser(formats, description=__doc__)
125 add_options(parser)
126 parser.run(argv)
127
128 if __name__ == '__main__':
129 main()
130