Browse Source

Port MPRIS plugin to new API

There's still no support for telling Kaylee that a handler failed, so
errors pass silently now.  The plugin got much more long-winded, but at
least we only have to get the MPRIS proxy object once instead of three
separate times like we did before.
Clara Hobbs 6 years ago
parent
commit
c1118260e1
1 changed files with 79 additions and 40 deletions
  1. 79
    40
      kayleevc/plugins/mpris.py

+ 79
- 40
kayleevc/plugins/mpris.py View File

@@ -19,9 +19,10 @@ this plugin.  Recognition while music is playing may still be mediocre,
19 19
 but without module-echo-cancel it has almost no chance of working.
20 20
 """
21 21
 
22
+from gi.repository import GLib
22 23
 from pydbus import SessionBus
23 24
 
24
-from .pluginbase import PluginBase
25
+from . import PluginBase, Handler
25 26
 
26 27
 
27 28
 class Plugin(PluginBase):
@@ -36,14 +37,14 @@ class Plugin(PluginBase):
36 37
         self._dbus_proxy = self._bus.get('.DBus')
37 38
 
38 39
         self.commands = {
39
-            'next song': self._next,
40
-            'next video': self._next,
41
-            'pause music': self._pause,
42
-            'pause video': self._pause,
43
-            'play music': self._play,
44
-            'play video': self._play,
45
-            'previous song': self._previous,
46
-            'previous video': self._previous
40
+            'next song': MPRISNextHandler,
41
+            'next video': MPRISNextHandler,
42
+            'pause music': MPRISPauseHandler,
43
+            'pause video': MPRISPauseHandler,
44
+            'play music': MPRISPlayHandler,
45
+            'play video': MPRISPlayHandler,
46
+            'previous song': MPRISPreviousHandler,
47
+            'previous video': MPRISPreviousHandler
47 48
         }
48 49
 
49 50
         self.corpus_strings.update(self.commands)
@@ -59,39 +60,77 @@ class Plugin(PluginBase):
59 60
         except IndexError:
60 61
             return None
61 62
 
62
-    def _next(self):
63
+    def get_handler(self, text):
64
+        """Return a handler if a recognized command is heard"""
65
+        if text not in self.corpus_strings:
66
+            return None
63 67
         p = self._mpris_proxy()
64
-        if p is not None:
65
-            p.Next()
68
+        if p is None:
69
+            return None
70
+        # Make a handler for the command we heard
71
+        return self.commands[text](1, p)
66 72
 
67
-    def _pause(self):
68
-        p = self._mpris_proxy()
69
-        if p is not None:
70
-            p.Pause()
71 73
 
72
-    def _play(self):
73
-        p = self._mpris_proxy()
74
-        if p is not None:
75
-            p.Play()
74
+class MPRISHandler(Handler):
75
+    """Base class for MPRIS plugin handlers"""
76 76
 
77
-    def _previous(self):
78
-        p = self._mpris_proxy()
79
-        if p is not None:
80
-            p.Previous()
77
+    def __init__(self, confidence, proxy):
78
+        """Store the MPRIS proxy object"""
79
+        super().__init__(confidence)
80
+        self.proxy = proxy
81 81
 
82
-    def confidence(self, text):
83
-        """Return whether or not the command can be handled"""
84
-        if text not in self.corpus_strings:
85
-            return 0
86
-        if self._mpris_proxy() is None:
87
-            return 0
88
-        return 1
89
-
90
-    def handle(self, text):
91
-        """Print and speak the phrase when it is heard"""
92
-        # Is there a matching command?
93
-        if self.confidence(text):
94
-            self.commands[text]()
95
-            return True
96
-        else:
97
-            return False
82
+
83
+class MPRISNextHandler(MPRISHandler):
84
+    """Handler for the MPRIS Next method"""
85
+
86
+    def __call__(self, tts):
87
+        """Call the MPRIS Next method"""
88
+        try:
89
+            self.proxy.Next()
90
+        except GLib.Error:
91
+            # This error is raised if the media player was closed after we got
92
+            # the proxy but before we called next.
93
+            # FIXME raise a different exception to tell Kaylee that we failed
94
+            pass
95
+
96
+
97
+class MPRISPauseHandler(MPRISHandler):
98
+    """Handler for the MPRIS Pause method"""
99
+
100
+    def __call__(self, tts):
101
+        """Call the MPRIS Pause method"""
102
+        try:
103
+            self.proxy.Pause()
104
+        except GLib.Error:
105
+            # This error is raised if the media player was closed after we got
106
+            # the proxy but before we called pause.
107
+            # FIXME raise a different exception to tell Kaylee that we failed
108
+            pass
109
+
110
+
111
+class MPRISPlayHandler(MPRISHandler):
112
+    """Handler for the MPRIS Play method"""
113
+
114
+    def __call__(self, tts):
115
+        """Call the MPRIS Play method"""
116
+        try:
117
+            self.proxy.Play()
118
+        except GLib.Error:
119
+            # This error is raised if the media player was closed after we got
120
+            # the proxy but before we called play.
121
+            # FIXME raise a different exception to tell Kaylee that we failed
122
+            pass
123
+
124
+
125
+class MPRISPreviousHandler(MPRISHandler):
126
+    """Handler for the MPRIS Previous method"""
127
+
128
+    def __call__(self, tts):
129
+        """Call the MPRIS Previous method"""
130
+        try:
131
+            self.proxy.Previous()
132
+        except GLib.Error:
133
+            # This error is raised if the media player was closed after we got
134
+            # the proxy but before we called previous.
135
+            # FIXME raise a different exception to tell Kaylee that we failed
136
+            pass

Loading…
Cancel
Save