transport_data.cli.interactive.Editor

class transport_data.cli.interactive.Editor(*args, **kwargs)[source]

Bases: Application

prompt_toolkit.Application for editing SDMX maintainable artefacts.

The editor has a simple UI with an output pane and a configurable prompts. These are set by View subclasses, which each implement View.accept() to accept and handle user input. Transitions between views are mainly described by FLOW.

__init__(*args, **kwargs) None

Methods

__init__(*args, **kwargs)

accept(buff)

Handle input to the input_field.

cancel_and_wait_for_background_tasks()

Cancel all background tasks, and wait for the cancellation to complete.

cpr_not_supported_callback()

Called when we don't receive the cursor position response in time.

create_background_task(coroutine)

Start a background task (coroutine) for the running application.

exit([result, exception, style])

Exit application.

get_used_style_strings()

Return a list of used style strings.

invalidate()

Thread safe way of sending a repaint trigger to the input event loop.

next([view])

Identify the next View cls; display it and its prompt.

print_text(text[, style])

Print a list of (style_str, text) tuples to the output.

reset()

Reset everything, for reading the next input.

run([pre_run, set_exception_handler, ...])

A blocking 'run' call that waits until the UI is finished.

run_async([pre_run, set_exception_handler, ...])

Run the prompt_toolkit Application until exit() has been called.

run_system_command(command[, ...])

