Fix #2381: mpdupdate on Python 3

Communicate bytes over the socket, obvi.
This commit is contained in:
Adrian Sampson 2017-01-10 12:22:30 -05:00
parent a2d37dd588
commit 2706339a83
2 changed files with 14 additions and 12 deletions

View file

@ -35,14 +35,14 @@ import six
# easier.
class BufferedSocket(object):
"""Socket abstraction that allows reading by line."""
def __init__(self, host, port, sep='\n'):
def __init__(self, host, port, sep=b'\n'):
if host[0] in ['/', '~']:
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(os.path.expanduser(host))
else:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
self.buf = ''
self.buf = b''
self.sep = sep
def readline(self):
@ -51,11 +51,11 @@ class BufferedSocket(object):
if not data:
break
self.buf += data
if '\n' in self.buf:
if self.sep in self.buf:
res, self.buf = self.buf.split(self.sep, 1)
return res + self.sep
else:
return ''
return b''
def send(self, data):
self.sock.send(data)
@ -106,24 +106,24 @@ class MPDUpdatePlugin(BeetsPlugin):
return
resp = s.readline()
if 'OK MPD' not in resp:
if b'OK MPD' not in resp:
self._log.warning(u'MPD connection failed: {0!r}', resp)
return
if password:
s.send('password "%s"\n' % password)
s.send(b'password "%s"\n' % password.encode('utf8'))
resp = s.readline()
if 'OK' not in resp:
if b'OK' not in resp:
self._log.warning(u'Authentication failed: {0!r}', resp)
s.send('close\n')
s.send(b'close\n')
s.close()
return
s.send('update\n')
s.send(b'update\n')
resp = s.readline()
if 'updating_db' not in resp:
if b'updating_db' not in resp:
self._log.warning(u'Update failed: {0!r}', resp)
s.send('close\n')
s.send(b'close\n')
s.close()
self._log.info(u'Database updated.')

View file

@ -4,7 +4,9 @@ Changelog
1.4.4 (in development)
----------------------
Changelog goes here!
Fixes:
* :doc:`/plugins/mpdupdate`: Fix Python 3 compatibility. :bug:`2381`
1.4.3 (January 9, 2017)