Package translate :: Package convert :: Module csv2tbx

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.csv2tbx

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2006-2007 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 Comma-Separated Value (.csv) files to a TermBase eXchange (.tbx) glossary file"""
    23
    24  from translate.storage import tbx
    25  from translate.storage import csvl10n
    26
    27
    28 -class csv2tbx:
    29      """a class that takes translations from a .csv file and puts them in a .tbx file"""
    30
    31 -    def __init__(self, charset=None):
    32          """construct the converter..."""
    33          self.charset = charset
    34
    35 -    def convertfile(self, thecsvfile):
    36          """converts a csvfile to a tbxfile, and returns it. uses templatepo if given at construction"""
    37          mightbeheader = True
    38          self.tbxfile = tbx.tbxfile()
    39          for thecsv in thecsvfile.units:
    40              if mightbeheader:
    41                  # ignore typical header strings...
    42                  mightbeheader = False
    43                  if [item.strip().lower() for item in thecsv.comment, thecsv.source, thecsv.target] == \
    44                       ["comment", "original", "translation"]:
    45                      continue
    46                  if len(thecsv.comment.strip()) == 0 and thecsv.source.find("Content-Type:") != -1:
    47                      continue
    48              term = tbx.tbxunit.buildfromunit(thecsv)
    49              # TODO: we might want to get the location or other information from CSV
    50              self.tbxfile.addunit(term)
    51          return self.tbxfile
    52
    53
    54 -def convertcsv(inputfile, outputfile, templatefile, charset=None, columnorder=None):
    55      """reads in inputfile using csvl10n, converts using csv2tbx, writes to outputfile"""
    56      inputstore = csvl10n.csvfile(inputfile, fieldnames=columnorder)
    57      convertor = csv2tbx(charset=charset)
    58      outputstore = convertor.convertfile(inputstore)
    59      if len(outputstore.units) == 0:
    60          return 0
    61      outputfile.write(str(outputstore))
    62      return 1
    63
    64
    65 -def main():
    66      from translate.convert import convert
    67      formats = {("csv", "tbx"): ("tbx", convertcsv), ("csv", None): ("tbx", convertcsv)}
    68      parser = convert.ConvertOptionParser(formats, usetemplates=False, description=__doc__)
    69      parser.add_option("", "--charset", dest="charset", default=None,
    70          help="set charset to decode from csv files", metavar="CHARSET")
    71      parser.add_option("", "--columnorder", dest="columnorder", default=None,
    72          help="specify the order and position of columns (comment,source,target)")
    73      parser.passthrough.append("charset")
    74      parser.passthrough.append("columnorder")
    75      parser.run()
    76
    77
    78  if __name__ == '__main__':
    79      main()
    80