Package translate :: Package convert :: Module symb2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.symb2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2008-2009 Zuza Software Foundation
      5  #
      6  # This file is part of the Translate Toolkit.
      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  """Convert Symbian localisation files to Gettext PO localization files."""
     22
     23  from translate.storage import factory
     24  from translate.storage.pypo import extractpoline
     25  from translate.storage.symbian import *
     26
     27
     28 -def read_header_items(ps):
     29      match = read_while(ps, header_item_or_end_re.match, lambda match: match is None)
     30      if match.groupdict()['end_comment'] is not None:
     31          return {}
     32
     33      results = {}
     34      while match:
     35          match_chunks = match.groupdict()
     36          ps.read_line()
     37          results[match_chunks['key']] = match_chunks['value']
     38          match = header_item_re.match(ps.current_line)
     39
     40      match = read_while(ps, identity, lambda line: not line.startswith('*/'))
     41      ps.read_line()
     42      return results
     43
     44
     45 -def parse(ps):
     46      header = read_header_items(ps)
     47      units = []
     48      try:
     49          while True:
     50              eat_whitespace(ps)
     51              skip_no_translate(ps)
     52              match = string_entry_re.match(ps.current_line)
     53              if match is not None:
     54                  units.append((match.groupdict()['id'], extractpoline(match.groupdict()['str'])))
     55              ps.read_line()
     56      except StopIteration:
     57          pass
     58      return header, units
     59
     60
     61 -def read_symbian(f):
     62      lines = list(f)
     63      charset = read_charset(lines)
     64      return parse(ParseState(iter(lines), charset))
     65
     66
     67 -def get_template_dict(template_file):
     68      if template_file is not None:
     69          template_header, template_units = read_symbian(template_file)
     70          return template_header, dict(template_units)
     71      else:
     72          return {}, {}
     73
     74
     75 -def build_output(units, template_header, template_dict):
     76      output_store = factory.classes['po']()
     77      ignore = set(['r_string_languagegroup_name'])
     78      header_entries = {
     79          'Last-Translator': template_header.get('Author', ''),
     80          'Language-Team': template_dict.get('r_string_languagegroup_name', ''),
     81          'Content-Transfer-Encoding': '8bit',
     82          'Content-Type': 'text/plain; charset=UTF-8',
     83          }
     84      output_store.updateheader(add=True, **header_entries)
     85      for id, source in units:
     86          if id in ignore:
     87              continue
     88          unit = output_store.UnitClass(source)
     89          unit.target = template_dict.get(id, '')
     90          unit.addlocation(id)
     91          output_store.addunit(unit)
     92      return output_store
     93
     94
     95 -def convert_symbian(input_file, output_file, template_file, pot=False, duplicatestyle="msgctxt"):
     96      header, units = read_symbian(input_file)
     97      template_header, template_dict = get_template_dict(template_file)
     98      output_store = build_output(units, template_header, template_dict)
     99
    100      if output_store.isempty():
    101          return 0
    102      else:
    103          output_file.write(str(output_store))
    104          return 1
    105
    106
    107 -def main(argv=None):
    108      from translate.convert import convert
    109      formats = {"r01": ("po", convert_symbian)}
    110      parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
    111      parser.add_duplicates_option()
    112      parser.passthrough.append("pot")
    113      parser.run(argv)
    114
    115
    116  if __name__ == '__main__':
    117      main()
    118