File: extensions.md

package info (click to toggle)
plover 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,356 kB
  • sloc: python: 21,589; sh: 682; ansic: 25; makefile: 11
file content (55 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download | duplicates (2)
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
# Extensions

Extension plugins are implemented as **classes**. The initializer should take
only a {class}`StenoEngine<plover.engine.StenoEngine>` as a parameter.

```ini
[options.entry_points]
plover.extension =
  example_extension = plover_my_plugin.extension:Extension
```

```python
# plover_my_plugin/extension.py

class Extension:
  def __init__(self, engine):
    # Called once to initialize an instance which lives until Plover exits.
    self.engine = engine

  def start(self):
    # Called to start the extension or when the user enables the extension.
    # It can be used to start a new thread for example.
    pass

  def stop(self):
    # Called when Plover exits or the user disables the extension.
    pass
```

Extensions can interact with the engine through the
{class}`StenoEngine<plover.engine.StenoEngine>` API, and receive engine
events through its {ref}`engine hooks<engine-hooks>` mechanism.

For example, a simple extension plugin that just writes strokes to a file,
using the {js:func}`stroked<stroked>` hook:

```python
class StrokeLogger:
  def __init__(self, engine):
    self.engine = engine
    self.output_file = None

  def start(self):
    self.output_file = open("strokes.txt")

    # self.on_stroked gets called on every stroke
    self.engine.hook_connect("stroked", self.on_stroked)

  def stop(self):
    self.engine.hook_connect("stroked", self.on_stroked)
    self.output_file.close()

  def on_stroked(self, stroke):
    print(stroke, file=self.output_file)
```