From bc30504b4e1713648bae8b8c66543c91621d1be8 Mon Sep 17 00:00:00 2001 From: kaliko Date: Mon, 9 Nov 2015 14:54:04 +0100 Subject: [PATCH] Support commands arguments --- musicpdaio.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/musicpdaio.py b/musicpdaio.py index 1663b27..4339f72 100644 --- a/musicpdaio.py +++ b/musicpdaio.py @@ -27,6 +27,7 @@ import logging from os import environ if 'DEBUG' in environ or 'PYTHONASYNCIODEBUG' in environ: + # environ['PYTHONASYNCIODEBUG'] = '1' logging.basicConfig(level=logging.DEBUG) HELLO_PREFIX = "OK MPD " @@ -48,15 +49,6 @@ class ProtocolError(MPDError): class CommandError(MPDError): pass -class CommandListError(MPDError): - pass - -class PendingCommandError(MPDError): - pass - -class IteratingError(MPDError): - pass - class Response: def __init__(self): @@ -69,8 +61,8 @@ class Response: self.version) class MPDProto(asyncio.Protocol): - def __init__(self, future, payload, pwd): - self.pwd = pwd + def __init__(self, future, payload): + logging.debug('payload: "%s"', payload) self.transport = None self.future = future self.payload = payload @@ -110,6 +102,12 @@ class MPDProto(asyncio.Protocol): return rcv class MPDClient: + """MPD Client + :param string host: Server name or IP, default to 'localhost' + :param integer port: Server port, default to 6600 + :param string passwd: Password, default to ``None`` + + """ def __init__(self, host='localhost', port=6600, passwd=None): self._evloop = asyncio.get_event_loop() @@ -117,11 +115,13 @@ class MPDClient: self.futures = [] self._host = host self._port = port - self._pwd = passwd + #self._pwd = passwd # TODO: authentication yet to implement self._commands = { 'currentsong', 'stats', 'playlistinfo', + 'next', + 'find', } def __getattr__(self, attr): @@ -147,10 +147,12 @@ class MPDClient: proto.future.set_exception(ConnectionError(err)) def _command(self, command, args): - payload = '{} {}'.format(command, ''.join(args)) + payload = command + for arg in args: + payload += ' "{}"'.format(escape(arg)) future = asyncio.Future() # kick off a task to create the connection to MPD - coro = self._connect(MPDProto(future, payload, self._pwd)) + coro = self._connect(MPDProto(future, payload)) asyncio.async(coro) self.futures.append(future) if not self.asio: @@ -171,3 +173,9 @@ class MPDClient: self.futures = [] else: logging.info('No task found in queue, need to set self.asio?') + + +def escape(text): + """Escapting quotes and backslash""" + return text.replace('\\', '\\\\').replace('"', '\\"') + -- 2.39.5