1 | import inspect |
---|
2 | import wx |
---|
3 | from wx import Timer |
---|
4 | import wx._misc |
---|
5 | |
---|
6 | def call_later_fix(): |
---|
7 | # TODO: test if we need the fix |
---|
8 | wx.CallLater = CallLater |
---|
9 | wx.FutureCall = FutureCall |
---|
10 | wx.PyTimer = PyTimer |
---|
11 | |
---|
12 | def trace_new_id(): |
---|
13 | wx.NewId = NewId |
---|
14 | |
---|
15 | def NewId(): |
---|
16 | id = wx._misc.NewId() |
---|
17 | frame = inspect.stack()[1] |
---|
18 | path = frame[1] |
---|
19 | index = path.find('/sas/') |
---|
20 | if index == -1: index = path.find('\\sas\\') |
---|
21 | path = path[index+1:] |
---|
22 | print "NewId %d from %s(%d):%s"%(id, path, frame[2], frame[3]) |
---|
23 | return id |
---|
24 | |
---|
25 | |
---|
26 | # ========================================================================== |
---|
27 | # Hacked versions of CallLater and PyTimer so that the main GUI loop doesn't |
---|
28 | # eat wx ids. |
---|
29 | # Changed lines are marked #PAK |
---|
30 | # ========================================================================== |
---|
31 | |
---|
32 | # For backwards compatibility with 2.4 |
---|
33 | class PyTimer(Timer): |
---|
34 | def __init__(self, notify, *args, **kw): #PAK |
---|
35 | Timer.__init__(self, *args, **kw) #PAK |
---|
36 | self.notify = notify |
---|
37 | |
---|
38 | def Notify(self): |
---|
39 | if self.notify: |
---|
40 | self.notify() |
---|
41 | |
---|
42 | |
---|
43 | class CallLater: |
---|
44 | """ |
---|
45 | A convenience class for `wx.Timer`, that calls the given callable |
---|
46 | object once after the given amount of milliseconds, passing any |
---|
47 | positional or keyword args. The return value of the callable is |
---|
48 | availbale after it has been run with the `GetResult` method. |
---|
49 | |
---|
50 | If you don't need to get the return value or restart the timer |
---|
51 | then there is no need to hold a reference to this object. |
---|
52 | |
---|
53 | :see: `wx.CallAfter` |
---|
54 | """ |
---|
55 | |
---|
56 | __RUNNING = set() |
---|
57 | |
---|
58 | def __init__(self, millis, callableObj, *args, **kwargs): |
---|
59 | # print "=================== entering CallLater constructor" |
---|
60 | assert callable(callableObj), "callableObj is not callable" |
---|
61 | self.millis = millis |
---|
62 | self.callable = callableObj |
---|
63 | self.SetArgs(*args, **kwargs) |
---|
64 | self.runCount = 0 |
---|
65 | self.running = False |
---|
66 | self.hasRun = False |
---|
67 | self.result = None |
---|
68 | self.timer = None |
---|
69 | self.id = wx.NewId() # PAK |
---|
70 | self.Start() |
---|
71 | |
---|
72 | |
---|
73 | def Start(self, millis=None, *args, **kwargs): |
---|
74 | """ |
---|
75 | (Re)start the timer |
---|
76 | """ |
---|
77 | self.hasRun = False |
---|
78 | if millis is not None: |
---|
79 | self.millis = millis |
---|
80 | if args or kwargs: |
---|
81 | self.SetArgs(*args, **kwargs) |
---|
82 | self.Stop() |
---|
83 | self.timer = PyTimer(self.Notify, id=self.id) # PAK |
---|
84 | self.timer.Start(self.millis, wx.TIMER_ONE_SHOT) |
---|
85 | self.running = True |
---|
86 | self.__RUNNING.add(self) |
---|
87 | Restart = Start |
---|
88 | |
---|
89 | |
---|
90 | def Stop(self): |
---|
91 | """ |
---|
92 | Stop and destroy the timer. |
---|
93 | """ |
---|
94 | if self.timer is not None: |
---|
95 | self.timer.Stop() |
---|
96 | self.timer = None |
---|
97 | self.__RUNNING.discard(self) |
---|
98 | |
---|
99 | |
---|
100 | def GetInterval(self): |
---|
101 | if self.timer is not None: |
---|
102 | return self.timer.GetInterval() |
---|
103 | else: |
---|
104 | return 0 |
---|
105 | |
---|
106 | |
---|
107 | def IsRunning(self): |
---|
108 | return self.timer is not None and self.timer.IsRunning() |
---|
109 | |
---|
110 | |
---|
111 | def SetArgs(self, *args, **kwargs): |
---|
112 | """ |
---|
113 | (Re)set the args passed to the callable object. This is |
---|
114 | useful in conjunction with Restart if you want to schedule a |
---|
115 | new call to the same callable object but with different |
---|
116 | parameters. |
---|
117 | """ |
---|
118 | self.args = args |
---|
119 | self.kwargs = kwargs |
---|
120 | |
---|
121 | |
---|
122 | def HasRun(self): |
---|
123 | return self.hasRun |
---|
124 | |
---|
125 | |
---|
126 | def GetResult(self): |
---|
127 | return self.result |
---|
128 | |
---|
129 | |
---|
130 | def Notify(self): |
---|
131 | """ |
---|
132 | The timer has expired so call the callable. |
---|
133 | """ |
---|
134 | if self.callable and getattr(self.callable, 'im_self', True): |
---|
135 | self.runCount += 1 |
---|
136 | self.running = False |
---|
137 | self.result = self.callable(*self.args, **self.kwargs) |
---|
138 | self.hasRun = True |
---|
139 | if not self.running: |
---|
140 | # if it wasn't restarted, then cleanup |
---|
141 | wx.CallAfter(self.Stop) |
---|
142 | |
---|
143 | Interval = property(GetInterval) |
---|
144 | Result = property(GetResult) |
---|
145 | |
---|
146 | |
---|
147 | class FutureCall(CallLater): |
---|
148 | """A compatibility alias for `CallLater`.""" |
---|