command_line_ui

A simple command-line user interface for controlling the pump.

class turboctl.ui.command_line_ui.CommandLineUI(port=None, auto_update=True, inputfile=None, outputfile=None)

A simple command-line UI.

Instances of this class should be closed after they are no longer needed by calling control_interface.close() or using a with block. Otherwise the parallel thread created by control_interface may continue to run in the background and consume resources.

This class contains several methods with names following the pattern cmd_<command>. These methods correspond to the commands a user can give to the UI: when the command string 'example' is entered, the cmd_example() method is called. Because messages printed by the help command use the docstrings of these methods in an unaltered form, the docstrings are written in plain text and cannot contain any reStructuredText syntax that isn’t easily readable by humans.

inputfile

A file-like object for receiving input.

outputfile

A file-like object for writing output.

debug

Setting this to True activates the debug mode; the default value is False. See cmd_debug() for details.

Type:

bool

verbose

Setting this to True increases the verbosity of the output of commands; the default value is False. See cmd_verbose() for details.

Type:

bool

control_interface

An ControlInterface object used for sending commands to the pump.

intro

A string displayed to the user upon starting the UI. The default value is f"Welcome to TurboCtl v. {__version__}! Type 'help' for a list of commands.".

prompt

A string displayed to the user every time the UI requests input. The default value is '>> '.

indent

Class attribute. A string used to indent text blocks. The default value is '    ' (four spaces).

cmds_and_aliases

Class attribute. A list of tuples, where index 0 of each tuple contains the name of a command accepted by the UI, and index 1 contains a list of aliases for that command. It has the following value:

[
    ('pump'   , []),
    ('status' , ['s']),
    ('reset'  , ['re']),
    ('read'   , ['r']),
    ('write'  , ['w']),
    ('info'   , ['i']),
    ('exit'   , ['e', 'q', 'x']),
    ('help'   , ['h']),
    ('docs'   , ['d']),
    ('debug'  , ['db']),
    ('verbose', ['v']),
]
__init__(port=None, auto_update=True, inputfile=None, outputfile=None)

Initialize a new CommandLineUI.

Parameters:
__enter__()

Called upon entering a with block; returns self.

__exit__(exception_type, exception_value, traceback)

Called upon exiting a with block; calls control_interface.close().

The arguments are ignored.

run()

Run the UI.

This function runs the following algorithm:

  1. Print prompt and wait for user input.

  2. Parse the input string into a command and its arguments by splitting it at whitespace.

  3. Call a method with the name cmd_<the given command>. The method will execute the command and print its output. If no method with that name exists, print an error message and start again from step 1.

  4. If the method was cmd_exit(), break the loop. Otherwise, start another iteration from step 1.

cmd_pump(value)

Turn the pump on or off.

Values of ‘1’, ‘True’ and ‘on’ turn the pump on; ‘0’, ‘False’ and ‘off’ turn it off.

cmd_status()

Get the status of the pump.

cmd_reset()

Reset the error status of the pump.

cmd_read(number, index=0)

Return the value of parameter number, index index.

cmd_write(number, value, index=0)

Write value to parameter number, index index.

cmd_info(letter, numbers)

Display information about parameters, errors, or warnings.

letter should be ‘p’, ‘e’, or ‘w’ depending on whether numbers refer to parameters, errors, or warnings.

numbers should be a list or a tuple of the numbers of those parameters/errors/warnings that should be displayed. It can also be a single number or ‘all’, if only a single parameter/error/warning or all of them should be listed.

cmd_help(value=None)

Display a help message.

value should be the name or an alias of the command that should be described. If no value is specified, all commands are listed and described.

cmd_docs()

Open TurboCtl documentation in a browser.

cmd_exit()

Exit the UI.

cmd_debug(value)

Activate or deactivate the debug mode.

Values of ‘1’, ‘True’ and ‘on’ activate the debug mode; ‘0’, ‘False’ and ‘off’ deactivate it.

In normal operation, TypeErrors and ValueErrors raised during the execution of commands are caught to prevent users from crashing the program with invalid commands. Activating the debug mode disables this error-catching in order to make debugging easier.

cmd_verbose(value)

Activate or deactivate the verbose mode.

Values of ‘1’, ‘True’ and ‘on’ activate the verbose mode; ‘0’, ‘False’ and ‘off’ deactivate it.

When the verbose mode is on, commands that send telegrams to the pump will print all the contents of the telegram and the reply to the screen.

input(prompt='')

Ask the user for input.

This method works like the built-in input() function, but uses inputfile and outputfile instead of sys.stdin and sys.stdout.

The line editing functionality provided by the readline module works with this method only if inputfile is sys.stdin.

print(*objects, sep=' ', end='\n')

Print text to the UI.

This method works like the built-in print() function, but uses inputfile and outputfile instead of sys.stdin and sys.stdout, and doesn’t include the file and flush arguments.