From 0562120ea261741b4e6eb735b9b9bc8c5f6af862 Mon Sep 17 00:00:00 2001 From: kaliko Date: Sun, 10 Mar 2024 17:32:45 +0100 Subject: [PATCH] Move protocol logic in the client --- mpdaio/client.py | 21 ++++++++++++++++----- mpdaio/connection.py | 17 ++--------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/mpdaio/client.py b/mpdaio/client.py index 00dbfed..aaffc0a 100644 --- a/mpdaio/client.py +++ b/mpdaio/client.py @@ -11,7 +11,7 @@ from .exceptions import MPDConnectionError, MPDProtocolError, MPDCommandError from .utils import Range, escape from .const import CONNECTION_MAX, CONNECTION_TIMEOUT -from .const import ERROR_PREFIX, SUCCESS, NEXT +from .const import HELLO_PREFIX, ERROR_PREFIX, SUCCESS, NEXT log = logging.getLogger(__name__) @@ -295,17 +295,28 @@ class CmdHandler: return retval async def _init_connection(self): - """Init connection if needed""" + """Init connection if needed + + * Consumes the hello line and sets the protocol version + * Send password command if a password is provided + """ if not self.connection.version: - # TODO: move hello here instead of connection? - # Need to consume hello - pass + await self._hello() if self.password and not self.connection.auth: # Need to send password await self._write_command('password', [self.password]) await self._fetch_nothing() self.connection.auth = True + async def _hello(self) -> None: + """Consume HELLO_PREFIX""" + data = await self.connection.readuntil(b'\n') + rcv = data.decode('utf-8') + if not rcv.startswith(HELLO_PREFIX): + raise MPDProtocolError(f'Got invalid MPD hello: "{rcv}"') + log.debug('consumed hello prefix: %r', rcv) + self.connection.version = rcv.split('\n')[0][len(HELLO_PREFIX):] + async def _write_line(self, line): self.connection.write(f"{line!s}\n".encode()) await self.connection.drain() diff --git a/mpdaio/connection.py b/mpdaio/connection.py index d00f85a..f200bad 100644 --- a/mpdaio/connection.py +++ b/mpdaio/connection.py @@ -12,9 +12,6 @@ from collections import OrderedDict from types import TracebackType from typing import Any, List, Optional, Tuple, Type -from .const import HELLO_PREFIX -from .exceptions import MPDProtocolError - try: # Python 3.7 base = contextlib.AbstractAsyncContextManager except AttributeError as err: @@ -75,7 +72,6 @@ class ConnectionPool(base): ) #log.debug('Connected to %s:%s', host[0], host[1]) connection = Connection(self, host, reader, writer) - await connection._hello() connections.append(connection) connection.in_use = True @@ -140,7 +136,8 @@ class Connection(base): self._reader = reader self._writer = writer self._closed = False - self.auth = False + #: password command with the secret was sent + self.auth: bool = False self.in_use = False self.version: str | None = None @@ -169,16 +166,6 @@ class Connection(base): except AttributeError: # wait_closed is new in 3.7 pass - async def _hello(self) -> None: - """Consume HELLO_PREFIX""" - self.in_use = True - data = await self.readuntil(b'\n') - rcv = data.decode('utf-8') - if not rcv.startswith(HELLO_PREFIX): - raise MPDProtocolError(f'Got invalid MPD hello: "{rcv}"') - log.debug('consumed hello prefix: %r', rcv) - self.version = rcv.split('\n')[0][len(HELLO_PREFIX):] - def __getattr__(self, name: str) -> Any: """All unknown attributes are delegated to the reader and writer""" if self._closed or not self.in_use: -- 2.39.5