pybot.minitel.forms

A simple Minitel forms management toolkit.

class pybot.minitel.forms.Form(mt)[source]

Bases: object

Defines the forms in terms of layout and field connection with the application.

A form is made of static prompts and entry fields. The class handles its rendering and the management of user interactions when modifying its content.

The definition of the form can be provided by calls to add_prompt() and add_field(). This can also be done by loading the equivalent JSON data structure, using load_definition().

For convenience, the dump_definition() does the reverse operation, which allows producing the JSON data directly from the current definition of the form.

Warning

The class is intended to be used in Videotex mode. Maybe it works fine in Teleinfo mode too, but it has not been tested. By the way, to ensure all goes well, the Minitel is switched in Videotex mode when rendering forms.

Parameters:mt (Minitel) – the Minitel instance
add_field(name, x, y, size, marker='.')[source]

Adds a field to the form, at a given position and with a given size.

Parameters:
  • name (str) – the field name
  • x (int) – X coordinate of the prompt start position
  • y (int) – Y coordinate of the prompt start position
  • size (int) – the field size
  • marker (char) – the character used to mark the fields area (default: ‘.’)
add_prompt(x, y, text)[source]

Adds a fixed text to the form, at a given position.

Parameters:
  • x (int) – X coordinate of the prompt start position
  • y (int) – Y coordinate of the prompt start position
  • text (str) – the prompt text (can include an attributes sequence)
dump_definition()[source]

Returns the form current definition as a JSON formatted structure.

The returned structure is a dictionary, containing the two top-level entries prompts and fields.

The value of the prompts entry is a list of tuples, composed of :

  • the X position
  • the Y position
  • the prompt text

The value of the fields entry is a sub-dictionary, keyed by the field names, and which values are tuples, composed of :

  • the X position
  • the Y position
  • the size (in characters)
  • the marker character (defaulted to . if not included)

Example:

{
    "prompts": [
        [0, 2, "First name"],
        [0, 4, "Last name"],
        [30, 23, "ENVOI"]
    ],
    "fields": {
        "lname": [15, 4, 20, "."],
        "fname": [15, 2, 20, "."]
    }
}
Returns:JSON form definition
Return type:str
input(content=None, max_wait=None)[source]

Handles user interactions and return the fields content if the form is submitted.

The cursor is made visible on start, and hidden back when exiting.

Special keys are interpreted as follows :

ENVOI (SEND)
submits the form, returning the fields content as a dictionary
SOMMAIRE (CONTENT)
cancels and returns None
RETOUR (BACK)
jumps to the previous field (or to the last one if current on the first one)
SUITE (NEXT)
jumps to the next field (or to the first one if current on the last one)
CORRECTION
backspaces one character in the field
ANNULATION (CANCEL)
clears the field
Parameters:
  • content (dict) – optional dictionary containing the initial field values
  • max_wait (int) – maximum wait time in seconds for filling and validating the form (if None, waits indefinitely)
Returns:

the fields content if the form has been submitted, None otherwise.

Return type:

dict

load_definition(data)[source]

Loads the form definition from the provided JSON structure.

Refer to dump_definition() documentation for the structure specifications.

Parameters:data (str) – the form definition in JSON format
Raises:ValueError – if no data or invalid JSON data provided
prepare()[source]

Prepares the form by sorting the prompts and fields according to their position.

Is automatically invoked by render().

render(content=None)[source]

Renders the form on the screen.

Should normally be invoked before calling input().

Warning

The Minitel is switched in Videotex mode before processing.

Parameters:content (dict) – optional dictionary containing the initial field values
render_and_input(content=None)[source]

A shortcut for the render / input sequence.

Refer to render() and input() for documentation of the parameters and return value.