self.version)
class MPDProto(asyncio.Protocol):
- def __init__(self, future, payload, cred):
+ def __init__(self, future, payload, pwd):
+ self.pwd = pwd
self.transport = None
self.future = future
self.payload = payload
self.sess.resp += rcv
if rcv.endswith(SUCCESS+'\n'):
+ # Strip final SUCCESS
+ self.sess.resp = self.sess.resp[:len(SUCCESS+'\n')*-1]
logging.debug('set future result')
self.transport.close()
self.future.set_result(self.sess)
class MPDClient:
- def __init__(self, host='localhost', port=6600, cred=None):
- self.eloop = asyncio.get_event_loop()
+ def __init__(self, host='localhost', port=6600, passwd=None):
+ self._evloop = asyncio.get_event_loop()
self.asio = False
self.futures = []
self._host = host
self._port = port
- self._cred = cred
+ self._pwd = passwd
self._commands = {
'currentsong',
'stats',
# coroutine allowing Exception handling
# src: http://comments.gmane.org/gmane.comp.python.tulip/1401
try:
- yield from self.eloop.create_connection(lambda: proto,
+ yield from self._evloop.create_connection(lambda: proto,
host=self._host,
port=self._port)
except Exception as err:
payload = '{} {}'.format(command, ''.join(args))
future = asyncio.Future()
# kick off a task to create the connection to MPD
- coro = self._connect(MPDProto(future, payload, self._cred))
+ coro = self._connect(MPDProto(future, payload, self._pwd))
asyncio.async(coro)
self.futures.append(future)
if not self.asio:
# return once completed.
- self.eloop.run_until_complete(future)
+ self._evloop.run_until_complete(future)
return future
# alternative w/ callback
#if not self.asio:
# future.add_done_callback(lambda ftr: MPDClient.loop.stop())
- # self.eloop.run_forever()
+ # self._evloop.run_forever()
#return future
def run(self):
+ """Run event loop gathering tasks from self.futures
+ """
if self.futures:
- self.eloop.run_until_complete(asyncio.gather(*self.futures))
+ self._evloop.run_until_complete(asyncio.gather(*self.futures))
self.futures = []
else:
logging.info('No task found in queue, need to set self.asio?')