Package translate :: Package convert :: Module po2ini

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2ini

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2002-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 Gettext PO localization files to .ini files"""
    24
    25  from translate.storage import factory
    26
    27 -class reini:
    28 -    def __init__(self, templatefile, inputstore, dialect="default"):
    29          from translate.storage import ini
    30          self.templatefile = templatefile
    31          self.templatestore = ini.inifile(templatefile, dialect=dialect)
    32          self.inputstore = inputstore
    33
    34 -    def convertstore(self, includefuzzy=False):
    35          self.includefuzzy = includefuzzy
    36          self.inputstore.makeindex()
    37          for unit in self.templatestore.units:
    38              for location in unit.getlocations():
    39                  if location in self.inputstore.locationindex:
    40                      inputunit = self.inputstore.locationindex[location]
    41                      if inputunit.isfuzzy() and not self.includefuzzy:
    42                          unit.target = unit.source
    43                      else:
    44                          unit.target = inputunit.target
    45                  else:
    46                      unit.target = unit.source
    47          return str(self.templatestore)
    48
    49 -def convertini(inputfile, outputfile, templatefile, includefuzzy=False, dialect="default"):
    50      inputstore = factory.getobject(inputfile)
    51      if templatefile is None:
    52          raise ValueError("must have template file for ini files")
    53      else:
    54          convertor = reini(templatefile, inputstore, dialect)
    55      outputstring = convertor.convertstore(includefuzzy)
    56      outputfile.write(outputstring)
    57      return 1
    58
    59 -def convertisl(inputfile, outputfile, templatefile, includefuzzy=False, dialect="inno"):
    60      convertini(inputfile, outputfile, templatefile, includefuzzy, dialect)
    61
    62 -def main(argv=None):
    63      # handle command line options
    64      from translate.convert import convert
    65      formats = {
    66                 ("po", "ini"): ("ini", convertini),
    67                 ("po", "isl"): ("isl", convertisl),
    68                }
    69      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
    70      parser.add_fuzzy_option()
    71      parser.run(argv)
    72
    73  if __name__ == '__main__':
    74      main()
    75