Package muntjac :: Package data :: Package util :: Package filter :: Module simple_string_filter
[hide private]
[frames] | no frames]

Source Code for Module muntjac.data.util.filter.simple_string_filter

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """Simple string filter for matching items that start with or contain a 
 17  specified string.""" 
 18   
 19  from muntjac.data.container import IFilter 
 20   
 21   
22 -class SimpleStringFilter(IFilter):
23 """Simple string filter for matching items that start with or contain a 24 specified string. The matching can be case-sensitive or case-insensitive. 25 26 This filter also directly supports in-memory filtering. When performing 27 in-memory filtering, values of other types are converted using __str__, 28 but other (lazy container) implementations do not need to perform such 29 conversions and might not support values of different types. 30 31 Note that this filter might not be very efficient e.g. for database 32 filtering. 33 """ 34
35 - def __init__(self, propertyId, filterString, ignoreCase, onlyMatchPrefix):
36 self.propertyId = propertyId 37 38 if ignoreCase: 39 self.filterString = filterString.lower() 40 else: 41 self.filterString = filterString 42 43 self.ignoreCase = ignoreCase 44 self.onlyMatchPrefix = onlyMatchPrefix
45 46
47 - def passesFilter(self, itemId, item):
48 p = item.getItemProperty(self.propertyId) 49 50 if p is None or str(p) is None: 51 return False 52 53 value = str(p).lower() if self.ignoreCase else str(p) 54 55 if self.onlyMatchPrefix: 56 if not value.startswith(self.filterString): 57 return False 58 else: 59 if self.filterString not in value: 60 return False 61 62 return True
63 64
65 - def appliesToProperty(self, propertyId):
66 return self.propertyId == propertyId
67 68
69 - def __eq__(self, obj):
70 # Only ones of the objects of the same class can be equal 71 if not isinstance(obj, SimpleStringFilter): 72 return False 73 74 # Checks the properties one by one 75 if (self.propertyId != obj.propertyId 76 and obj.propertyId is not None 77 and obj.propertyId != self.propertyId): 78 return False 79 80 if (self.filterString != obj.filterString 81 and obj.filterString is not None 82 and obj.filterString != self.filterString): 83 return False 84 85 if self.ignoreCase != obj.ignoreCase: 86 return False 87 88 if self.onlyMatchPrefix != obj.onlyMatchPrefix: 89 return False 90 91 return True
92 93
94 - def __hash__(self):
95 h1 = hash(self.propertyId) if self.propertyId is not None else 0 96 h2 = hash(self.filterString) if self.filterString is not None else 0 97 return h1 ^ h2
98 99
100 - def getPropertyId(self):
101 """Returns the property identifier to which this filter applies. 102 103 @return: property id 104 """ 105 return self.propertyId
106 107
108 - def getFilterString(self):
109 """Returns the filter string. 110 111 Note: this method is intended only for implementations of lazy 112 string filters and may change in the future. 113 114 @return: filter string given to the constructor 115 """ 116 return self.filterString
117 118
119 - def isIgnoreCase(self):
120 """Returns whether the filter is case-insensitive or case-sensitive. 121 122 Note: this method is intended only for implementations of lazy string 123 filters and may change in the future. 124 125 @return: true if performing case-insensitive filtering, false for 126 case-sensitive 127 """ 128 return self.ignoreCase
129 130
131 - def isOnlyMatchPrefix(self):
132 """Returns true if the filter only applies to the beginning of the value 133 string, false for any location in the value. 134 135 Note: this method is intended only for implementations of lazy string 136 filters and may change in the future. 137 138 @return: true if checking for matches at the beginning of the value only, 139 false if matching any part of value 140 """ 141 return self.onlyMatchPrefix
142