-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScriptRunner.py
executable file
·1322 lines (1118 loc) · 48.4 KB
/
ScriptRunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""Code to run scripts that can wait for various things without messing up the main event loop
(and thus starving the rest of your program).
ScriptRunner allows your script to wait for the following:
- wait for a given time interval using: yield waitMS(...)
- run a slow computation as a background thread using waitThread
- run a command via the keyword dispatcher using waitCmd
- run multiple commands at the same time:
- start each command with startCmd,
- wait for one or more commands to finish using waitCmdVars
- wait for a keyword variable to be set using waitKeyVar
- wait for a sub-script by yielding it (i.e. yield subscript(...));
the sub-script must contain a yield for this to work; if it has no yield then just call it directly
An example is given as the test code at the end.
Code comments:
- Wait functions use a class to do all the work. This standardizes
some tricky internals (such as registering and deregistering
cancel functions) and allows the class to easily keep track
of private internal data.
- The wait class is created but is not explicitly kept around.
Why doesn't it immediately vanish? Because the wait class registers a method as a completion callback.
As long as somebody has a pointer to that method then the instance is kept alive.
- waitThread originally relied on generating an event when the script ended.
Unfortunately, that proved unreliable; if the thread was very short,
it could actually start trying to continue before the current
iteration of the generator was finished! I'm surprised that was
possible (I expected the event to get queued), but in any case
it was bad news. The current scheme is a kludge -- poll the thread.
I hope I can figure out something better.
History:
2004-08-12 ROwen
2004-09-10 ROwen Modified for RO.Wdg.Constants->RO.Constants.
Bug fix: _WaitMS cancel used afterID instead of self.afterID.
Bug fix: test for resume while wait callback pending was broken,
leading to false "You forgot the 'yield'" errors.
2004-10-01 ROwen Bug fix: waitKeyVar was totally broken.
2004-10-08 ROwen Bug fix: waitThread could fail if the thread was too short.
2004-12-16 ROwen Added a debug mode that prints diagnostics to stdout
and does not wait for commands or keyword variables.
2005-01-05 ROwen showMsg: changed level to severity.
2005-06-16 ROwen Changed default cmdStatusBar from statusBar to no bar.
2005-06-24 ROwen Changed to use new CmdVar.lastReply instead of .replies.
2005-08-22 ROwen Clarified _WaitCmdVars.getState() doc string.
2006-03-09 ROwen Added scriptClass argument to ScriptRunner.
2006-03-28 ROwen Modified to allow scripts to call subscripts.
2006-04-24 ROwen Improved error handling in _continue.
Bug fixes to debug mode:
- waitCmd miscomputed iterID
- startCmd dispatched commands
2006-11-02 ROwen Added checkFail argument to waitCmd and waitCmdVars methods.
waitCmd now returns the cmdVar in sr.value.
Added keyVars argument to startCmd and waitCmd.
2006-11-13 ROwen Added waitUser and resumeUser methods.
2006-12-12 ROwen Bug fix: start did not initialize waitUser instance vars.
Added initVars method to centralize initialization.
2008-04-21 ROwen Improved debug mode output:
- showMsg prints messages
- _setState prints requested state
- _end prints the end function
Added debugPrint method to simplify handling unicode errors.
2008-04-24 ROwen Bug fix: waitKeyVar referenced a nonexistent variable in non-debug mode.
2008-04-29 ROwen Fixed reporting of exceptions that contain unicode arguments.
2008-06-26 ROwen Renamed isAborting method to didFail (to be more consistent with isDone).
Added isPaused method.
Improved documentation for didFail and isDone methods.
Improved documentation for abortCmdStr and keyVars arguments to waitCmd.
2010-05-26 ROwen Tweaked to use _removeAllCallbacks() instead of nulling _callbacks.
2010-06-28 ROwen Made _WaitBase a modern class (thanks to pychecker).
Removed unused and broken internal method _waitEndFunc (thanks to pychecker).
2010-10-20 ROwen Tweaked waitCmd doc string.
2011-06-17 ROwen Changed "type" to "msgType" in parsed message dictionaries to avoid conflict with builtin.
2011-08-16 ROwen Commented out a diagnostic print statement.
2012-01-26 ROwen Write full state to stderr on unexpected errors.
2012-06-01 ROwen Use best effort to remove callbacks during cleanup, instead of raising an exception on failure.
Modified _WaitCmdVars to not try to register callbacks on commands that are finished,
and to not try to remove callbacks from CmdVars that are done.
2012-07-09 ROwen Made ScriptRunner argument "master" optional and moved later in argument list.
Modified to use RO.Comm.Generic.Timer.
2014-03-14 ROwen Changed default abortCmdStr from None to "".
2014-04-29 ROwen Bug fix: pause followed by resume lost the value returned by whatever was being paused.
2014-07-21 ROwen Added waitPause and waitSec.
2015-09-24 ROwen Replace "== None" with "is None" to modernize the code.
2015-11-03 ROwen Replace "!= None" with "is not None" to modernize the code.
2015-11-05 ROwen Changed ==/!= True/False to is/is not True/False to modernize the code.
"""
__all__ = ["ScriptError", "ScriptRunner"]
import sys
import threading
import queue
import traceback
import RO.AddCallback
import RO.Constants
import RO.KeyVariable
import RO.SeqUtil
import RO.StringUtil
from RO.Comm.Generic import Timer
# state constants
Ready = 2
Paused = 1
Running = 0
Done = -1
Cancelled = -2
Failed = -3
_DebugState = False
# a dictionary that describes the various values for the connection state
_StateDict = {
Ready: "Ready",
Paused: "Paused",
Running: "Running",
Done: "Done",
Cancelled: "Cancelled",
Failed: "Failed",
}
# internal constants
_PollDelaySec = 0.1 # polling interval for threads (sec)
# a list of possible keywords that hold reasons for a command failure
# in the order in which they are checked
_ErrKeys = ("text", "txt", "exposetxt")
class _Blank(object):
def __init__(self):
object.__init__(self)
class ScriptError (RuntimeError):
"""Use to raise exceptions in your script
when you don't want a traceback.
"""
pass
class ScriptRunner(RO.AddCallback.BaseMixin):
"""Execute a script.
Allows waiting for various things without messing up the main event loop.
"""
def __init__(self,
name,
runFunc = None,
scriptClass = None,
dispatcher = None,
master = None,
initFunc = None,
endFunc = None,
stateFunc = None,
startNow = False,
statusBar = None,
cmdStatusBar = None,
debug = False,
):
"""Create a ScriptRunner
Inputs:
- name script name; used to report status
- runFunc the main script function; executed whenever
the start button is pressed
- scriptClass a class with a run method and an optional end method;
if specified, runFunc, initFunc and endFunc may not be specified.
- dispatcher keyword dispatcher (opscore.actor.CmdKeyVarDispatcher);
required to use wait methods and startCmd.
- master master Tk widget; your script may grid or pack objects into this;
may be None for scripts that do not have widgets.
- initFunc function to call ONCE when the ScriptRunner is constructed
- endFunc function to call when runFunc ends for any reason
(finishes, fails or is cancelled); used for cleanup
- stateFunc function to call when the ScriptRunner changes state
- startNow if True, starts executing the script immediately
instead of waiting for user to call start.
- statusBar status bar, if available. Used by showMsg
- cmdStatusBar command status bar, if available.
Used to show the status of executing commands.
May be the same as statusBar.
- debug if True, startCmd and wait... print diagnostic messages to stdout
and there is no waiting for commands or keyword variables. Thus:
- waitCmd and waitCmdVars return success immediately
- waitKeyVar returns defVal (or None if not specified) immediately
All functions (runFunc, initFunc, endFunc and stateFunc) receive one argument: sr,
this ScriptRunner object. The functions can pass information using sr.globals,
an initially empty object (to which you can add instance variables and set or read them).
Only runFunc is allowed to call sr methods that wait.
The other functions may only run non-waiting code.
WARNING: when runFunc calls any of the ScriptRunner methods that wait,
IT MUST YIELD THE RESULT, as in:
def runFunc(sr):
...
yield sr.waitMS(500)
...
All such methods are marked "yield required".
If you forget to yield, your script will not wait. Your script will then halt
with an error message when it calls the next ScriptRunner method that involves waiting
(but by the time it gets that far it may have done some strange things).
If your script yields when it should not, it will simply halt.
"""
if scriptClass:
if runFunc or initFunc or endFunc:
raise ValueError("Cannot specify runFunc, initFunc or endFunc with scriptClass")
if not hasattr(scriptClass, "run"):
raise ValueError("scriptClass=%r has no run method" % scriptClass)
elif runFunc is None:
raise ValueError("Must specify runFunc or scriptClass")
#elif not callable(runFunc):
elif not hasattr(runFunc, '__call__'):
raise ValueError("runFunc=%r not callable" % (runFunc,))
self.runFunc = runFunc
self.name = name
self.dispatcher = dispatcher
self.master = master
self.initFunc = initFunc
self.endFunc = endFunc
self.debug = bool(debug)
self._statusBar = statusBar
self._cmdStatusBar = cmdStatusBar
# useful constant for script writers
self.ScriptError = ScriptError
RO.AddCallback.BaseMixin.__init__(self)
self.globals = _Blank()
self.initVars()
"""create a private widget and bind <Delete> to it
to kill the script when the master widget is destroyed.
This makes sure the script halts when the master toplevel closes
and avoids wait_variable hanging forever when the application is killed.
Binding <Destroy> to a special widget instead of master avoids two problems:
- If the user creates a widget and then destroys it
<Delete> would be called, mysteriously halting the script.
- When the master is deleted it also gets a <Delete> event for every
child widget. Thus the <Delete> binding would be called repeatedly,
which is needlessly inefficient.
"""
if self.master:
import tkinter
self._privateWdg = tkinter.Frame(self.master)
self._privateWdg.bind("<Destroy>", self.__del__)
if stateFunc:
self.addCallback(stateFunc)
# initialize, as appropriate
if scriptClass:
self.scriptObj = scriptClass(self)
self.runFunc = self.scriptObj.run
self.endFunc = getattr(self.scriptObj, "end", None)
elif self.initFunc:
res = self.initFunc(self)
#if hasattr(res, "next"):
if hasattr(res, "__next__"):
raise RuntimeError("init function tried to wait")
if startNow:
self.start()
# methods for starting, pausing and aborting script
# and for getting the current state of execution.
def cancel(self):
"""Cancel the script.
The script will not actually halt until the next
waitXXX or doXXX method is called, but this should
occur quickly.
"""
if self.isExecuting():
self._setState(Cancelled, "")
def debugPrint(self, msgStr):
"""Print the message to stdout if in debug mode.
Handles unicode as best it can.
"""
if not self.debug:
return
try:
print(msgStr)
except (TypeError, ValueError):
print(repr(msgStr))
def getFullState(self):
"""Returns the current state as a tuple:
- state: a numeric value; named constants are available
- stateStr: a short string describing the state
- reason: the reason for the state ("" if none)
"""
state, reason = self._state, self._reason
try:
stateStr = _StateDict[state]
except KeyError:
stateStr = "Unknown (%r)" % (state,)
return (state, stateStr, reason)
def getState(self):
"""Return the current state as a numeric value.
See the state constants defined in RO.ScriptRunner.
See also getFullState.
"""
return self._state
def initVars(self):
"""Initialize variables.
Call at construction and when starting a new run.
"""
self._cancelFuncs = []
self._endingState = None
self._state = Ready
self._reason = ""
self._iterID = [0]
self._iterStack = []
self._waiting = False # set when waiting for a callback
self._userWaitID = None
self.value = None
def didFail(self):
"""Return True if script aborted or failed.
Note: may not be fully ended (there may be cleanup to do and callbacks to call).
"""
return self._endingState in (Cancelled, Failed)
def isDone(self):
"""Return True if script is finished, successfully or otherwise.
Note: may not be fully ended (there may be cleanup to do and callbacks to call).
"""
return self._state <= Done
def isExecuting(self):
"""Returns True if script is running or paused."""
return self._state in (Running, Paused)
def isPaused(self):
"""Return True if script is paused
"""
return self._state == Paused
def pause(self):
"""Pause execution.
Note that the script must be waiting for something when the pause occurs
(because that's when the GUI will be freed up to get the request to pause).
If the thing being waited for fails then the script will fail (thus going
from Paused to Failed with no user interation).
Has no effect unless the script is running.
"""
self._printState("pause")
if not self._state == Running:
return
self._setState(Paused)
def resume(self):
"""Resume execution after a pause.
Has no effect if not paused.
"""
self._printState("resume")
if not self._state == Paused:
return
self._setState(Running)
if not self._waiting:
self._continue(self._iterID, val=self.value)
def resumeUser(self):
"""Resume execution from waitUser
"""
if self._userWaitID is None:
raise RuntimeError("Not in user wait mode")
iterID = self._userWaitID
self._userWaitID = None
self._continue(iterID)
def start(self):
"""Start executing runFunc.
If already running, raises RuntimeError
"""
print("script runner start")
if self.isExecuting():
print("script runner error")
raise RuntimeError("already executing")
if self._statusBar:
print("script runner statusBar sent msg ")
self._statusBar.setMsg("")
if self._cmdStatusBar:
print("script runner cmdbar sent msg ")
self._cmdStatusBar.setMsg("")
print("init vars")
self.initVars()
self._iterID = [0]
self._iterStack = []
print("script state running ")
self._setState(Running)
print("script runner continue " + str(self._iterID))
self._continue(self._iterID)
print("ending in the start here")
# methods for use in scripts
# with few exceptions all wait for something
# and thus require a "yield"
def getKeyVar(self,
keyVar,
ind=0,
defVal=Exception,
):
"""Return the current value of keyVar.
See also waitKeyVar, which can wait for a value.
Note: if you want to be sure the keyword data was in response to a particular command
that you sent, then use the keyVars argument of startCmd or waitCmd instead.
Do not use yield because it does not wait for anything.
Inputs:
- keyVar keyword variable
- ind which value is wanted? (None for all values)
- defVal value to return if value cannot be determined
(if omitted, the script halts)
"""
if self.debug:
argList = ["keyVar=%s" % (keyVar,)]
if ind != 0:
argList.append("ind=%s" % (ind,))
if defVal != Exception:
argList.append("defVal=%r" % (defVal,))
if defVal == Exception:
defVal = None
currVal, isCurrent = keyVar.get()
if isCurrent:
if ind is not None:
retVal = currVal[ind]
else:
retVal = currVal
else:
if defVal==Exception:
raise ScriptError("Value of %s invalid" % (keyVar,))
else:
retVal = defVal
if self.debug: # else argList does not exist
self.debugPrint("getKeyVar(%s); returning %r" % (", ".join(argList), retVal))
return retVal
def showMsg(self, msg, severity=RO.Constants.sevNormal):
"""Display a message--on the status bar, if available,
else sys.stdout.
Do not use yield because it does not wait for anything.
Inputs:
- msg: string to display, without a final \n
- severity: one of RO.Constants.sevNormal (default), sevWarning or sevError
"""
if self._statusBar:
self._statusBar.setMsg(msg, severity)
self.debugPrint(msg)
else:
print(msg)
def startCmd(self,
actor="",
cmdStr = "",
timeLim = 0,
callFunc = None,
callTypes = RO.KeyVariable.DoneTypes,
timeLimKeyword = None,
abortCmdStr = "",
keyVars = None,
checkFail = True,
):
"""Start a command using the same arguments as waitCmd.
Inputs: same as waitCmd, which see.
Returns a command variable that you can wait for using waitCmdVars.
Do not use yield because it does not wait for anything.
"""
cmdVar = RO.KeyVariable.CmdVar(
actor=actor,
cmdStr = cmdStr,
timeLim = timeLim,
callFunc = callFunc,
callTypes = callTypes,
timeLimKeyword = timeLimKeyword,
abortCmdStr = abortCmdStr,
keyVars = keyVars,
)
print("we started a command")
if checkFail:
cmdVar.addCallback(
callFunc = self._cmdFailCallback,
callTypes = RO.KeyVariable.FailTypes,
)
if self.debug:
argList = ["actor=%r, cmdStr=%r" % (actor, cmdStr)]
if timeLim != 0:
argList.append("timeLim=%s" % (timeLim,))
if callFunc is not None:
argList.append("callFunc=%r" % (callFunc,))
if callTypes != RO.KeyVariable.DoneTypes:
argList.append("callTypes=%r" % (callTypes,))
if timeLimKeyword is not None:
argList.append("timeLimKeyword=%r" % (timeLimKeyword,))
if abortCmdStr:
argList.append("abortCmdStr=%r" % (abortCmdStr,))
if checkFail is not True:
argList.append("checkFail=%r" % (checkFail,))
self.debugPrint("startCmd(%s)" % (", ".join(argList),))
self._showCmdMsg("%s started" % cmdStr)
# set up command completion callback
def endCmd(self=self, cmdVar=cmdVar):
endMsgDict = self.dispatcher.makeMsgDict(
cmdr = None,
actor = cmdVar.actor,
msgType = ":",
)
cmdVar.reply(endMsgDict)
msgStr = "%s finished" % cmdVar.cmdStr
self._showCmdMsg(msgStr)
Timer(1.0, endCmd)
else:
if self._cmdStatusBar:
self._cmdStatusBar.doCmd(cmdVar)
else:
self.dispatcher.executeCmd(cmdVar)
return cmdVar
def waitCmd(self,
actor="",
cmdStr = "",
timeLim = 0,
callFunc=None,
callTypes = RO.KeyVariable.DoneTypes,
timeLimKeyword = None,
abortCmdStr = "",
keyVars = None,
checkFail = True,
):
print("it is a wait command")
"""Start a command and wait for it to finish.
Returns the cmdVar in sr.value.
A yield is required.
Inputs:
- actor: the name of the device to command
- cmdStr: the command (without a terminating \n)
- timeLim: maximum time before command expires, in sec; 0 for no limit
- callFunc: a function to call when the command changes state;
see below for details.
- callTypes: the message types for which to call the callback;
a string of one or more choices; see RO.KeyVariable.TypeDict for the choices;
useful constants include DoneTypes (command finished or failed)
and AllTypes (all message types, thus any reply).
Not case sensitive (the string you supply will be lowercased).
- timeLimKeyword: a keyword specifying a delta-time by which the command must finish
- abortCmdStr: a command string that will abort the command. This string is sent to the actor
if the command is aborted, e.g. if the script is cancelled while the command is executing.
- keyVars: a sequence of 0 or more keyword variables to monitor.
Any data for those variables that arrives IN RESPONSE TO THIS COMMAND is saved
and can be retrieved using cmdVar.getKeyVarData or cmdVar.getLastKeyVarData,
where cmdVar is returned in sr.value.
- checkFail: check for command failure?
if True (the default) command failure will halt your script
Callback arguments:
msgType: the message type, a character (e.g. "i", "w" or ":");
see RO.KeyVariable.TypeDict for the various types.
msgDict: the entire message dictionary
cmdVar (by name): the key command object
see RO.KeyVariable.CmdVar for details
Note: timeLim and timeLimKeyword work together as follows:
- The initial time limit for the command is timeLim
- If timeLimKeyword is seen before timeLim seconds have passed
then self.maxEndTime is updated with the new value
Also the time limit is a lower limit. The command is guaranteed to
expire no sooner than this but it may take a second longer.
"""
if isinstance(actor, RO.KeyVariable.CmdVar):
raise RuntimeError("waitCmd error: actor must be a string; did you mean to call waitCmdVars? actor=%s" % (actor,))
self._waitCheck(setWait = False)
self.debugPrint("waitCmd calling startCmd")
cmdVar = self.startCmd (
actor = actor,
cmdStr = cmdStr,
timeLim = timeLim,
callFunc = callFunc,
callTypes = callTypes,
timeLimKeyword = timeLimKeyword,
abortCmdStr = abortCmdStr,
keyVars = keyVars,
checkFail = False,
)
self.waitCmdVars(cmdVar, checkFail=checkFail, retVal=cmdVar)
def waitCmdVars(self, cmdVars, checkFail=True, retVal=None):
"""Wait for one or more command variables to finish.
Command variables are the objects returned by startCmd.
A yield is required.
Returns successfully if all commands succeed.
Fails as soon as any command fails.
Inputs:
- one or more command variables (RO.KeyVariable.CmdVar objects)
- checkFail: check for command failure?
if True (the default) command failure will halt your script
- retVal: value to return at the end; defaults to None
"""
_WaitCmdVars(self, cmdVars, checkFail=checkFail, retVal=retVal)
def waitKeyVar(self,
keyVar,
ind=0,
defVal=Exception,
waitNext=False,
):
"""Get the value of keyVar in self.value.
If it is currently unknown or if waitNext is true,
wait for the variable to be updated.
See also getKeyVar (which does not wait).
A yield is required.
Inputs:
- keyVar keyword variable
- ind which value is wanted? (None for all values)
- defVal value to return if value cannot be determined
(if omitted, the script halts)
- waitNext if True, ignores the current value and waits
for the next transition.
"""
_WaitKeyVar(
scriptRunner = self,
keyVar = keyVar,
ind = ind,
defVal = defVal,
waitNext = waitNext,
)
def waitMS(self, msec):
"""Wait for msec milliseconds.
A yield is required.
Inputs:
- msec number of milliseconds to pause
"""
self.debugPrint("waitMS(msec=%s)" % (msec,))
_WaitMS(self, msec)
def waitSec(self, sec):
"""Wait for sec seconds.
A yield is required.
Inputs:
- sec number of seconds to pause
"""
self.debugPrint("waitSec(sec=%s)" % (sec,))
_WaitMS(self, sec * 1000)
def waitPause(self, msgStr="Paused", severity=RO.Constants.sevNormal):
"""Pause execution and wait
A no-op if not running
"""
Timer(0, self.showMsg, msgStr, severity=severity)
self.pause()
def waitThread(self, func, *args, **kargs):
"""Run func as a background thread, waits for completion
and sets self.value = the result of that function call.
A yield is required.
Warning: func must NOT interact with Tkinter widgets or variables
(not even reading them) because Tkinter is not thread-safe.
(The only thing I'm sure a background thread can safely do with Tkinter
is generate an event, a technique that is used to detect end of thread).
"""
print("WAIT THREAD ")
self.debugPrint("waitThread(func=%r, args=%s, keyArgs=%s)" % (func, args, kargs))
_WaitThread(self, func, *args, **kargs)
def waitUser(self):
"""Wait until resumeUser called.
Typically used if waiting for user input
but can be used for any external trigger.
"""
self._waitCheck(setWait=True)
if self._userWaitID is not None:
raise RuntimeError("Already in user wait mode")
self._userWaitID = self._getNextID()
# private methods
def _cmdFailCallback(self, msgType, msgDict, cmdVar):
"""Use as a callback for when an asynchronous command fails.
"""
# print "ScriptRunner._cmdFailCallback(%r, %r, %r)" % (msgType, msgDict, cmdVar)
if not msgType in RO.KeyVariable.FailTypes:
errMsg = "Bug! RO.ScriptRunner._cmdFail(%r, %r, %r) called for non-failed command" % (msgType, msgDict, cmdVar)
raise RuntimeError(errMsg)
MaxLen = 10
if len(cmdVar.cmdStr) > MaxLen:
cmdDescr = "%s %s..." % (cmdVar.actor, cmdVar.cmdStr[0:MaxLen])
else:
cmdDescr = "%s %s" % (cmdVar.actor, cmdVar.cmdStr)
for key, values in msgDict.get("data", {}).items():
if key.lower() in _ErrKeys:
reason = values[0]
break
else:
reason = msgDict.get("data")
if not reason:
reason = str(msgDict)
self._setState(Failed, reason="%s failed: %s" % (cmdDescr, reason))
def _continue(self, iterID, val=None):
"""Continue executing the script.
Inputs:
- iterID: ID of iterator that is continuing
- val: self.value is set to val
"""
print("in continue val: " + str(val))
self._printState("_continue(%r, %r)" % (iterID, val))
if not self.isExecuting():
raise RuntimeError('%s: bug! _continue called but script not executing' % (self,))
try:
if iterID != self._iterID:
#print "Warning: _continue called with iterID=%s; expected %s" % (iterID, self._iterID)
raise RuntimeError("%s: bug! _continue called with bad id; got %r, expected %r" % (self, iterID, self._iterID))
self.value = val
self._waiting = False
if self._state == Paused:
#print "_continue: still paused"
return
if not self._iterStack:
# just started; call run function,
# and if it's an iterator, put it on the stack
print("about to call run func: " + str(self.runFunc) + " type: " + str(type(self.runFunc)))
res = self.runFunc(self)
print("called run func with result res " + str(res))
#if not hasattr(res, "next"):
if not hasattr(res, "__next__"):
# function was a function, not a generator; all done
print("zomg all done")
self._setState(Done)
return
self._iterStack = [res]
self._printState("_continue: before iteration")
self._state = 0
possIter = next(self._iterStack[-1])
#if hasattr(possIter, "next"):
if hasattr(possIter, "__next__"):
self._iterStack.append(possIter)
self._iterID = self._getNextID(addLevel = True)
# print "Iteration yielded an iterator"
self._continue(self._iterID)
else:
self._iterID = self._getNextID()
self._printState("_continue: after iteration")
except StopIteration:
print("stop iteration")
# print "StopIteration seen in _continue"
self._iterStack.pop(-1)
if not self._iterStack:
print("stop exception")
self._setState(Done)
else:
self._continue(self._iterID, val=self.value)
except KeyboardInterrupt:
print("keyboard interrupt")
self._setState(Cancelled, "keyboard interrupt")
except SystemExit:
print("system exit")
self.__del__()
sys.exit(0)
except ScriptError as e:
print("script error")
self._setState(Failed, RO.StringUtil.strFromException(e))
except Exception as e:
print("exception")
traceback.print_exc(file=sys.stderr)
self._printFullState()
self._setState(Failed, RO.StringUtil.strFromException(e))
def _printState(self, prefix):
"""Print the state at various times.
Ignored unless _DebugState or self.debug true.
"""
if _DebugState:
print("Script %s: %s: state=%s, iterID=%s, waiting=%s, iterStack depth=%s" % \
(self.name, prefix, self._state, self._iterID, self._waiting, len(self._iterStack)))
def _printFullState(self):
"""Print the full state to stderr
"""
sys.stderr.write("self.name=%s, self._state=%s, self._iterID=%r, self._waiting=%r, self._userWaitID=%r, self.value=%r\n" % \
(self.name, self._state, self._iterID, self._waiting, self._userWaitID, self.value))
sys.stderr.write("self._iterStack=%r\n" % (self._iterStack,))
def _showCmdMsg(self, msg, severity=RO.Constants.sevNormal):
"""Display a message--on the command status bar, if available,
else sys.stdout.
Do not use yield because it does not wait for anything.
Inputs:
- msg: string to display, without a final \n
- severity: one of RO.Constants.sevNormal (default), sevWarning or sevError
"""
if self._cmdStatusBar:
self._cmdStatusBar.setMsg(msg, severity)
else:
print(msg)
def __del__(self, evt=None):
"""Called just before the object is deleted.
Deletes any state callbacks and then cancels script execution.
The evt argument is ignored, but allows __del__ to be
called from a Tk event binding.
"""
self._removeAllCallbacks()
self.cancel()
def _end(self):
"""Call the end function (if any).
"""
# Warning: this code must not execute _setState or __del__
# to avoid infinite loops. It also need not execute _cancelFuncs.
if self.endFunc:
self.debugPrint("ScriptRunner._end: calling end function")
try:
res = self.endFunc(self)
#if hasattr(res, "next"):
if hasattr(res, "__next__"):
self._state = Failed
self._reason = "endFunc tried to wait"
except KeyboardInterrupt:
self._state = Cancelled
self._reason = "keyboard interrupt"
except SystemExit:
raise
except Exception as e:
print("end exception")
self._state = Failed
self._reason = "endFunc failed: %s" % (RO.StringUtil.strFromException(e),)
traceback.print_exc(file=sys.stderr)
else:
self.debugPrint("ScriptRunner._end: no end function to call")
def _getNextID(self, addLevel=False):
"""Return the next iterator ID"""
self._printState("_getNextID(addLevel=%s)" % (addLevel,))
newID = self._iterID[:]
if addLevel:
newID += [0]
else:
newID[-1] = (newID[-1] + 1) % 10000
return newID
def _setState(self, newState, reason=None):
"""Update the state of the script runner.
If the new state is Cancelled or Failed
then any existing cancel function is called
to abort outstanding callbacks.
"""
print("set state " + str(newState) + "reason: " + str(reason))
self._printState("_setState(%r, %r)" % (newState, reason))
if self.debug:
newStateName = _StateDict.get(newState, "?")
self.debugPrint("ScriptRunner._setState(newState=%s=%s, reason=%r)" % (newState, newStateName, reason))
# if ending, clean up appropriately
if self.isExecuting() and newState <= Done:
self._endingState = newState
# if aborting and a cancel function exists, call it
if newState < Done:
for func in self._cancelFuncs:
# print "%s _setState calling cancel function %r" % (self, func)
func()
self._cancelFuncs = []
self._end()
print("really setting state to: " + str(newState))
self._state = newState
if reason is not None:
self._reason = reason
print("Do call backs")
self._doCallbacks()
def __str__(self):
"""String representation of script"""
return "script %s" % (self.name,)
def _waitCheck(self, setWait=False):
"""Verifies that the script runner is running and not already waiting
(as can easily happen if the script is missing a "yield").
Call at the beginning of every waitXXX method.
Inputs:
- setWait: if True, sets the _waiting flag True
"""
if self._state != Running:
raise RuntimeError("Tried to wait when not running")
if self._waiting:
raise RuntimeError("Already waiting; did you forget the 'yield' when calling a ScriptRunner method?")
if setWait:
self._waiting = True
class _WaitBase(object):
"""Base class for waiting.
Handles verifying iterID, registering the termination function,
registering and unregistering the cancel function, etc.
"""
def __init__(self, scriptRunner):
scriptRunner._printState("%s init" % (self.__class__.__name__))
scriptRunner._waitCheck(setWait = True)
self.scriptRunner = scriptRunner
self.master = scriptRunner.master
self._iterID = scriptRunner._getNextID()
self.scriptRunner._cancelFuncs.append(self.cancelWait)
def cancelWait(self):
"""Call to cancel waiting.
Perform necessary cleanup but do not set state.
Subclasses can override and should usually call cleanup.
"""
self.cleanup()
def fail(self, reason):
"""Call if waiting fails.
"""
# report failure; this causes the scriptRunner to call
# all pending cancelWait functions, so don't do that here