Show More
@@ -1,65 +1,70 b'' | |||||
|
1 | """String dispatch class to match regexps and dispatch commands. | |||
|
2 | """ | |||
|
3 | ||||
|
4 | # Stdlib imports | |||
|
5 | import re | |||
|
6 | ||||
|
7 | # Our own modules | |||
1 | from IPython.hooks import CommandChainDispatcher |
|
8 | from IPython.hooks import CommandChainDispatcher | |
2 | import IPython.hooks |
|
9 | import IPython.hooks | |
3 |
|
10 | |||
4 | import re |
|
|||
5 |
|
11 | |||
|
12 | # Code begins | |||
6 | class StrDispatch(object): |
|
13 | class StrDispatch(object): | |
7 |
""" |
|
14 | """Dispatch (lookup) a set of strings / regexps for match. | |
|
15 | ||||
|
16 | Example: | |||
|
17 | ||||
|
18 | >>> dis = StrDispatch() | |||
|
19 | >>> dis.add_s('hei',34, priority = 4) | |||
|
20 | >>> dis.add_s('hei',123, priority = 2) | |||
|
21 | >>> dis.add_re('h.i', 686) | |||
|
22 | >>> print list(dis.flat_matches('hei')) | |||
|
23 | [123, 34, 686] | |||
|
24 | """ | |||
|
25 | ||||
8 | def __init__(self): |
|
26 | def __init__(self): | |
9 | self.strs = {} |
|
27 | self.strs = {} | |
10 | self.regexs = {} |
|
28 | self.regexs = {} | |
|
29 | ||||
11 | def add_s(self, s, obj, priority= 0 ): |
|
30 | def add_s(self, s, obj, priority= 0 ): | |
12 | """ Adds a target 'string' for dispatching """ |
|
31 | """ Adds a target 'string' for dispatching """ | |
13 |
|
32 | |||
14 | chain = self.strs.get(s, CommandChainDispatcher()) |
|
33 | chain = self.strs.get(s, CommandChainDispatcher()) | |
15 | chain.add(obj,priority) |
|
34 | chain.add(obj,priority) | |
16 | self.strs[s] = chain |
|
35 | self.strs[s] = chain | |
17 |
|
36 | |||
18 | def add_re(self, regex, obj, priority= 0 ): |
|
37 | def add_re(self, regex, obj, priority= 0 ): | |
19 | """ Adds a target regexp for dispatching """ |
|
38 | """ Adds a target regexp for dispatching """ | |
20 |
|
39 | |||
21 | chain = self.regexs.get(regex, CommandChainDispatcher()) |
|
40 | chain = self.regexs.get(regex, CommandChainDispatcher()) | |
22 | chain.add(obj,priority) |
|
41 | chain.add(obj,priority) | |
23 | self.regexs[regex] = chain |
|
42 | self.regexs[regex] = chain | |
24 |
|
43 | |||
25 | def dispatch(self, key): |
|
44 | def dispatch(self, key): | |
26 | """ Get a seq of Commandchain objects that match key """ |
|
45 | """ Get a seq of Commandchain objects that match key """ | |
27 | if key in self.strs: |
|
46 | if key in self.strs: | |
28 | yield self.strs[key] |
|
47 | yield self.strs[key] | |
29 |
|
48 | |||
30 | for r, obj in self.regexs.items(): |
|
49 | for r, obj in self.regexs.items(): | |
31 | if re.match(r, key): |
|
50 | if re.match(r, key): | |
32 | yield obj |
|
51 | yield obj | |
33 | else: |
|
52 | else: | |
34 | #print "nomatch",key |
|
53 | #print "nomatch",key # dbg | |
35 | pass |
|
54 | pass | |
36 |
|
||||
37 |
|
55 | |||
38 | def __repr__(self): |
|
56 | def __repr__(self): | |
39 | return "<Strdispatch %s, %s>" % (self.strs, self.regexs) |
|
57 | return "<Strdispatch %s, %s>" % (self.strs, self.regexs) | |
40 |
|
58 | |||
41 | def s_matches(self, key): |
|
59 | def s_matches(self, key): | |
42 | if key not in self.strs: |
|
60 | if key not in self.strs: | |
43 | return |
|
61 | return | |
44 | for el in self.strs[key]: |
|
62 | for el in self.strs[key]: | |
45 | yield el[1] |
|
63 | yield el[1] | |
46 |
|
64 | |||
47 |
|
||||
48 | def flat_matches(self, key): |
|
65 | def flat_matches(self, key): | |
49 | """ Yield all 'value' targets, without priority """ |
|
66 | """ Yield all 'value' targets, without priority """ | |
50 | for val in self.dispatch(key): |
|
67 | for val in self.dispatch(key): | |
51 | for el in val: |
|
68 | for el in val: | |
52 | yield el[1] # only value, no priority |
|
69 | yield el[1] # only value, no priority | |
53 | return |
|
70 | return | |
54 |
|
||||
55 |
|
||||
56 | def test(): |
|
|||
57 | d = StrDispatch() |
|
|||
58 | d.add_s('hei',34, priority = 4) |
|
|||
59 | d.add_s('hei',123, priority = 2) |
|
|||
60 | print list(d.dispatch('hei')) |
|
|||
61 | d.add_re('h.i', 686) |
|
|||
62 | print list(d.flat_matches('hei')) |
|
|||
63 |
|
||||
64 | if __name__ == '__main__': |
|
|||
65 | test() No newline at end of file |
|
General Comments 0
You need to be logged in to leave comments.
Login now