Mercurial > genshi > mirror
annotate genshi/template/match.py @ 701:52a597419c0d experimental-match-fastpaths
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
author | aflett |
---|---|
date | Fri, 28 Mar 2008 17:05:04 +0000 |
parents | 1240ada13334 |
children | af57b12e3dd2 |
rev | line source |
---|---|
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
1 from genshi.core import START |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
2 from genshi.path import CHILD, LocalNameTest |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
3 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
4 from copy import copy |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
5 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
6 def is_simple_path(path): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
7 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
8 Is the path merely a tag match like "foo"? |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
9 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
10 if len(path.paths) == 1 and len(path.paths[0]) == 1: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
11 axis, nodetest, predicates = path.paths[0][0] |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
12 if (axis is CHILD and |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
13 not predicates and |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
14 isinstance(nodetest, LocalNameTest)): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
15 return True |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
16 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
17 return False |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
18 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
19 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
20 class MatchSet(object): |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
21 """ A MatchSet is a set of matches discovered by the parser. This |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
22 class encapsulates the matching of a particular event to a set of |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
23 matches. It is optimized for basic tag matches, since that is by |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
24 far the most common use of py:match. |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
25 |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
26 The two primary entry points into MatchSet are ``add``, which adds |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
27 a new py:match, and ``find_matches``, which returns all |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
28 /candidate/ match templates. The consumer of ``find_matches`` |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
29 still must call each candidates' match() to ensure the event |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
30 really matches, and to maintain state within the match. |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
31 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
32 If a given py:match's path is simply a node name match, |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
33 (LocalNameTest) like "xyz", then MatchSet indexes that in a |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
34 dictionary that maps tag names to matches. |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
35 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
36 If the path is more complex like "xyz[k=z]" then then that match |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
37 will always be returned by ``find_matches``. """ |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
38 def __init__(self, parent=None, exclude=None): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
39 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
40 If a parent is given, it means this is a wrapper around another |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
41 set. |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
42 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
43 If exclude is given, it means include everything in the |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
44 parent, but exclude a specific match template. |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
45 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
46 self.parent = parent |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
47 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
48 self.current_index = 0 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
49 |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
50 if parent is None: |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
51 # merely for indexing. Note that this is shared between |
701
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
52 # all MatchSets that share the same root parent. We don't |
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
53 # have to worry about exclusions here |
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
54 self.match_order = {} |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
55 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
56 # tag_templates are match templates whose path are simply |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
57 # a tag, like "body" or "img" |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
58 self.tag_templates = {} |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
59 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
60 # other_templates include all other match templates, such |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
61 # as ones with complex paths like "[class=container]" |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
62 self.other_templates = [] |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
63 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
64 # exclude is a list of templates to ignore when iterating |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
65 # through templates |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
66 self.exclude = [] |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
67 if exclude is not None: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
68 self.exclude.append(exclude) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
69 else: |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
70 # We have a parent: Just copy references to member |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
71 # variables in parent so that there's no performance loss, |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
72 # but make our own exclusion set, so we don't have to |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
73 # chain exclusions across a chain of MatchSets |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
74 self.match_order = parent.match_order |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
75 self.tag_templates = parent.tag_templates |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
76 self.other_templates = parent.other_templates |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
77 |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
78 self.exclude = copy(parent.exclude) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
79 if exclude is not None: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
80 self.exclude.append(exclude) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
81 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
82 def add(self, match_template): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
83 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
84 match_template is a tuple the form |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
85 test, path, template, hints, namespace, directives |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
86 """ |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
87 |
701
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
88 # match_templates are currently tuples that contain unhashable |
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
89 # objects. So we'll use id() for now. |
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
90 self.match_order[id(match_template)] = len(self.match_order) |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
91 |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
92 path = match_template[1] |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
93 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
94 self.current_index += 1 |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
95 if is_simple_path(path): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
96 # special cache of tag |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
97 tag_name = path.paths[0][0][1].name |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
98 # setdefault is wasteful |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
99 if tag_name not in self.tag_templates: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
100 self.tag_templates[tag_name] = [match_template] |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
101 else: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
102 self.tag_templates[tag_name].append(match_template) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
103 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
104 else: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
105 self.other_templates.append(match_template) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
106 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
107 def remove(self, match_template): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
108 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
109 Permanently remove a match_template - mainly for match_once |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
110 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
111 path = match_template[1] |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
112 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
113 if is_simple_path(path): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
114 tag_name = path.paths[0][0][1].name |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
115 if tag_name in self.tag_templates: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
116 template_list = self.tag_templates[tag_name] |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
117 template_list.remove(match_template) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
118 if not template_list: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
119 del self.tag_templates[tag_name] |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
120 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
121 else: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
122 self.other_templates.remove(match_template) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
123 |
701
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
124 # clean up match_order |
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
125 del self.match_order[id(match_template)] |
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
126 |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
127 def single_match(cls, match_template): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
128 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
129 Factory for creating a MatchSet with just one match |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
130 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
131 match_set = cls() |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
132 match_set.add(match_template) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
133 return match_set |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
134 single_match = classmethod(single_match) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
135 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
136 def with_exclusion(self, exclude): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
137 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
138 Factory for creating a MatchSet based on another MatchSet, but |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
139 with certain templates excluded |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
140 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
141 cls = self.__class__ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
142 new_match_set = cls(parent=self, exclude=exclude) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
143 return new_match_set |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
144 |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
145 def find_raw_matches(self, event): |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
146 """ Return a list of all valid templates that can be used for the |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
147 given event. Ordering is funky because we first check |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
148 self.tag_templates, then check self.other_templates. |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
149 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
150 kind, data, pos = event[:3] |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
151 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
152 # todo: get the order right |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
153 if kind is START: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
154 tag, attrs = data |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
155 if tag.localname in self.tag_templates: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
156 for template in self.tag_templates[tag.localname]: |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
157 yield template |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
158 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
159 for template in self.other_templates: |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
160 yield template |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
161 |
690
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
162 def find_matches(self, event): |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
163 """ Return a list of all valid templates that can be used for the |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
164 given event. |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
165 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
166 The basic work here is sorting the result of find_raw_matches |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
167 """ |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
168 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
169 # remove exclusions |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
170 matches = filter(lambda template: template not in self.exclude, |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
171 self.find_raw_matches(event)) |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
172 |
1240ada13334
more code/comment clean up - make sure to retain match order
aflett
parents:
687
diff
changeset
|
173 # sort the results according to the order they were added |
701
52a597419c0d
minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
aflett
parents:
690
diff
changeset
|
174 return sorted(matches, key=lambda v: self.match_order[id(v)]) |
687
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
175 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
176 def __nonzero__(self): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
177 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
178 allow this to behave as a list |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
179 """ |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
180 return bool(self.tag_templates or self.other_templates) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
181 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
182 def __str__(self): |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
183 parent = "" |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
184 if self.parent: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
185 parent = ": child of 0x%x" % id(self.parent) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
186 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
187 exclude = "" |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
188 if self.exclude: |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
189 exclude = " / excluding %d items" % len(self.exclude) |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
190 |
3d7288f373bd
land first cut at fast-path matching - needs some cleanup
aflett
parents:
diff
changeset
|
191 return "<MatchSet 0x%x %d tag templates, %d other templates%s%s>" % (id(self), len(self.tag_templates), len(self.other_templates), parent, exclude) |