1
from IPython.hooks import CommandChainDispatcher
6
class StrDispatch(object):
7
""" Dispatch (lookup) a set of strings / regexps for match """
11
def add_s(self, s, obj, priority= 0 ):
12
""" Adds a target 'string' for dispatching """
14
chain = self.strs.get(s, CommandChainDispatcher())
15
chain.add(obj,priority)
18
def add_re(self, regex, obj, priority= 0 ):
19
""" Adds a target regexp for dispatching """
21
chain = self.regexs.get(regex, CommandChainDispatcher())
22
chain.add(obj,priority)
23
self.regexs[regex] = chain
25
def dispatch(self, key):
26
""" Get a seq of Commandchain objects that match key """
30
for r, obj in self.regexs.items():
39
return "<Strdispatch %s, %s>" % (self.strs, self.regexs)
41
def s_matches(self, key):
42
if key not in self.strs:
44
for el in self.strs[key]:
48
def flat_matches(self, key):
49
""" Yield all 'value' targets, without priority """
50
for val in self.dispatch(key):
52
yield el[1] # only value, no priority
58
d.add_s('hei',34, priority = 4)
59
d.add_s('hei',123, priority = 2)
60
print list(d.dispatch('hei'))
62
print list(d.flat_matches('hei'))
64
if __name__ == '__main__':
b'\\ No newline at end of file'