CscCommander¶
-
class
lsst.ts.salobj.
CscCommander
(name, index=0, enable=False, exclude=None, fields_to_ignore=('ignored', 'value', 'priority'))¶ Bases:
object
Command a CSC from the command line.
Parameters: - name :
str
SAL component name of CSC.
- index :
int
, optional SAL index of CSC.
- exclude :
List
[str
] orNone
, optional Names of telemetry or event topics to not print. If
None
or empty then no topics are excluded.- fields_to_ignore :
List
[str
], optional SAL topic fields to ignore when specifying command parameters, and when printing events and telemetry.
- enable :
bool
, optional Enable the CSC (when the commander starts up)? Note:
amain
always supplies this argument.
Notes
Warning: use with caution. Running a commander may interfere with telescope operations! If you provide a command-line script in
bin/
that runs a commander, consider picking a name that is obscure and not easily confused with the script that runs the CSC.Make a subclass of
CscCommander
if you want to add extra commands or provide special handling for some commands, events or telemetry. Subclasses may provide overrides as follows:To hide unwanted commands: delete them from
self.command_dict
in your constructor. Most CSCs should hide the “abort”, “enterControl” and “setValue” commands, as follows:for command_to_ignore in ("abort", "enterControl", "setValue"): del self.command_dict(command_to_ignore)
To override handling of a standard command (one defined in the XML): define a
do_<command_name>
method. The method receives one argument: a list of str arguments. You should provide a custom handler for, or hide, any command with array parameters, because the command parser only accepts scalars.To add an additional command (one not defined in the XML):
Define a
do_<command_name>
to handle the command. The method receives one argument: a list of str arguments.Add an entry to
help_dict
. The key is the command name and the value is a brief (preferably only one line) help string that lists the arguments first, and a brief description after. Here is an example:self.help_dict["sine"] = "start_position amplitude " "# track one cycle of a sine wave",
To override handling of an event or telemetry topic: define method
evt_<event_name>_callback
ortel_<event_name>_callback
, respectively. It receives one argument: the DDS sample. This can be especially useful if the default behavior is too chatty for one or more telemetry topics.
I have not found a way to write a unit test for this class. I tried running a commander in a subprocess but could not figure out how to send multiple commands (the
suprocess.communicate
method only allows sending one item of data). Instead I suggest manually running it to control the Test CSC.Examples
If the default behavior suffices, all you need is a command-line script such as the following:
import asyncio from lsst.ts import salobj asyncio.run(salobj.CscCommander.amain(name="Test", index=True))
For an example with extra commands and special telemetry handling, see
RotatorCommander
in the ts_rotator package.Attributes: - domain :
Domain
DDS domain.
- remote :
Remote
Remote for the CSC being commanded.
- help_dict :
dict
Dict of command_name: documentation. You should add one entry for every do_command method you define. Each documentation string should start with a list of argument names (the command name will be prepended for you) optionally followed by # and a brief description. The documentation string should a single line, if practical.
Methods Summary
add_arguments
(parser)Add arguments to the parser created by make_from_cmd_line
.add_kwargs_from_args
(args, kwargs)Add constructor keyword arguments based on parsed arguments. amain
(*, index, **kwargs)Construct the commander and run it. check_arguments
(args, *names)Check that the required arguments are provided, and return them as a keyword argument dict with cast values. close
()Close the commander, prior to quitting. do_start
(args)Allow the start command to have no arguments. event_callback
(data, name)Generic callback for events. evt_summaryState_callback
(data)field_is_public
(name)Return True if the specified field name is public, False otherwise. format_data
(data)Format an event or telemetry sample for printing. format_item
(key, value)Format one event or telemetry field for printing. get_commands_help
()Get help for each command, as a list of strings. get_public_data
(data)Return a dict of field_name: value for public fields. get_rounded_public_fields
(data[, digits])Get the public fields for a sample, with float values rounded. make_from_cmd_line
(index, **kwargs)Construct a SAL-related class from command line arguments. print_help
()Print help. run_command_topic
(command_name, args)Run a command that has an associated salobj RemoteCommand topic. run_coroutine
(coro, command_name)Run a coroutine and manage the task in self.tasks. start
()Start asynchonous processes. telemetry_callback
(data, name)Generic callback for telemetry. Methods Documentation
-
classmethod
add_arguments
(parser)¶ Add arguments to the parser created by
make_from_cmd_line
.Parameters: - parser :
argparse.ArgumentParser
The argument parser.
Notes
If you override this method then you should almost certainly override
add_kwargs_from_args
as well.- parser :
-
classmethod
add_kwargs_from_args
(args, kwargs)¶ Add constructor keyword arguments based on parsed arguments.
Parameters: - args :
argparse.namespace
Parsed command.
- kwargs :
dict
Keyword argument dict for the constructor. Update this based on
args
. The index argument will already be present if relevant.
Notes
If you override this method then you should almost certainly override
add_arguments
as well.- args :
-
classmethod
amain
(*, index, **kwargs)¶ Construct the commander and run it.
Parse the command line to construct the commander, then parse and execute commands until the
exit
is seen.Parameters:
-
check_arguments
(args, *names)¶ Check that the required arguments are provided, and return them as a keyword argument dict with cast values.
Parameters: - args :
List
[str
] Command arguments, as strings.
- *names :
List
[str
ortuple
] Argument name and optional cast function. Each element is either:
- An argument name, in which case the argument is cast to a float
- A tuple of (name, cast function), in which case the argument
- is cast using the cast function.
- args :
-
close
()¶ Close the commander, prior to quitting.
-
do_start
(args)¶ Allow the start command to have no arguments.
-
event_callback
(data, name)¶ Generic callback for events.
You may provide evt_<event_name> methods to override printing of specific events.
-
evt_summaryState_callback
(data)¶
-
field_is_public
(name)¶ Return True if the specified field name is public, False otherwise.
-
format_data
(data)¶ Format an event or telemetry sample for printing.
-
format_item
(key, value)¶ Format one event or telemetry field for printing.
-
get_commands_help
()¶ Get help for each command, as a list of strings.
End with “Other Commands:” and any commands in help_dict that are not in command_dict.
-
get_public_data
(data)¶ Return a dict of field_name: value for public fields.
Parameters: - data :
dds_sample
DDS sample.
- data :
-
get_rounded_public_fields
(data, digits=4)¶ Get the public fields for a sample, with float values rounded.
-
classmethod
make_from_cmd_line
(index, **kwargs)¶ Construct a SAL-related class from command line arguments.
Parameters: - index :
int
,True
,False
orNone
If the SAL component is indexed: specify
True
to make index a required command line argument, or specify a non-zeroint
to use that index. If the SAL component is not indexed: specifyNone
or 0.- **kwargs :
dict
, optional Additional keyword arguments for your class’s constructor.
Returns: - instance :
cls
The constructed instance.
Notes
To add additional command-line arguments, override
add_arguments
andadd_kwargs_from_args
.- index :
-
print_help
()¶ Print help.
-
run_command_topic
(command_name, args)¶ Run a command that has an associated salobj RemoteCommand topic.
Parameters: Notes
This method works for command topics that take scalar arguments. To support command topics with more exotic arguments you must provide a do_<command> method that parses the arguments and add an entry to self.help_dict.
-
run_coroutine
(coro, command_name)¶ Run a coroutine and manage the task in self.tasks.
This allows the task to be cancelled in
close
.Parameters: - coro : awaitable
Coroutine or other awaitable.
-
start
()¶ Start asynchonous processes.
-
telemetry_callback
(data, name)¶ Generic callback for telemetry.
You may provide tel_<telemetry_name> methods to override printing of specific telemetry topics.
- name :