Commits

Adrian Sampson committed b963839 Merge

Merge pull request #313 from jhawthorn/mpdupdate_unix

mpdupdate: Allow UNIX domain socket for MPD server

  • Participants
  • Parent commits 4cf7657, 367b94c

Comments (0)

Files changed (1)

File beetsplug/mpdupdate.py

 # easier.
 class BufferedSocket(object):
     """Socket abstraction that allows reading by line."""
-    def __init__(self, sep='\n'):
-        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    def __init__(self, host, port, sep='\n'):
+        if host[0] == '/':
+            self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+            self.sock.connect(host)
+        else:
+            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            self.sock.connect((host, port))
         self.buf = ''
         self.sep = sep
 
-    def connect(self, host, port):
-        self.sock.connect((host, port))
-
     def readline(self):
         while self.sep not in self.buf:
             data = self.sock.recv(1024)
     """
     print('Updating MPD database...')
 
-    s = BufferedSocket()
-    s.connect(host, port)
+    s = BufferedSocket(host, port)
     resp = s.readline()
     if 'OK MPD' not in resp:
         print('MPD connection failed:', repr(resp))