Bläddra i källkod

Move plugin base classes to kayleevc.plugins

No reason to have them in a module of their own.  And oh, the new
Handler base class exists, and the PluginBase has been revised as per
issue #18.
Clara Hobbs 6 år sedan
förälder
incheckning
3d5a70861c
2 ändrade filer med 63 tillägg och 57 borttagningar
  1. 63
    0
      kayleevc/plugins/__init__.py
  2. 0
    57
      kayleevc/plugins/pluginbase.py

+ 63
- 0
kayleevc/plugins/__init__.py Visa fil

@@ -0,0 +1,63 @@
1
+# This is part of Kaylee
2
+# -- this code is licensed GPLv3
3
+# Copyright 2015-2017 Clayton G. Hobbs
4
+# Portions Copyright 2013 Jezra
5
+
6
+
7
+class PluginBase():
8
+    """Base class for Kaylee plugins
9
+
10
+    Each Kaylee plugin module must define a subclass of this class, named
11
+    ``Plugin``.
12
+    """
13
+
14
+    def __init__(self, config, name):
15
+        """Initialize the plugin
16
+
17
+        All strings to be included in the corpus should be elements of
18
+        ``corpus_strings`` by the end of __init__'s execution.  Note however
19
+        that the words required for number support are automatically included,
20
+        so they need not be listed explicitly on a per-plugin basis.
21
+        """
22
+        super().__init__()
23
+        self.config = config
24
+        self.name = name
25
+        self.options = config.plugins[name]
26
+        self.corpus_strings = set()
27
+
28
+    def get_handler(self, text):
29
+        """Return a handler for the given text
30
+
31
+        This method returns a Handler object which handles the command when
32
+        called.  If the plugin does not recognize the command at all, this
33
+        method must return None instead.  The method must also return None if
34
+        the command is recognized, but for some reason would not perform any
35
+        action currently, e.g. "pause music" when no music player is running.
36
+        """
37
+        return None
38
+
39
+
40
+class Handler:
41
+    """Base class for Kaylee plugin handlers
42
+
43
+    Plugins should subclass this for their own command handlers.
44
+    """
45
+
46
+    def __init__(self, confidence):
47
+        """Initialize the handler
48
+
49
+        The confidence with which this handler will handle the command must be
50
+        passed as ``confidence``, a floating-point number in (0, 1].  A
51
+        confidence of 1 indicates that the handler is definitely supposed to
52
+        handle the command it was created for.  Values between 0 and 1 may be
53
+        used to indicate that the plugin may be able to handle the command,
54
+        but it was misunderstood by the speech recognition system.
55
+        """
56
+        self.confidence = confidence
57
+
58
+    def __call__(self, tts):
59
+        """Handle the command
60
+
61
+        When called, ``tts`` is a function which takes a string as its only
62
+        parameter and passes it to Kaylee's configured text-to-speech system.
63
+        """

+ 0
- 57
kayleevc/plugins/pluginbase.py Visa fil

@@ -1,57 +0,0 @@
1
-# This is part of Kaylee
2
-# -- this code is licensed GPLv3
3
-# Copyright 2015-2017 Clayton G. Hobbs
4
-# Portions Copyright 2013 Jezra
5
-
6
-from gi.repository import GObject
7
-
8
-
9
-class PluginBase(GObject.Object):
10
-    """Base class for Kaylee plugins
11
-
12
-    Each Kaylee plugin module must define a subclass of this class, named
13
-    ``Plugin``.
14
-    """
15
-
16
-    __gsignals__ = {
17
-        'tts' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
18
-                (GObject.TYPE_STRING,))
19
-    }
20
-
21
-    def __init__(self, config, name):
22
-        """Initialize the plugin
23
-
24
-        All strings to be included in the corpus should be elements of
25
-        ``corpus_strings`` by the end of __init__'s execution.  Note however
26
-        that the words required for number support are automatically included,
27
-        so they need not be listed explicitly on a per-plugin basis.
28
-        """
29
-        super().__init__()
30
-        self.config = config
31
-        self.name = name
32
-        self.options = config.plugins[name]
33
-        self.corpus_strings = set()
34
-
35
-    def confidence(self, text):
36
-        """Return the confidence (0-1) with which this plugin can handle text
37
-
38
-        This method must return 1 if the command is definitely supposed to be
39
-        handled by this plugin (text is exactly a sentence configured to
40
-        perform some action) and must return 0 if the command is not recognized
41
-        at all.  The method must also return 0 if the command is recognized,
42
-        but for some reason would not perform any action currently, e.g. "pause
43
-        music" when no music player is running.  Intermediate values may be
44
-        used to indicate that text may be a command this plugin should handle,
45
-        but was misunderstood by the speech recognition system.
46
-        """
47
-        return 0
48
-
49
-    def handle(self, text):
50
-        """Process a recognized voice command
51
-
52
-        This method must return True if the command was handled by the plugin,
53
-        and False otherwise.  If the plugin wants to speak some words to the
54
-        user, it can do so by emitting a ``tts`` signal with the words to be
55
-        spoken as its parameter.
56
-        """
57
-        return False

Laddar…
Avbryt
Spara