Commits

Luke Plant  committed 0f1755d

Added support for cmus

  • Participants
  • Parent commits 13ac193

Comments (0)

Files changed (6)

 Change log
 ==========
 
+Version 0.6
+-----------
+
+* Added support for cmus
+
 Version 0.5.2
 -------------
 
 * Amarok
 * Banshee
 * Clementine
+* cmus
 * Exaile
 * Guayadeque
 * moc

File playerdo/backends/__init__.py

+from playerdo.backends.cmus import *
 from playerdo.backends.moc import *
 from playerdo.backends.rhythmbox import *
 from playerdo.backends.shellfm import *

File playerdo/backends/cmus.py

+import os
+import os.path
+
+from playerdo.backends.base import Player
+from playerdo.backends.socket import SocketPlayerMixin
+from playerdo.utils import process_retval, PlayerException
+
+class Cmus(SocketPlayerMixin, Player):
+
+    process_name = "cmus"
+    friendly_name = "cmus"
+
+    def socket_path(self):
+        return os.path.join(os.environ['HOME'], '.cmus', 'socket')
+
+    def is_stopped(self):
+        return 'status stopped' in self.send_socket_command('status', receive=10000)
+
+    def is_paused(self):
+        return 'status paused' in self.send_socket_command('status', receive=10000)
+
+    def play(self):
+        self.send_socket_command('player-play')
+
+    def pause(self):
+        self.send_socket_command("player-pause")
+
+    def unpause(self):
+        self.pause()
+
+    def stop(self):
+        self.send_socket_command("player-stop")
+
+    def next(self):
+        self.send_socket_command("player-next")
+
+    def prev(self):
+        self.send_socket_command("player-prev")

File playerdo/backends/shellfm.py

 import os.path
 import re
-import socket
 
 from playerdo.backends.base import Player
+from playerdo.backends.socket import SocketPlayerMixin
 from playerdo.utils import process_retval, PlayerException
 
-class ShellFm(Player):
+class ShellFm(SocketPlayerMixin, Player):
 
     process_name = "shell-fm"
     friendly_name = "shell-fm"
 
-    def _socket_path(self):
+    def socket_path(self):
         rc_path = os.path.join(os.environ['HOME'], '.shell-fm', 'shell-fm.rc')
         conf = open(rc_path).read()
         return re.search(r'^\s*unix\s*=\s*([^#\s]+)', conf, re.MULTILINE).groups()[0]
 
-    def _send_command(self, command):
-        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-        s.connect(self._socket_path())
-        s.send((command + "\n").encode("ascii"))
-        s.close()
-
     def is_stopped(self):
         return not os.path.isfile(os.path.join(os.environ['HOME'],
                                                ".shell-fm",
             raise PlayerException("Cannot play shell-fm when in a stopped state.")
 
     def pause(self):
-        self._send_command("pause")
+        self.send_socket_command("pause")
 
     def unpause(self):
         self.pause()
 
     def stop(self):
-        self._send_command("stop")
+        self.send_socket_command("stop")
 
     def next(self):
-        self._send_command("skip")
+        self.send_socket_command("skip")

File playerdo/backends/socket.py

+from __future__ import absolute_import
+
+import socket
+
+class SocketPlayerMixin(object):
+
+    def send_socket_command(self, command, receive=0):
+        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        s.connect(self.socket_path())
+        s.send((command + "\n").encode("ascii"))
+        if receive > 0:
+            retval = s.recv(receive)
+        else:
+            retval = None
+        s.close()
+        return retval
+
+    def socket_path(self):
+        raise NotImplentedError()