Mercurial > pylearn
annotate doc/v2_planning/plugin.py @ 1175:805e7c369fd1
small change to fix warning and allow the file to generate a HTML page.
author | Frederic Bastien <nouiz@nouiz.org> |
---|---|
date | Fri, 17 Sep 2010 16:19:17 -0400 |
parents | a1957faecc9b |
children |
rev | line source |
---|---|
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
1 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
2 import time |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
3 from collections import defaultdict, deque |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
4 from copy import copy |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
5 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
6 inf = float('inf') |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
7 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
8 ############# |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
9 ### EVENT ### |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
10 ############# |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
11 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
12 class Event(object): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
13 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
14 def __init__(self, type, **attributes): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
15 self.type = type |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
16 self.__dict__.update(attributes) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
17 self.attributes = dict(type = type, **attributes) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
18 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
19 def match(self, other): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
20 if isinstance(other, Matcher): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
21 return other(self) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
22 else: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
23 oattr = other.attributes |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
24 for k, v in self.attributes.iteritems(): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
25 if k in oattr: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
26 v2 = oattr[k] |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
27 if isinstance(v2, Matcher): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
28 if not v2(v): return False |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
29 else: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
30 if v != v2: return False |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
31 return True |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
32 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
33 def __str__(self): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
34 return "Event(%s)" % ", ".join("%s=%s" % (k, v) for k, v in self.attributes.iteritems()) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
35 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
36 class Matcher(object): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
37 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
38 def __call__(self, object): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
39 raise NotImplementedError("Implement this!") |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
40 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
41 class FnMatcher(Matcher): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
42 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
43 def __init__(self, function): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
44 self.function = function |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
45 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
46 def __call__(self, object): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
47 return self.function(object) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
48 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
49 all_events = FnMatcher(lambda _: True) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
50 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
51 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
52 |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
53 ################ |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
54 ### SCHEDULE ### |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
55 ################ |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
56 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
57 class Schedule(Matcher): |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
58 def __add__(self, i): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
59 return OffsetSchedule(self, i) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
60 def __or__(self, s): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
61 return UnionSchedule(self, to_schedule(s)) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
62 def __and__(self, s): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
63 return IntersectionSchedule(self, to_schedule(s)) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
64 def __sub__(self, i): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
65 return OffsetSchedule(self, -i) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
66 def __ror__(self, s): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
67 return UnionSchedule(to_schedule(s), self) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
68 def __rand__(self, s): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
69 return IntersectionSchedule(to_schedule(s), self) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
70 def __invert__(self): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
71 return NegatedSchedule(self) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
72 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
73 def to_schedule(x): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
74 if x in (None, False): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
75 return never |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
76 if x is True: |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
77 return always |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
78 elif isinstance(x, (list, tuple)): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
79 return reduce(UnionSchedule, x) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
80 else: |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
81 return x |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
82 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
83 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
84 class ScheduleMix(Schedule): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
85 __n__ = None |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
86 def __init__(self, *subschedules): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
87 assert (not self.__n__) or len(subschedules) == self.__n__ |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
88 self.subschedules = map(to_schedule, subschedules) |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
89 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
90 class UnionSchedule(ScheduleMix): |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
91 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
92 return any(s(time) for s in self.subschedules) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
93 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
94 class IntersectionSchedule(ScheduleMix): |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
95 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
96 return all(s(time) for s in self.subschedules) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
97 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
98 class DifferenceSchedule(ScheduleMix): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
99 __n__ = 2 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
100 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
101 return self.subschedules[0](time) and not self.subschedules[1](time) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
102 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
103 class NegatedSchedule(ScheduleMix): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
104 __n__ = 1 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
105 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
106 return not self.subschedules[0](time) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
107 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
108 class OffsetSchedule(Schedule): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
109 def __init__(self, schedule, offset): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
110 self.schedule = schedule |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
111 self.offset = offset |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
112 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
113 if isinstance(time, int): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
114 return self.schedule(time - self.offset) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
115 else: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
116 t1, t2 = time |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
117 return self.schedule((t1 - self.offset, t2 - self.offset)) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
118 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
119 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
120 class AlwaysSchedule(Schedule): |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
121 def __call__(self, time): |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
122 return True |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
123 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
124 always = AlwaysSchedule() |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
125 never = ~always |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
126 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
127 class IntervalSchedule(Schedule): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
128 def __init__(self, step, repeat = inf): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
129 self.step = step |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
130 self.upper_bound = step * (repeat - 1) |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
131 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
132 if isinstance(time, int): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
133 if time < 0 or time > self.upper_bound: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
134 return False |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
135 return time % self.step == 0 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
136 else: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
137 t1, t2 = time |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
138 if t2 < 0 or t1 > self.upper_bound: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
139 return False |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
140 diff = t2 - t1 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
141 t1m = t1 % self.step |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
142 t2m = t2 % self.step |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
143 return (diff >= self.step |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
144 or t1m == 0 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
145 or t2m == 0 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
146 or t1m > t2m) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
147 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
148 each = lambda step, repeat = inf: each0(step, repeat) + step |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
149 each0 = IntervalSchedule |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
150 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
151 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
152 class RangeSchedule(Schedule): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
153 def __init__(self, low = None, high = None): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
154 self.low = low or -inf |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
155 self.high = high or inf |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
156 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
157 if isinstance(time, int): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
158 return self.low <= time <= self.high |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
159 else: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
160 t1, t2 = time |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
161 return self.low <= t1 <= self.high \ |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
162 or self.low <= t2 <= self.high |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
163 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
164 inrange = RangeSchedule |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
165 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
166 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
167 class ListSchedule(Schedule): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
168 def __init__(self, *schedules): |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
169 self.schedules = schedules |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
170 def __call__(self, time): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
171 if isinstance(time, int): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
172 return time in self.schedules |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
173 else: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
174 for t in self.schedules: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
175 if t1 <= t <= t2: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
176 return True |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
177 return False |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
178 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
179 at = ListSchedule |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
180 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
181 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
182 ############## |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
183 ### PLUGIN ### |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
184 ############## |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
185 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
186 class Plugin(object): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
187 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
188 def attach(self, scheduler): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
189 c = copy(self) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
190 c.scheduler = scheduler |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
191 return c |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
192 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
193 def __call__(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
194 raise NotImplementedError("Implement this!") |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
195 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
196 def fire(self, type, **attributes): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
197 event = Event(type, issuer = self, **attributes) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
198 self.scheduler.queue(event) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
199 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
200 class FnPlugin(Plugin): |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
201 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
202 def __init__(self, function): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
203 self.function = function |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
204 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
205 def __call__(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
206 return self.function(self, event) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
207 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
208 class DispatchPlugin(Plugin): |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
209 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
210 def __call__(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
211 getattr(self, "on_" + event.type, self.generic)(event) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
212 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
213 def generic(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
214 return |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
215 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
216 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
217 ################# |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
218 ### SCHEDULER ### |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
219 ################# |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
220 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
221 class Scheduler(object): |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
222 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
223 def __init__(self): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
224 self.plugins = [] |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
225 self.categorized = defaultdict(list) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
226 self.event_queue = deque() |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
227 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
228 def __call__(self): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
229 i = 0 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
230 evq = self.event_queue |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
231 self.queue(Event("begin", issuer = self)) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
232 while True: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
233 self.queue(Event("tick", issuer = self, time = i)) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
234 while evq: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
235 event = evq.popleft() |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
236 candidates = self.categorized[event.type] + self.categorized[None] |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
237 for event_template, plugin in candidates: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
238 if event.match(event_template): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
239 plugin(event) # note: the plugin might queue more events |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
240 if event.type == "terminate": |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
241 return |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
242 i += 1 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
243 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
244 def schedule_plugin(self, event_template, plugin): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
245 plugin = plugin.attach(self) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
246 if isinstance(event_template, Matcher) or isinstance(event_template.type, Matcher): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
247 # These plugins may execute upon any event type |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
248 self.categorized[None].append((event_template, plugin)) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
249 else: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
250 self.categorized[event_template.type].append((event_template, plugin)) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
251 self.plugins.append((event_template, plugin)) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
252 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
253 def queue(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
254 self.event_queue.append(event) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
255 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
256 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
257 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
258 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
259 @FnPlugin |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
260 def printer(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
261 print event |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
262 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
263 @FnPlugin |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
264 def stopper(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
265 self.fire("terminate") |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
266 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
267 @FnPlugin |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
268 def byebye(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
269 print "bye bye!" |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
270 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
271 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
272 @FnPlugin |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
273 def waiter(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
274 time.sleep(0.1) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
275 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
276 # @FnPlugin |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
277 # def timer(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
278 # if not hasattr(self, 'previous'): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
279 # self.beginning = time.time() |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
280 # self.previous = 0 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
281 # now = time.time() - self.beginning |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
282 # inow = int(now) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
283 # if inow > self.previous: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
284 # self.fire("second", time = inow) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
285 # self.previous = now |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
286 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
287 class Timer(DispatchPlugin): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
288 |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
289 def on_begin(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
290 self.beginning = time.time() |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
291 self.previous = 0 |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
292 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
293 def on_tick(self, event): |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
294 now = time.time() - self.beginning |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
295 inow = int(now) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
296 if inow > self.previous: |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
297 self.fire("second", time = inow) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
298 self.previous = now |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
299 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
300 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
301 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
302 sch = Scheduler() |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
303 |
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
304 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
305 sch.schedule_plugin(all_events, Timer()) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
306 sch.schedule_plugin(Event("tick"), waiter) # this means: execute the waiter plugin (a delay) on every "tick" event. Is it confusing to use Event(...)? |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
307 sch.schedule_plugin(Event("second"), printer) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
308 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
309 # sch.schedule_plugin(all_events, printer) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
310 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
311 sch.schedule_plugin(Event("tick", time = at(100)), stopper) |
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
312 sch.schedule_plugin(Event("terminate"), byebye) |
1118
8cc324f388ba
proposal for a plugin system
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
313 |
1135
a1957faecc9b
revised plugin interface and implementation
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
1118
diff
changeset
|
314 sch() |