1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
# `plover.engine` -- Steno engine
```{py:module} plover.engine
```
The steno engine is the core of Plover; it handles communication between the
machine and the translation and formatting subsystems, and manages configuration
and dictionaries.
````{class} StenoEngine(config, controller, keyboard_emulation)
```{attribute} config
:type: Dict[str, any]
:noindex:
A dictionary containing configuration options.
```
```{attribute} controller
:type: plover.oslayer.controller.Controller
:noindex:
An instance of {class}`Controller<plover.oslayer.controller.Controller>`
for managing commands sent to this Plover instance. This is provided
during startup.
```
```{attribute} keyboard_emulation
:type: plover.oslayer.keyboardcontrol.KeyboardEmulation
:noindex:
An instance of {class}`KeyboardEmulation<plover.oslayer.keyboardcontrol.KeyboardEmulation>`
provided during startup.
```
```{data} HOOKS
:type: List[str]
A list of all the possible engine hooks. See {ref}`engine-hooks` below for
a list of valid hooks.
```
```{attribute} machine_state
:type: str
The connection state of the current machine. One of `stopped`,
`initializing`, `connected` or `disconnected`.
```
```{attribute} output
:type: bool
`True` if steno output is enabled, `False` otherwise.
```
```{attribute} config
:type: plover.config.Config
A {class}`Config<plover.config.Config>` object containing the engine's
configuration.
```
```{attribute} translator_state
:type: plover.translation._State
A {class}`_State<plover.translation._State>` object containing the
current state of the translator.
```
```{attribute} starting_stroke_state
:type: StartingStrokeState
A {class}`StartingStrokeState` representing the initial state of the
formatter.
```
```{attribute} dictionaries
:type: plover.steno_dictionary.StenoDictionaryCollection
A
{class}`StenoDictionaryCollection<plover.steno_dictionary.StenoDictionaryCollection>`
of all the dictionaries Plover has loaded for the current system.
This includes disabled dictionaries and dictionaries that failed to load.
```
```{method} _in_engine_thread() -> bool
Returns whether we are currently in the same thread that the engine
is running on. This is useful because event listeners for machines and
others are run on separate threads, and we want to be able to run
engine events on the same thread as the main engine.
```
```{method} start()
Starts the steno engine.
```
```{method} quit([code=0])
Quits the steno engine, ensuring that all pending tasks are completed
before exiting.
```
```{method} restart()
Quits and restarts the steno engine, ensuring that all pending tasks
are completed.
```
```{method} run()
Starts the steno engine, translating any strokes that are input.
```
```{method} join()
Joins any sub-threads if necessary and returns an exit code.
```
```{method} load_config() -> bool
Loads the Plover configuration file and returns `True` if it was
loaded successfully, `False` if not.
```
```{method} reset_machine()
Resets the machine state and Plover's connection with the machine, if
necessary, and loads all the configuration and dictionaries.
```
```{method} send_engine_command(command: str)
Runs the specified Plover command, which can be either a built-in
command like `set_config` or one from an external plugin.
`command` is a string containing the command and its argument (if any),
separated by a colon. For example, `lookup` sends the
`lookup` command (the same as stroking `{PLOVER:LOOKUP}`), and
`run_shell:foo` sends the `run_shell` command with the argument
`foo`.
```
```{method} toggle_output()
Toggles steno mode. See {attr}`output` to get the current state, or
{meth}`set_output` to set the state to a specific value.
```
```{method} set_output(enabled: bool)
Enables or disables steno mode. Set `enabled` to `True` to enable
steno mode, or `False` to disable it.
```
```{method} __getitem__(setting: str) -> any
Returns the value of the configuration property `setting`.
```
```{method} __setitem__(setting: str, value: any)
Sets the configuration property `setting` to `value`.
```
```{method} get_suggestions(translation: str) -> List[plover.suggestions.Suggestion]
Returns a list of suggestions for the specified `translation`.
```
```{method} clear_translator_state([undo=False])
Resets the translator to an empty state, as if Plover had just started up,
clearing the entire translation stack. If `undo` is `True`, this also reverts
all previous translations on the stack (which could include a lot of backspaces).
```
```{method} hook_connect(hook: str, callback: Function)
Adds `callback` to the list of handlers that are called when the `hook`
hook gets triggered. Raises a `KeyError` if `hook` is not in
{data}`HOOKS`.
The expected signature of the callback is depends on the hook; see
{ref}`engine-hooks` for more information.
```
```{method} hook_disconnect(hook: str, callback: Function)
Removes `callback` from the list of handlers that are called when
the `hook` hook is triggered. Raises a `KeyError` if `hook` is not in
{data}`HOOKS`, and a `ValueError` if `callback` was never added as
a handler in the first place.
```
The following methods simply provide a way to access the underlying
{class}`StenoDictionaryCollection<plover.steno_dictionary.StenoDictionaryCollection>`.
See the documentation there for more complete information.
```{method} lookup(translation: Tuple[str]) -> str
Returns the first translation for the steno outline `translation` using
all the filters.
```
```{method} raw_lookup(translation: Tuple[str]) -> str
Like {meth}`lookup`, but without any of the filters.
```
```{method} lookup_from_all(translation: Tuple[str]) -> List[str]
Returns all translations for the steno outline `translation` using
all the filters.
```
```{method} raw_lookup_from_all(translation: Tuple[str]) -> List[str]
Like {meth}`lookup_from_all`, but without any of the filters.
```
```{method} reverse_lookup(translation: str) -> List[Tuple[str]]
Returns the list of steno outlines that translate to `translation`.
```
```{method} casereverse_lookup(translation: str) -> List[Tuple[str]]
Like {meth}`reverse_lookup`, but performs a case-insensitive lookup.
```
```{method} add_dictionary_filter(dictionary_filter: Function[(Tuple[str], str), bool])
Adds `dictionary_filter` to the list of dictionary filters.
See {attr}`StenoDictionaryCollection.filters<plover.steno_dictionary.StenoDictionaryCollection.filters>`
for more information.
```
```{method} remove_dictionary_filter(dictionary_filter: Function[(Tuple[str], str), bool])
Removes `dictionary_filter` from the list of dictionary filters.
```
```{method} add_translation(strokes: Tuple[str], translation: str[, dictionary_path: str = None])
Adds a steno entry mapping the steno outline `strokes` to
`translation` in the dictionary at `dictionary_path`, if specified,
or the first writable dictionary.
```
````
````{class} StartingStrokeState(attach, capitalize)
An object representing the starting state of the formatter before any
strokes are input.
```{attribute} attach
:type: bool
Whether to delete the space before the translation when the initial
stroke is translated.
```
```{attribute} capitalize
:type: bool
Whether to capitalize the translation when the initial stroke is
translated.
```
````
````{class} MachineParams(type, options, keymap)
An object representing the current state of the machine.
```{attribute} type
:type: str
The name of the machine. This is the same as the name of the plugin
that provides the machine's functionality. `Keyboard` by default.
```
```{attribute} options
:type: Dict[str, any]
A dictionary of machine specific options. See {mod}`plover.config`
for more information.
```
```{attribute} keymap
:type: plover.machine.keymap.Keymap
A {class}`Keymap<plover.machine.keymap.Keymap>` mapping the current
system to this machine.
```
````
````{class} ErroredDictionary(path, exception)
A placeholder class for a dictionary that failed to load. This is a subclass
of {class}`StenoDictionary<plover.steno_dictionary.StenoDictionary>`.
```{attribute} path
:type: str
The path to the dictionary file.
```
```{attribute} exception
:type: any
The exception that caused the dictionary loading to fail.
```
````
(engine-hooks)=
## Engine Hooks
Plover uses engine hooks to allow plugins to listen to engine events. By
calling {meth}`engine.hook_connect<StenoEngine.hook_connect>` and passing the
name of one of the hooks below and a function, you can write handlers that are
called when Plover hooks get triggered.
```{plover:hook} stroked(stroke: plover.steno.Stroke)
The user just sent a stroke.
```
```{plover:hook} translated(old, new)
% TODO
```
```{plover:hook} machine_state_changed(machine_type: str, machine_state: str)
Either the machine type was changed by the user, or the connection state
of the machine changed. `machine_type` is the name of the machine
(e.g. `Gemini PR`), and `machine_state` is one of `stopped`,
`initializing`, `connected` or `disconnected`.
```
```{plover:hook} output_changed(enabled: bool)
The user requested to either enable or disable steno output. `enabled` is
`True` if output is enabled, `False` otherwise.
```
```{plover:hook} config_changed(config: Dict[str, any])
The configuration was changed, or it was loaded for the first time.
`config` is a dictionary containing *only* the changed fields. Call the
hook function with the
{attr}`StenoEngine.config<plover.engine.StenoEngine.config>`
to initialize your plugin based on the full configuration.
```
```{plover:hook} dictionaries_loaded(dictionaries: plover.steno_dictionary.StenoDictionaryCollection)
The dictionaries were loaded, either when Plover starts up or the system
is changed or when the engine is reset.
```
```{plover:hook} send_string(s: str)
Plover just sent the string `s` over keyboard output.
```
```{plover:hook} send_backspaces(b: int)
Plover just sent backspaces over keyboard output. `b` is the number of
backspaces sent.
```
```{plover:hook} send_key_combination(c: str)
Plover just sent a keyboard combination over keyboard output. `c` is a
string representing the keyboard combination, for example `Alt_L(Tab)`.
```
```{plover:hook} add_translation()
The Add Translation command was activated -- open the Add Translation tool.
```
```{plover:hook} focus()
The Show command was activated -- reopen Plover's main window and bring it
to the front.
```
```{plover:hook} configure()
The Configure command was activated -- open the configuration window.
```
```{plover:hook} lookup()
The Lookup command was activated -- open the Lookup tool.
```
```{plover:hook} suggestions()
The Suggestions command was activated -- open the Suggestions tool.
```
```{plover:hook} quit()
The Quit command was activated -- wrap up any pending tasks and quit Plover.
```
|