Package translate :: Package convert :: Module po2web2py

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2web2py

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2009 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     7  #
     8  # This program 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  # This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
    20  #
    21  # (c) 2009 Dominic König (dominic@nursix.org)
    22  #
    23
    24  """convert GNU/gettext PO files to web2py translation dictionaries (.py)"""
    25
    26  from translate.storage import factory
    27
    28 -class po2pydict:
    29 -    def __init__(self):
    30          return
    31
    32 -    def convertstore(self, inputstore, includefuzzy):
    33          from StringIO import StringIO
    34          str_obj = StringIO()
    35
    36          mydict = dict()
    37          for unit in inputstore.units:
    38              if unit.isheader():
    39                  continue
    40              if unit.istranslated() or (includefuzzy and unit.isfuzzy()):
    41                  mydict[unit.source] = unit.target
    42              else:
    43                  mydict[unit.source] = '*** ' + unit.source
    44
    45          str_obj.write('{\n')
    46          for source_str in mydict:
    47              str_obj.write("%s:%s,\n" % (repr(str(source_str)),repr(str(mydict[source_str]))))
    48          str_obj.write('}\n')
    49          str_obj.seek(0)
    50          return str_obj
    51
    52 -def convertpy(inputfile, outputfile, templatefile=None, includefuzzy=False):
    53      inputstore = factory.getobject(inputfile)
    54      convertor = po2pydict()
    55      outputstring = convertor.convertstore(inputstore, includefuzzy)
    56      outputfile.write(outputstring.read())
    57      return 1
    58
    59 -def main(argv=None):
    60      from translate.convert import convert
    61      from translate.misc import stdiotell
    62      import sys
    63      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    64      formats = {("po", "py"):("py", convertpy), ("po"):("py", convertpy)}
    65      parser = convert.ConvertOptionParser(formats, usetemplates=False, description=__doc__)
    66      parser.add_fuzzy_option()
    67      parser.run(argv)
    68
    69  if __name__ == '__main__':
    70      main()
    71