translate.convert.odf2xliff

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  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'):
     32      """reads in stdin using fromfileclass, converts using convertorclass,
     33         writes to stdout
     34      """
     35
     36      def translate_toolkit_implementation(store):
     37          import cStringIO
     38          import zipfile
     39
     40          from translate.storage.xml_extract import extract
     41          from translate.storage import odf_shared
     42
     43          contents = odf_io.open_odf(inputfile)
     44          for data in contents.values():
     45              parse_state = extract.ParseState(odf_shared.no_translate_content_elements,
     46                                               odf_shared.inline_elements)
     47              extract.build_store(cStringIO.StringIO(data), store, parse_state)
     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          # Make the XLIFF file
     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      # Since the convertoptionsparser will give us an open file, we risk that
     85      # it could have been opened in non-binary mode on Windows, and then we'll
     86      # have problems, so let's make sure we have what we want.
     87      inputfile.close()
     88      inputfile = file(inputfile.name, mode='rb')
     89      with_(store_context(), with_block)
     90      return True
     91
     92
     93  # For formats see OpenDocument 1.2 draft 7 Appendix C
     94  formats = {
     95      "sxw": ("xlf", convertodf),
     96      "odt": ("xlf", convertodf), # Text
     97      "ods": ("xlf", convertodf), # Spreadsheet
     98      "odp": ("xlf", convertodf), # Presentation
     99      "odg": ("xlf", convertodf), # Drawing
    100      "odc": ("xlf", convertodf), # Chart
    101      "odf": ("xlf", convertodf), # Formula
    102      "odi": ("xlf", convertodf), # Image
    103      "odm": ("xlf", convertodf), # Master Document
    104      "ott": ("xlf", convertodf), # Text template
    105      "ots": ("xlf", convertodf), # Spreadsheet template
    106      "otp": ("xlf", convertodf), # Presentation template
    107      "otg": ("xlf", convertodf), # Drawing template
    108      "otc": ("xlf", convertodf), # Chart template
    109      "otf": ("xlf", convertodf), # Formula template
    110      "oti": ("xlf", convertodf), # Image template
    111      "oth": ("xlf", convertodf), # Web page template
    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