Run system command (While hiding the prompt.

set_prompt(value[, default])

Set the input prompt and default text.

suspend_to_background([suspend_group])

(Not thread safe -- to be called from inside the key bindings.) Suspend process.

Attributes

color_depth

The active ColorDepth.

current_buffer

The currently focused Buffer.

current_search_state

Return the current SearchState.

invalidated

True when a redraw operation has been scheduled.

is_done

is_running

True when the application is currently active/running.

current

quoted_insert

Quoted insert.

vi_state

Vi state.

ttimeoutlen

When to flush the input (For flushing escape keys.) This is important on terminals that use vt100 input.

timeoutlen

Like Vim's timeoutlen option.

render_counter

Render counter.

key_processor

The InputProcessor instance.

accept(buff) bool

Handle input to the input_field.

async cancel_and_wait_for_background_tasks() None

Cancel all background tasks, and wait for the cancellation to complete. If any of the background tasks raised an exception, this will also propagate the exception.

(If we had nurseries like Trio, this would be the __aexit__ of a nursery.)

property color_depth: ColorDepth

The active ColorDepth.

The current value is determined as follows:

  • If a color depth was given explicitly to this application, use that value.

  • Otherwise, fall back to the color depth that is reported by the Output implementation. If the Output class was created using output.defaults.create_output, then this value is coming from the $PROMPT_TOOLKIT_COLOR_DEPTH environment variable.

cpr_not_supported_callback() None

Called when we don’t receive the cursor position response in time.

create_background_task(coroutine: Coroutine[Any, Any, None]) Task[None]

Start a background task (coroutine) for the running application. When the Application terminates, unfinished background tasks will be cancelled.

Given that we still support Python versions before 3.11, we can’t use task groups (and exception groups), because of that, these background tasks are not allowed to raise exceptions. If they do, we’ll call the default exception handler from the event loop.

If at some point, we have Python 3.11 as the minimum supported Python version, then we can use a TaskGroup (with the lifetime of Application.run_async(), and run run the background tasks in there.

This is not threadsafe.

property current_buffer: Buffer

The currently focused Buffer.

(This returns a dummy Buffer when none of the actual buffers has the focus. In this case, it’s really not practical to check for None values or catch exceptions every time.)

property current_search_state: SearchState

Return the current SearchState. (The one for the focused BufferControl.)

exit(result: _AppResult | None = None, exception: BaseException | type[BaseException] | None = None, style: str = '') None

Exit application.

Note

If Application.exit is called before Application.run() is called, then the Application won’t exit (because the Application.future doesn’t correspond to the current run). Use a pre_run hook and an event to synchronize the closing if there’s a chance this can happen.

Parameters:
  • result – Set this result for the application.

  • exception – Set this exception as the result for an application. For a prompt, this is often EOFError or KeyboardInterrupt.

  • style – Apply this style on the whole content when quitting, often this is ‘class:exiting’ for a prompt. (Used when erase_when_done is not set.)

get_used_style_strings() list[str]

Return a list of used style strings. This is helpful for debugging, and for writing a new Style.

invalidate() None

Thread safe way of sending a repaint trigger to the input event loop.

property invalidated: bool

True when a redraw operation has been scheduled.

property is_running: bool

True when the application is currently active/running.

key_processor

The InputProcessor instance.

next(view: type[View] | None = None) None

Identify the next View cls; display it and its prompt.

If view is None (the default), FLOW is used to identify the class.

print_text(text: AnyFormattedText, style: BaseStyle | None = None) None

Print a list of (style_str, text) tuples to the output. (When the UI is running, this method has to be called through run_in_terminal, otherwise it will destroy the UI.)

Parameters:
  • text – List of (style_str, text) tuples.

  • style – Style class to use. Defaults to the active style in the CLI.

quoted_insert

Quoted insert. This flag is set if we go into quoted insert mode.

render_counter

Render counter. This one is increased every time the UI is rendered. It can be used as a key for caching certain information during one rendering.

reset() None

Reset everything, for reading the next input.

run(pre_run: Callable[[], None] | None = None, set_exception_handler: bool = True, handle_sigint: bool = True, in_thread: bool = False, inputhook: Callable[[InputHookContext], None] | None = None) _AppResult

A blocking ‘run’ call that waits until the UI is finished.

This will run the application in a fresh asyncio event loop.

Parameters:
  • pre_run – Optional callable, which is called right after the “reset” of the application.

  • set_exception_handler – When set, in case of an exception, go out of the alternate screen and hide the application, display the exception, and wait for the user to press ENTER.

  • in_thread – When true, run the application in a background thread, and block the current thread until the application terminates. This is useful if we need to be sure the application won’t use the current event loop (asyncio does not support nested event loops). A new event loop will be created in this background thread, and that loop will also be closed when the background thread terminates. When this is used, it’s especially important to make sure that all asyncio background tasks are managed through get_appp().create_background_task(), so that unfinished tasks are properly cancelled before the event loop is closed. This is used for instance in ptpython.

  • handle_sigint – Handle SIGINT signal. Call the key binding for Keys.SIGINT. (This only works in the main thread.)

async run_async(pre_run: Callable[[], None] | None = None, set_exception_handler: bool = True, handle_sigint: bool = True, slow_callback_duration: float = 0.5) _AppResult

Run the prompt_toolkit Application until exit() has been called. Return the value that was passed to exit().

This is the main entry point for a prompt_toolkit Application and usually the only place where the event loop is actually running.

Parameters:
  • pre_run – Optional callable, which is called right after the “reset” of the application.

  • set_exception_handler – When set, in case of an exception, go out of the alternate screen and hide the application, display the exception, and wait for the user to press ENTER.

  • handle_sigint – Handle SIGINT signal if possible. This will call the <sigint> key binding when a SIGINT is received. (This only works in the main thread.)

  • slow_callback_duration – Display warnings if code scheduled in the asyncio event loop takes more time than this. The asyncio default of 0.1 is sometimes not sufficient on a slow system, because exceptionally, the drawing of the app, which happens in the event loop, can take a bit longer from time to time.

async run_system_command(command: str, wait_for_enter: bool = True, display_before_text: AnyFormattedText = '', wait_text: str = 'Press ENTER to continue...') None

Run system command (While hiding the prompt. When finished, all the output will scroll above the prompt.)

Parameters:
  • command – Shell command to be executed.

  • wait_for_enter – FWait for the user to press enter, when the command is finished.

  • display_before_text – If given, text to be displayed before the command executes.

Returns:

A Future object.

set_prompt(value: str, default: str = '') None

Set the input prompt and default text.

suspend_to_background(suspend_group: bool = True) None

(Not thread safe – to be called from inside the key bindings.) Suspend process.

Parameters:

suspend_group – When true, suspend the whole process group. (This is the default, and probably what you want.)

timeoutlen

Like Vim’s timeoutlen option. This can be None or a float. For instance, suppose that we have a key binding AB and a second key binding A. If the uses presses A and then waits, we don’t handle this binding yet (unless it was marked ‘eager’), because we don’t know what will follow. This timeout is the maximum amount of time that we wait until we call the handlers anyway. Pass None to disable this timeout.

ttimeoutlen

When to flush the input (For flushing escape keys.) This is important on terminals that use vt100 input. We can’t distinguish the escape key from for instance the left-arrow key, if we don’t know what follows after “x1b”. This little timer will consider “x1b” to be escape if nothing did follow in this time span. This seems to work like the ttimeoutlen option in Vim.

vi_state

Vi state. (For Vi key bindings.)