fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / filters / helpers.py
blob857d73b5b0d5624765a9e057e1c0ce786d20eed2
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2004-2006 Zuza Software Foundation
5 #
6 # This file is part of translate.
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.
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.
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
22 """a set of helper functions for filters..."""
24 import operator
26 def countmatch(str1, str2, countstr):
27 """checks whether countstr occurs the same number of times in str1 and str2"""
28 return str1.count(countstr) == str2.count(countstr)
30 def funcmatch(str1, str2, func, *args):
31 """returns whether the result of func is the same for str1 and str2"""
32 return func(str1, *args) == func(str2, *args)
34 def countsmatch(str1, str2, countlist):
35 """checks whether each element in countlist occurs the same number of times in str1 and str2"""
36 return reduce(operator.and_, [countmatch(str1, str2, countstr) for countstr in countlist], True)
38 def funcsmatch(str1, str2, funclist):
39 """checks whether the results of each func in funclist match for str1 and str2"""
40 return reduce(operator.and_, [funcmatch(str1, str2, funcstr) for funcstr in funclist], True)
42 def filtercount(str1, func):
43 """returns the number of characters in str1 that pass func"""
44 return len(filter(func, str1))
46 def filtertestmethod(testmethod, strfilter):
47 """returns a version of the testmethod that operates on filtered strings using strfilter"""
48 def filteredmethod(str1, str2):
49 return testmethod(strfilter(str1), strfilter(str2))
50 filteredmethod.__doc__ = testmethod.__doc__
51 filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
52 return filteredmethod
54 def multifilter(str1, strfilters, *args):
55 """passes str1 through a list of filters"""
56 for strfilter in strfilters:
57 str1 = strfilter(str1, *args)
58 return str1
60 def multifiltertestmethod(testmethod, strfilters):
61 """returns a version of the testmethod that operates on filtered strings using strfilter"""
62 def filteredmethod(str1, str2):
63 return testmethod(multifilter(str1, strfilters), multifilter(str2, strfilters))
64 filteredmethod.__doc__ = testmethod.__doc__
65 filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
66 return filteredmethod