Writing a task script
When you write a Python script that runs with srsgui, you make a subclass of
Task class, and implement
setup,
test and
cleanup.
Following is the simplest form of a task. Even though it does not do much,
srsgui is happy to run the task, if it is included in a .taskconfig file.
from srsgui import Task
class ZerothTask(Task):
    def setup(self):
        print('Setup done.')
    def test(self):
        print('Test finished.')
    def cleanup(self):
        print('Cleanup done.')
When a task runs, the setup() method runs first. if the setup finished with an exception, the task is aborted without running test() or cleanup(). If the setup() method completes without exception, the test() method runs next. Regardless of any exceptions, the cleanup() method runs last.
Your task may be much more involved of course, utilizing the resources and APIs
provided in the Task class for Graphical User Interface (GUI) inputs and outputs.
As long as your tasks make proper use of the GUI resources available from the Task class,
your task will run successfully in the srsgui application.
A task is executed as a Python thread (if it is run by an application with a Qt backend, it will be QThread.), running concurrently with the main application.
The Task is designed with much consideration
on protection of the main application from crashing caused by unhandled Exceptions.
srsgui provides debug information and error handling just as Python interpreter does.
If you have modified a task, reopen the .taskconfig file from within srsgui in order to reload
the modified task before you run it again (otherwise your modified code will not be run).
The main application provides resources that a task can use, and responds to the callbacks from the task. The resources are set using the APIs provided by the task.
set_inst_dict– set the instrument dictionary that contains the instrument instances.
set_data_dict– set the data dictionary that contains the data instances.
set_figure_dict– set the figure dictionary that contains the figure instances.
set_session_handler– set the session handler that saves the data to a file.
set_callback_handler– set the callback handler that handles the callbacks from the task.
The main application and the running task are separate threads, and the main application responds only to
the callbacks from the task. Following are convenience methods for callbacks and related ones
in the Task class.
- For text output,
- write_textis the base method for Task to use- callbacks.text_availablecallback.
 
- For python logging,
- get_loggeris to get the logger instance for the task.
- logger.debugis to use the logger instance to log debug messages.
- logger.infois to use the logger instance to log info messages.
- logger.erroris to use the logger instance to log error messages.
- logger.warningis to use the logger instance to log warning messages.
- logger.criticalis to use the logger instance to log critical messages.
 
- For the input panel in the srsguimain window,
- input_parametersis a dictionary that contains the input parameters that will be displayed in the input panel.- get_all_input_parametersis to get all the input parameters that are displayed in the input panel.
- set_input_parameteris to set the value of an input parameter.
- get_input_parameteris to get the value of an input parameter.
- notify_parameter_changedis to notify the main application that the value of an input parameter has changed. The main application will update the value of the input parameter in the input panel.
 
- For Matplotlib figures,
- You can use most of Axes-based Matplotlib APIs with the figure instance you get with - get_figure. After adding data and formats into the figure, call- request_figure_update.
- For a question dialog box during running a task,
- For the session_handler (which saves information from a task to a file),
- For inst_dict
- get_instrumentis to retrieve the Instrument subclass instance named in the .taskconfig file. Once getting the instrument instance, you can use it in the task in the same way with the instance created from a Python interpreter.
 
Once you get used to the API for the Task class,
you can write scripts that run as a part of srsgui.