translate.filters.prefilters

Package translate :: Package filters :: Module prefilters |
[hide private]
[frames] | no frames]

# Source Code for Module translate.filters.prefilters

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004-2008 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  """This is a set of string filters that strings can be passed through before
     23  certain tests."""
     24
     25  from translate.filters import decoration
     26  from translate.misc import quote
     27  import re
     28
     29 -def removekdecomments(str1):
     30      """removed kde-style po comments i.e. starting with _: and ending with litteral \\n"""
     31      assert isinstance(str1, unicode)
     32      iskdecomment = False
     33      lines = str1.split("\n")
     34      removelines = []
     35      for linenum in range(len(lines)):
     36          line = lines[linenum]
     37          if line.startswith("_:"):
     38              lines[linenum] = ""
     39              iskdecomment = True
     40          if iskdecomment:
     41              removelines.append(linenum)
     42          if line.strip() and not iskdecomment:
     43              break
     44          if iskdecomment and line.strip().endswith("\\n"):
     45              iskdecomment = False
     46      lines = [lines[linenum] for linenum in range(len(lines)) if linenum not in removelines]
     47      return "\n".join(lines)
     48
     49 -def filteraccelerators(accelmarker):
     50      """returns a function that filters accelerators marked using accelmarker in strings"""
     51      if accelmarker is None:
     52          accelmarkerlen = 0
     53      else:
     54          accelmarkerlen = len(accelmarker)
     55      def filtermarkedaccelerators(str1, acceptlist=None):
     56          """modifies the accelerators in str1 marked with a given marker, using a given filter"""
     57          acclocs, badlocs = decoration.findaccelerators(str1, accelmarker, acceptlist)
     58          fstr1, pos = "", 0
     59          for accelstart, accelerator in acclocs:
     60              fstr1 += str1[pos:accelstart]
     61              fstr1 += accelerator
     62              pos = accelstart + accelmarkerlen + len(accelerator)
     63          fstr1 += str1[pos:]
     64          return fstr1
     65      return filtermarkedaccelerators
     66
     67 -def varname(variable, startmarker, endmarker):
     68      """a simple variable filter that returns the variable name without the marking punctuation"""
     69      return variable
     70      # if the punctuation were included, we'd do the following:
     71      if startmarker is None:
     72          return variable[:variable.rfind(endmarker)]
     73      elif endmarker is None:
     74          return variable[variable.find(startmarker)+len(startmarker):]
     75      else:
     76          return variable[variable.find(startmarker)+len(startmarker):variable.rfind(endmarker)]
     77
     78 -def varnone(variable, startmarker, endmarker):
     79      """a simple variable filter that returns an emoty string"""
     80      return ""
     81
     82 -def filtervariables(startmarker, endmarker, varfilter):
     83      """returns a function that filters variables marked using startmarker and
     84      endmarker in strings"""
     85      if startmarker is None:
     86          startmarkerlen = 0
     87      else:
     88          startmarkerlen = len(startmarker)
     89      if endmarker is None:
     90          endmarkerlen = 0
     91      elif type(endmarker) == int:
     92          endmarkerlen = 0
     93      else:
     94          endmarkerlen = len(endmarker)
     95
     96      def filtermarkedvariables(str1):
     97          """modifies the variables in str1 marked with a given marker, using a given filter"""
     98          varlocs = decoration.findmarkedvariables(str1, startmarker, endmarker)
     99          fstr1, pos = "", 0
    100          for varstart, variable in varlocs:
    101              fstr1 += str1[pos:varstart]
    102              fstr1 += varfilter(variable, startmarker, endmarker)
    103              pos = varstart + startmarkerlen + len(variable) + endmarkerlen
    104          fstr1 += str1[pos:]
    105          return fstr1
    106      return filtermarkedvariables
    107
    108  # a list of special words with punctuation
    109  # all apostrophes in the middle of the word are handled already
    110  wordswithpunctuation = ["'n","'t" # Afrikaans
    111                         ]
    112  # map all the words to their non-punctified equivalent
    113  wordswithpunctuation = dict([(word, filter(str.isalnum, word)) for word in wordswithpunctuation])
    114
    115 -def filterwordswithpunctuation(str1):
    116      """goes through a list of known words that have punctuation and removes the
    117      punctuation from them"""
    118      assert isinstance(str1, unicode)
    119      occurrences = []
    120      for word, replacement in wordswithpunctuation.iteritems():
    121          occurrences.extend([(pos, word, replacement) for pos in quote.find_all(str1, word)])
    122      for match in re.finditer("(?u)\w+'\w+", str1):
    123          word = match.group()
    124          replacement = filter(unicode.isalnum, word)
    125          occurrences.append((match.start(), word, replacement))
    126      occurrences.sort()
    127      if occurrences:
    128          lastpos = 0
    129          newstr1 = ""
    130          for pos, word, replacement in occurrences:
    131              newstr1 += str1[lastpos:pos]
    132              newstr1 += replacement
    133              lastpos = pos + len(word)
    134          newstr1 += str1[lastpos:]
    135          return newstr1
    136      else:
    137          return str1
    138