Merge commit 'wmcbrine/master'
[pyTivo/TheBayer.git] / stager.py
blob0a5376bbb230d2e1af928360be8837240c225b7f
1 # Module: stager.py
2 # Author: Eric von Bayer
3 # Contact:
4 # Date: August 18, 2009
5 # Description:
6 # Module to handle deferred callbacks in stages.
8 # Copyright (c) 2009, Eric von Bayer
9 # All rights reserved.
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions are met:
14 # * Redistributions of source code must retain the above copyright notice,
15 # this list of conditions and the following disclaimer.
16 # * Redistributions in binary form must reproduce the above copyright notice,
17 # this list of conditions and the following disclaimer in the documentation
18 # and/or other materials provided with the distribution.
19 # * The names of the contributors may not be used to endorse or promote
20 # products derived from this software without specific prior written
21 # permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # Class to hold a stage
35 class Stage(object):
36 """An individual stage to be executed at a later time"""
38 # Initialize the stage
39 def __init__(self):
40 self.__callbacks = list()
41 self.__done = False
43 # Execute all the callbacks in the stage
44 def Execute(self):
45 if self.__done == False:
46 self.__done = True
47 for call in self.__callbacks:
48 call()
49 del self.__callbacks
51 # Add a callback to the stage
52 def AddCallback(self, call):
53 if self.__done == True:
54 call()
55 else:
56 self.__callbacks.append( call )
58 # Internal storage for the stages
59 stager_stages = dict()
61 # Execute the named stage
62 def Execute(name):
63 if name in stager_stages:
64 stager_stages[name].Execute()
66 # Add a callback to the named stage
67 def AddCallback(name, call):
68 if not name in stager_stages:
69 stager_stages[name] = Stage()
71 stager_stages[name].AddCallback( call )