From: kaliko Date: Sun, 26 Apr 2015 15:52:21 +0000 (+0200) Subject: Renaming module and coding style changes X-Git-Url: http://git.kaliko.me/?p=python-musicpdaio.git;a=commitdiff_plain;h=f44e0dd8d4f1198096e40d3e9a5a73e65faa36c4 Renaming module and coding style changes --- diff --git a/musicpdasio.py b/musicpdaio.py similarity index 87% rename from musicpdasio.py rename to musicpdaio.py index 8bd1ba4..1663b27 100644 --- a/musicpdasio.py +++ b/musicpdaio.py @@ -69,7 +69,8 @@ class Response: 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 @@ -94,6 +95,8 @@ class MPDProto(asyncio.Protocol): 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) @@ -108,13 +111,13 @@ class MPDProto(asyncio.Protocol): 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', @@ -137,7 +140,7 @@ class MPDClient: # 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: @@ -147,22 +150,24 @@ class MPDClient: 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?')