[Pkg-mpd-commits] [python-mpd] 37/91: Implement proper argument parsing for twisted client
Simon McVittie
smcv at debian.org
Sat Feb 24 14:55:32 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 9523599cef0d46846bb52eb5030e555123aef870
Author: Robert Niederreiter <office at squarewave.at>
Date: Tue Sep 13 09:38:01 2016 +0200
Implement proper argument parsing for twisted client
---
mpd/base.py | 12 ++++++++----
mpd/tests.py | 15 +++++++++++++++
mpd/twisted.py | 20 ++++++++++++++++++--
3 files changed, 41 insertions(+), 6 deletions(-)
diff --git a/mpd/base.py b/mpd/base.py
index ed87d1a..3f47548 100644
--- a/mpd/base.py
+++ b/mpd/base.py
@@ -411,8 +411,10 @@ class MPDClient(MPDClientBase):
self._wfile = _NotConnected()
def _send(self, command, args, retval):
- warnings.warn("The 'send_%s' is deprecated in favor of asynchronous api" % command, DeprecationWarning)
-
+ warnings.warn(
+ "``send_{}`` is deprecated in favor of "
+ "asynchronous API".format(command),
+ DeprecationWarning)
if self._command_list is not None:
raise CommandListError(
"Cannot use send_{} in a command list".format(command))
@@ -421,8 +423,10 @@ class MPDClient(MPDClientBase):
self._pending.append(command)
def _fetch(self, command, args, retval):
- warnings.warn("The 'fetch_%s' is deprecated in favor of asynchronous api" % command, DeprecationWarning)
-
+ warnings.warn(
+ "``fetch_{}`` is deprecated in favor of "
+ "asynchronous API".format(command),
+ DeprecationWarning)
if self._command_list is not None:
raise CommandListError(
"Cannot use fetch_{} in a command list".format(command))
diff --git a/mpd/tests.py b/mpd/tests.py
index 5116841..9c716a3 100755
--- a/mpd/tests.py
+++ b/mpd/tests.py
@@ -631,6 +631,21 @@ class TestMPDProtocol(unittest.TestCase):
)
self.protocol.transport = MockTransport()
+ def test_create_command(self):
+ self.init_protocol(default_idle=False)
+ self.assertEqual(
+ self.protocol._create_command('play'),
+ b'play')
+ self.assertEqual(
+ self.protocol._create_command('rangeid', args=['1', ()]),
+ b'rangeid "1" ":"')
+ self.assertEqual(
+ self.protocol._create_command('rangeid', args=['1', (1,)]),
+ b'rangeid "1" "1:"')
+ self.assertEqual(
+ self.protocol._create_command('rangeid', args=['1', (1, 2)]),
+ b'rangeid "1" "1:2"')
+
def test_success(self):
self.init_protocol(default_idle=False)
diff --git a/mpd/twisted.py b/mpd/twisted.py
index 4b65c6f..d414ac2 100644
--- a/mpd/twisted.py
+++ b/mpd/twisted.py
@@ -139,9 +139,25 @@ class MPDProtocol(basic.LineReceiver, MPDClientBase):
deferred.addCallback(self._parse_command_list_item)
return deferred
+ def _create_command(self, command, args=[]):
+ # XXX: this function should be generalized in future. There exists
+ # almost identical code in ``MPDClient._write_command``, with the
+ # difference that it's using ``encode_str`` for text arguments.
+ parts = [command]
+ for arg in args:
+ if type(arg) is tuple:
+ if len(arg) == 0:
+ parts.append('":"')
+ elif len(arg) == 1:
+ parts.append('"{}:"'.format(int(arg[0])))
+ else:
+ parts.append('"{}:{}"'.format(int(arg[0]), int(arg[1])))
+ else:
+ parts.append('"{}"'.format(escape(arg)))
+ return ' '.join(parts).encode('utf-8')
+
def _write_command(self, command, args=[]):
- parts = [command] + ['"{}"'.format(escape(arg)) for arg in args]
- self.sendLine(' '.join(parts).encode('utf-8'))
+ self.sendLine(self._create_command(command, args))
def _parse_command_list_item(self, result):
if isinstance(result, types.GeneratorType):
--
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