[Pkg-mpd-commits] [python-mpd] 17/91: Twisted client works with python 3
Simon McVittie
smcv at debian.org
Sat Feb 24 14:55:28 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 482c9360cbedeae7c695fb8f86edb7bd4f111856
Author: Robert Niederreiter <office at squarewave.at>
Date: Mon Sep 12 09:46:52 2016 +0200
Twisted client works with python 3
---
examples/twisted_example.py | 17 +++++++++--------
mpd/twisted.py | 35 ++++++++++++++++-------------------
2 files changed, 25 insertions(+), 27 deletions(-)
diff --git a/examples/twisted_example.py b/examples/twisted_example.py
index 761d529..ef4a55e 100644
--- a/examples/twisted_example.py
+++ b/examples/twisted_example.py
@@ -12,15 +12,15 @@ class MPDApp(object):
def __call__(self, result):
# idle result callback
- print(result)
+ print('Subsystems: {}'.format(list(result)))
def status_success(result):
# status query success
- print(result)
+ print('Status success: {}'.format(result))
def status_error(result):
# status query failure
- print(result)
+ print('Status error: {}'.format(result))
# query player status
self.protocol.status()\
@@ -32,22 +32,23 @@ class MPDClientFactory(protocol.ClientFactory):
protocol = MPDProtocol
def buildProtocol(self, addr):
- print("Create MPD protocol")
+ 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)
+ print('Connection failed - goodbye!: {}'.format(reason))
reactor.stop()
def clientConnectionLost(self, connector, reason):
- print("Connection lost - goodbye!: %s" % reason)
- reactor.stop()
+ print('Connection lost - goodbye!: {}'.format(reason))
+ if reactor.running:
+ reactor.stop()
if __name__ == '__main__':
factory = MPDClientFactory()
- reactor.connectTCP("localhost", 6600, factory)
+ reactor.connectTCP('localhost', 6600, factory)
reactor.run()
diff --git a/mpd/twisted.py b/mpd/twisted.py
index ec57ef9..3793d81 100644
--- a/mpd/twisted.py
+++ b/mpd/twisted.py
@@ -23,6 +23,7 @@
# https://github.com/Mic92/python-mpd2/issues
from __future__ import absolute_import
+from __future__ import unicode_literals
from mpd.base import CommandError
from mpd.base import CommandListError
from mpd.base import ERROR_PREFIX
@@ -36,7 +37,7 @@ from mpd.base import mpd_command_provider
from mpd.base import mpd_commands
from twisted.internet import defer
from twisted.protocols import basic
-from types import GeneratorType
+import types
def _create_command(wrapper, name, callback):
@@ -49,7 +50,7 @@ def _create_command(wrapper, name, callback):
@mpd_command_provider
class MPDProtocol(basic.LineReceiver, MPDClientBase):
- delimiter = "\n"
+ delimiter = b'\n'
def __init__(self, default_idle=True, idle_result=None):
super(MPDProtocol, self).__init__(use_unicode=True)
@@ -74,7 +75,7 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
return
# create command and hook it on class
func = _create_command(cls._execute, name, callback)
- escaped_name = name.replace(" ", "_")
+ escaped_name = name.replace(' ', '_')
setattr(cls, escaped_name, func)
def lineReceived(self, line):
@@ -92,7 +93,7 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
state_list[0].errback(CommandError(error))
for state in state_list[1:-1]:
state.errback(
- CommandListError("An earlier command failed."))
+ CommandListError('An earlier command failed.'))
state_list[-1].errback(CommandListError(error))
del self._state[0]
del self._command_list_results[0]
@@ -112,10 +113,10 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
def _execute(self, command, args, parser):
# close or kill command in command list not allowed
if self._command_list and not callable(parser):
- msg = "{} not allowed in command list".format(command)
+ msg = '{} not allowed in command list'.format(command)
raise CommandListError(msg)
# default state idle and currently in idle state, trigger noidle
- if self._default_idle and self._idle and command != "idle":
+ if self._default_idle and self._idle and command != 'idle':
self.noidle().addCallback(self._dispatch_noidle_result)
# write command to MPD
self._write_command(command, args)
@@ -134,15 +135,11 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
return deferred
def _write_command(self, command, args=[]):
- parts = [command]
- parts += ['"{}"'.format(escape(arg.encode('utf-8'))
- if isinstance(arg, unicode) else str(arg)) for arg in args]
- cmd = " ".join(parts)
- self.sendLine(cmd)
+ parts = [command] + ['"{}"'.format(escape(arg)) for arg in args]
+ self.sendLine(' '.join(parts).encode('utf-8'))
def _parse_command_list_item(self, result):
- # TODO: find a better way to do this
- if type(result) == GeneratorType:
+ if isinstance(result, types.GeneratorType):
result = list(result)
self._command_list_results[0].append(result)
return result
@@ -177,13 +174,13 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
def idle(self):
if self._idle:
- raise CommandError("Already in idle state")
+ raise CommandError('Already in idle state')
self._idle = True
return self._execute('idle', [], self._parse_list)
def noidle(self):
if not self._idle:
- raise CommandError("Not in idle state")
+ raise CommandError('Not in idle state')
# delete first pending deferred, idle returns nothing when
# noidle gets called
self._state.pop(0)
@@ -192,18 +189,18 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
def command_list_ok_begin(self):
if self._command_list:
- raise CommandListError("Already in command list")
+ raise CommandListError('Already in command list')
if self._default_idle and self._idle:
self.noidle().addCallback(self._dispatch_noidle_result)
- self._write_command("command_list_ok_begin")
+ self._write_command('command_list_ok_begin')
self._command_list = True
self._command_list_results.append([])
self._state.append([])
def command_list_end(self):
if not self._command_list:
- raise CommandListError("Not in command list")
- self._write_command("command_list_end")
+ raise CommandListError('Not in command list')
+ self._write_command('command_list_end')
deferred = defer.Deferred()
deferred.addCallback(self._parse_command_list_end)
self._state[-1].append(deferred)
--
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