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 hasmarkedcomment(self, commentmarker):
     86          raise NotImplementedError
     87
     88 -    def isreview(self):
     89          return self.hasmarkedcomment("review") or self.hasmarkedcomment("pofilter")
     90
     91
     92 -def encodingToUse(encoding):
     93      """Tests whether the given encoding is known in the python runtime, or returns utf-8.
     94      This function is used to ensure that a valid encoding is always used."""
     95      if encoding == "CHARSET" or encoding == None:
     96          return 'utf-8'
     97      return encoding
     98  #    if encoding is None: return False
     99  #    return True
    100  #    try:
    101  #        tuple = codecs.lookup(encoding)
    102  #    except LookupError:
    103  #        return False
    104  #    return True
    105
    106 -class pofile(poheader.poheader, base.TranslationStore):
    107      Name = _("Gettext PO file") # pylint: disable-msg=E0602
    108      Mimetypes  = ["text/x-gettext-catalog", "text/x-gettext-translation", "text/x-po", "text/x-pot"]
    109      Extensions = ["po", "pot"]
    110
    111 -    def __init__(self, inputfile=None, encoding=None):
    112          super(pofile, self).__init__(unitclass=self.UnitClass)
    113          self.units = []
    114          self.filename = ''
    115          self._encoding = encodingToUse(encoding)
    116          if inputfile is not None:
    117              self.parse(inputfile)
    118          else:
    119              self.init_headers()
    120