1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
66 return self.propertyId == propertyId
67
68
70
71 if not isinstance(obj, SimpleStringFilter):
72 return False
73
74
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
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
101 """Returns the property identifier to which this filter applies.
102
103 @return: property id
104 """
105 return self.propertyId
106
107
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
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
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