Package translate :: Package convert :: Module po2csv

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2csv

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2003-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  """convert Gettext PO localization files to Comma-Separated Value (.csv) files
    23
    24  see: http://translate.sourceforge.net/wiki/toolkit/po2csv for examples and
    25  usage instructions
    26  """
    27
    28  from translate.storage import po
    29  from translate.storage import csvl10n
    30
    31 -class po2csv:
    32 -    def convertcomments(self, inputunit):
    33          return " ".join(inputunit.getlocations())
    34
    35 -    def convertunit(self, inputunit):
    36          csvunit = csvl10n.csvunit()
    37          if inputunit.isheader():
    38              csvunit.comment = "location"
    39              csvunit.source = "source"
    40              csvunit.target = "target"
    41          elif inputunit.isblank():
    42              return None
    43          else:
    44              csvunit.comment = self.convertcomments(inputunit)
    45              csvunit.source = inputunit.source
    46              csvunit.target = inputunit.target
    47          return csvunit
    48
    49 -    def convertplurals(self, inputunit):
    50          csvunit = csvl10n.csvunit()
    51          csvunit.comment = self.convertcomments(inputunit)
    52          csvunit.source = inputunit.source.strings[1]
    53          csvunit.target = inputunit.target.strings[1]
    54          return csvunit
    55
    56 -    def convertstore(self, inputstore, columnorder=None):
    57          outputstore = csvl10n.csvfile(fieldnames=columnorder)
    58          for inputunit in inputstore.units:
    59              outputunit = self.convertunit(inputunit)
    60              if outputunit is not None:
    61                  outputstore.addunit(outputunit)
    62              if inputunit.hasplural():
    63                  outputunit = self.convertplurals(inputunit)
    64                  if outputunit is not None:
    65                      outputstore.addunit(outputunit)
    66          return outputstore
    67
    68 -def convertcsv(inputfile, outputfile, templatefile, columnorder=None):
    69      """reads in inputfile using po, converts using po2csv, writes to outputfile"""
    70      # note that templatefile is not used, but it is required by the converter...
    71      inputstore = po.pofile(inputfile)
    72      if inputstore.isempty():
    73          return 0
    74      convertor = po2csv()
    75      outputstore = convertor.convertstore(inputstore, columnorder)
    76      outputfile.write(str(outputstore))
    77      return 1
    78
    79 -def main(argv=None):
    80      from translate.convert import convert
    81      formats = {"po":("csv", convertcsv)}
    82      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
    83      parser.add_option("", "--columnorder", dest="columnorder", default=None,
    84          help="specify the order and position of columns (location,source,target)")
    85      parser.passthrough.append("columnorder")
    86      parser.run(argv)
    87
    88
    89  if __name__ == '__main__':
    90      main()
    91