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