-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdeferredcommand.py
63 lines (50 loc) · 2.03 KB
/
deferredcommand.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
# validated: 2024-01-24 DS 192a28af4731 DeferredCommand.java
from typing import Callable
from wpiutil import SendableBuilder
from .command import Command
from .commandscheduler import CommandScheduler
from .printcommand import PrintCommand
from .subsystem import Subsystem
class DeferredCommand(Command):
"""
Defers Command construction to runtime. Runs the command returned by the supplier when this
command is initialized, and ends when it ends. Useful for performing runtime tasks before
creating a new command. If this command is interrupted, it will cancel the command.
Note that the supplier *must* create a new Command each call. For selecting one of a
preallocated set of commands, use :class:`commands2.SelectCommand`.
"""
def __init__(self, supplier: Callable[[], Command], *requirements: Subsystem):
"""
:param supplier: The command supplier
:param requirements: The command requirements.
"""
super().__init__()
assert callable(supplier)
self._null_command = PrintCommand(
f"[DeferredCommand] Supplied command (from {supplier!r} was None!"
)
self._supplier = supplier
self._command = self._null_command
self.addRequirements(*requirements)
def initialize(self):
cmd = self._supplier()
if cmd is not None:
self._command = cmd
CommandScheduler.getInstance().registerComposedCommands([self._command])
self._command.initialize()
def execute(self):
self._command.execute()
def isFinished(self):
return self._command.isFinished()
def end(self, interrupted):
self._command.end(interrupted)
self._command = self._null_command
def initSendable(self, builder: SendableBuilder):
super().initSendable(builder)
builder.addStringProperty(
"deferred",
lambda: "null"
if self._command is self._null_command
else self._command.getName(),
lambda _: None,
)