translate.storage.pocommon

Package translate :: Package storage :: Module pocommon |
[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.pocommon

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2002-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  from translate.storage import base
     23  from translate.storage import poheader
     24
     25  import re
     26
     27  msgid_comment_re = re.compile("_: (.*?)\n")
     28
     29 -def extract_msgid_comment(text):
     30      """The one definitive way to extract a msgid comment out of an unescaped
     31      unicode string that might contain it.
     32
     33      @rtype: unicode"""
     34      msgidcomment = msgid_comment_re.match(text)
     35      if msgidcomment:
     36          return msgidcomment.group(1)
     37      return u""
     38
     39
     40 -class pounit(base.TranslationUnit):
     41
     42 -    def adderror(self, errorname, errortext):
     43          """Adds an error message to this unit."""
     44          text = u'(pofilter) %s: %s' % (errorname, errortext)
     45          # Don't add the same error twice:
     46          if text not in self.getnotes(origin='translator'):
     47              self.addnote(text, origin="translator")
     48
     49 -    def geterrors(self):
     50          """Get all error messages."""
     51          notes = self.getnotes(origin="translator").split('\n')
     52          errordict = {}
     53          for note in notes:
     54              if '(pofilter) ' in note:
     55                  error = note.replace('(pofilter) ', '')
     56                  errorname, errortext = error.split(': ')
     57                  errordict[errorname] = errortext
     58          return errordict
     59
     60 -    def markreviewneeded(self, needsreview=True, explanation=None):
     61          """Marks the unit to indicate whether it needs review. Adds an optional explanation as a note."""
     62          if needsreview:
     63              reviewnote = "(review)"
     64              if explanation:
     65                  reviewnote += " " + explanation
     66              self.addnote(reviewnote, origin="translator")
     67          else:
     68              # Strip (review) notes.
     69              notestring = self.getnotes(origin="translator")
     70              notes = notestring.split('\n')
     71              newnotes = []
     72              for note in notes:
     73                  if not '(review)' in note:
     74                      newnotes.append(note)
     75              newnotes = '\n'.join(newnotes)
     76              self.removenotes()
     77              self.addnote(newnotes, origin="translator")
     78
     79 -    def istranslated(self):
     80          return super(pounit, self).istranslated() and not self.isobsolete()
     81
     82 -    def istranslatable(self):
     83          return not (self.isheader() or self.isblank() or self.isobsolete())
     84
     85 -    def isreview(self):
     86          return self.hasmarkedcomment("review") or self.hasmarkedcomment("pofilter")
     87
     88
     89 -def encodingToUse(encoding):
     90      """Tests whether the given encoding is known in the python runtime, or returns utf-8.
     91      This function is used to ensure that a valid encoding is always used."""
     92      if encoding == "CHARSET" or encoding == None:
     93          return 'utf-8'
     94      return encoding
     95  #    if encoding is None: return False
     96  #    return True
     97  #    try:
     98  #        tuple = codecs.lookup(encoding)
     99  #    except LookupError:
    100  #        return False
    101  #    return True
    102
    103 -class pofile(poheader.poheader, base.TranslationStore):
    104      Name = _("Gettext PO file")
    105      Mimetypes  = ["text/x-gettext-catalog", "text/x-gettext-translation", "text/x-po", "text/x-pot"]
    106      Extensions = ["po", "pot"]
    107
    108 -    def __init__(self, inputfile=None, encoding=None):
    109          super(pofile, self).__init__(unitclass=self.UnitClass)
    110          self.units = []
    111          self.filename = ''
    112          self._encoding = encodingToUse(encoding)
    113          if inputfile is not None:
    114              self.parse(inputfile)
    115          else:
    116              self.init_headers()
    117