Package translate :: Package convert :: Module odf2xliff |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.odf2xliff

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004-2006 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      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.
     12  #
     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.
     17  #
     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
     21  #
     22
     23  """convert OpenDocument (ODF) files to XLIFF localization files"""
     24
     25  # Import from ttk
     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):
     33      """reads in stdin using fromfileclass, converts using convertorclass,
     34         writes to stdout
     35      """
     36
     37      def translate_toolkit_implementation(store):
     38          import cStringIO
     39          import zipfile
     40
     41          from translate.storage.xml_extract import extract
     42          from translate.storage import odf_shared
     43
     44          contents = odf_io.open_odf(inputfile)
     45          for data in contents.values():
     46              parse_state = extract.ParseState(odf_shared.no_translate_content_elements,
     47                                               odf_shared.inline_elements)
     48              extract.build_store(cStringIO.StringIO(data), store, parse_state)
     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          # Make the XLIFF file
     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      # Since the convertoptionsparser will give us an open file, we risk that
     87      # it could have been opened in non-binary mode on Windows, and then we'll
     88      # have problems, so let's make sure we have what we want.
     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      # For formats see OpenDocument 1.2 draft 7 Appendix C
    106      formats = {"sxw": ("xlf", convertodf),
    107                 "odt": ("xlf", convertodf), # Text
    108                 "ods": ("xlf", convertodf), # Spreadsheet
    109                 "odp": ("xlf", convertodf), # Presentation
    110                 "odg": ("xlf", convertodf), # Drawing
    111                 "odc": ("xlf", convertodf), # Chart
    112                 "odf": ("xlf", convertodf), # Formula
    113                 "odi": ("xlf", convertodf), # Image
    114                 "odm": ("xlf", convertodf), # Master Document
    115                 "ott": ("xlf", convertodf), # Text template
    116                 "ots": ("xlf", convertodf), # Spreadsheet template
    117                 "otp": ("xlf", convertodf), # Presentation template
    118                 "otg": ("xlf", convertodf), # Drawing template
    119                 "otc": ("xlf", convertodf), # Chart template
    120                 "otf": ("xlf", convertodf), # Formula template
    121                 "oti": ("xlf", convertodf), # Image template
    122                 "oth": ("xlf", convertodf), # Web page template
    123                }
    124      parser = convert.ConvertOptionParser(formats, description=__doc__)
    125      add_options(parser)
    126      parser.run(argv)
    127
    128  if __name__ == '__main__':
    129      main()
    130