1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
25 """The ExtScheduler ist used for time based events that are not part of the simulation(gui, menu, scrolling).
26 To start a timed callback, call add_new_object() to make the TimingThread Class create a CallbackObject for you.
27 @param pump: pump list the schedular registers itself with.
30 def __init__(self
, pump
):
33 self
.pump
.append(self
.tick
)
37 @param tick_id: int id of the tick.
39 for tup
in self
.schedule
:
40 if tup
[0] <= time
.time():
41 object = self
.schedule
.pop(0)[1]
43 if object.loops
> 0 or object.loops
is -1:
44 self
.add_object(object) # re-add object
48 def add_object(self
, object):
49 """Adds a new CallbackObject instance to the callbacks list
50 @param object: CallbackObject type object, containing all neccessary information
54 self
.schedule
.append(((time
.time() + object.runin
), object))
57 def add_new_object(self
, callback
, class_instance
, runin
=1, loops
=1):
58 """Creates a new CallbackObject instance and calls the self.add_object() function.
59 @param callback: function callback, which is called runin time.
60 @param class_instance: class instance the function belongs to.
61 @param runin: float number of seconds after which the callback is called. Standard is 1, run next tick.
62 @param loops: How often the callback is called. -1 = infinit times. Standard is 1, run once."""
63 object = CallbackObject(callback
, class_instance
, runin
, loops
)
64 self
.add_object(object)
66 def rem_all_classinst_calls(self
, class_instance
):
67 """Removes all callbacks from the scheduler that belong to the class instance class_inst."""
68 for tup
in self
.schedule
:
69 if tup
[1].class_instance
is class_instance
:
70 self
.schedule
.remove(tup
)
74 self
.pump
.remove(self
.tick
)
78 class CallbackObject(object):
79 """Class used by the ExtScheduler Class to organize callbacks."""
80 def __init__(self
, callback
, class_instance
, runin
=1, loops
=1):
81 """Creates the CallbackObject instance.
82 @param callback: lambda function callback, which is called runin ticks.
83 @param class_instance: class instance the original function(not the lambda function!) belongs to.
84 @param runin: int number of ticks after which the callback is called. Standard is 1, run next tick.
85 @param loops: How often the callback is called. -1 = infinit times. Standard is 1, run once.
87 self
.callback
= callback
88 self
.class_instance
= class_instance