[Pkg-mpd-commits] [python-mpd] 27/91: Add twisted example

Simon McVittie smcv at debian.org
Sat Feb 24 14:55:30 UTC 2018


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch debian/master
in repository python-mpd.

commit 12e7e00cb43c231e952ea74d396ad4f52b5590ce
Author: Robert Niederreiter <office at squarewave.at>
Date:   Tue Sep 6 10:17:30 2016 +0200

    Add twisted example
---
 examples/twisted_example.py | 53 +++++++++++++++++++++++++++++++++++++++++++++
 mpd/twisted.py              |  6 ++---
 2 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/examples/twisted_example.py b/examples/twisted_example.py
new file mode 100644
index 0000000..761d529
--- /dev/null
+++ b/examples/twisted_example.py
@@ -0,0 +1,53 @@
+from __future__ import print_function
+from mpd import MPDProtocol
+from twisted.internet import protocol
+from twisted.internet import reactor
+
+
+class MPDApp(object):
+    # Example application which deals with MPD
+
+    def __init__(self, protocol):
+        self.protocol = protocol
+
+    def __call__(self, result):
+        # idle result callback
+        print(result)
+
+        def status_success(result):
+            # status query success
+            print(result)
+
+        def status_error(result):
+            # status query failure
+            print(result)
+
+        # query player status
+        self.protocol.status()\
+            .addCallback(status_success)\
+            .addErrback(status_error)
+
+
+class MPDClientFactory(protocol.ClientFactory):
+    protocol = MPDProtocol
+
+    def buildProtocol(self, addr):
+        print("Create MPD protocol")
+        protocol = self.protocol()
+        protocol.factory = self
+        protocol.idle_result = MPDApp(protocol)
+        return protocol
+
+    def clientConnectionFailed(self, connector, reason):
+        print("Connection failed - goodbye!: %s" % reason)
+        reactor.stop()
+
+    def clientConnectionLost(self, connector, reason):
+        print("Connection lost - goodbye!: %s" % reason)
+        reactor.stop()
+
+
+if __name__ == '__main__':
+    factory = MPDClientFactory()
+    reactor.connectTCP("localhost", 6600, factory)
+    reactor.run()
diff --git a/mpd/twisted.py b/mpd/twisted.py
index 49f53a4..a1d688f 100644
--- a/mpd/twisted.py
+++ b/mpd/twisted.py
@@ -55,7 +55,7 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
         super(MPDProtocol, self).__init__(use_unicode=use_unicode)
         # flag whether client should idle by default
         self._default_idle = default_idle
-        self._idle_result = idle_result
+        self.idle_result = idle_result
         self._reset()
 
     def _reset(self):
@@ -160,8 +160,8 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
             self.idle().addCallback(self._dispatch_idle_result)
 
     def _do_dispatch(self, result):
-        if self._idle_result:
-            self._idle_result(result)
+        if self.idle_result:
+            self.idle_result(result)
         else:
             res = list(result)
             msg = 'MPDProtocol: no idle callback defined: {}'.format(res)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git



More information about the Pkg-mpd-commits mailing list