1. Luke Plant
  2. playerdo

Commits

Luke Plant  committed cddceb9

Added command 'is_playing'

This required updating shell-fm to require version 0.8, and so the shell-fm
backend to use facilities available in 0.8.

  • Participants
  • Parent commits f4b4f54
  • Branches default

Comments (0)

Files changed (13)

File CHANGES.rst

View file
 Change log
 ==========
 
+Version 0.8
+-----------
+
+* new command 'is_playing'.
+* shell-fm support now requires version 0.8
+
 Version 0.7
 -----------
 

File README.rst

View file
 * moc
 * Quodlibet
 * Rhythmbox
-* shell-fm
+* shell-fm (0.8 and later)
 * VLC (2.0 and later)
 
 Many other players will be supported due to support for the MPRIS DBUS protocol,

File player_do

View file
 # determined.
 #
 
-from playerdo.main import do_test, do_command, find_players
+from playerdo.main import do_test, do_command, find_players, is_playing
 from playerdo.backends.base import Player
 from playerdo import install
 import sys
                      mk_command(c)))
 
 commands.extend([
+    ("is_playing", "Exits status 0 if a player is playing, otherwise 1", is_playing),
     ("test", "Tests that all dependencies are available.", do_test),
     ("help", "Prints help.", print_usage),
     ("install_gnome", "Install keybindings for GNOME2 and launch keybinding editor",

File playerdo/backends/banshee.py

View file
         return process_stdout(["banshee", "--query-current-state"]).strip() \
             ==  "current-state: paused"
 
+    def is_playing(self):
+        return process_stdout(["banshee", "--query-current-state"]).strip() \
+            ==  "current-state: playing"
+
     def play(self):
         process_retval(["banshee", "--play"])
 

File playerdo/backends/base.py

View file
         """
         raise NotImplementedError
 
+    def is_playing(self):
+        """
+        Returns True if the player is playing a song, False otherwise.
+        """
+
     def __lt__(self, other):
         # This is needed to stop errors on Python 3
         return self.sort_order < other.sort_order

File playerdo/backends/cmus.py

View file
     def is_paused(self):
         return 'status paused' in self.send_socket_command('status', receive=10000)
 
+    def is_playing(self):
+        return 'status playing' in self.send_socket_command('status', receive=10000)
+
     def play(self):
         self.send_socket_command('player-play')
 

File playerdo/backends/moc.py

View file
 # Convoluted expressions fpr Python 2/3 compat
 STATE_STOP = "State: STOP".encode('ascii')
 STATE_PAUSE = "State: PAUSE".encode('ascii')
+STATE_PLAY = "State: PLAY".encode('ascii')
 
 
 class Moc(Player):
         info = process_stdout(["mocp", "-i"])
         return STATE_PAUSE in info
 
+    def is_playing(self):
+        info = process_stdout(["mocp", "-i"])
+        return STATE_PLAY in info
+
     def play(self):
         process_retval(["mocp", "--play"])
 

File playerdo/backends/mpris.py

View file
             # Assume stopped if doesn't support GetStatus
             return True
 
+    def is_playing(self):
+        import dbus
+        try:
+            return self.player.GetStatus()[0] == 0
+        except dbus.exceptions.DBusException:
+            # Assume stopped, not playing, if doesn't support GetStatus
+            return False
+
     def check_dependencies(self):
         retval = []
         try:

File playerdo/backends/mpris2.py

View file
     def is_stopped(self):
         return self._playback_status() == "Stopped"
 
+    def is_playing(self):
+        return self._playback_status() == "Playing"
+
     def check_dependencies(self):
         retval = []
         try:

File playerdo/backends/quodlibet.py

View file
         # Has no concept of 'stopped'
         return self.is_paused()
 
+    def is_playing(self):
+        return process_stdout(["quodlibet", "--status"]).decode('ascii').strip().split(' ')[0:1] == ["playing"]
+
     def play(self):
         process_retval(["quodlibet", "--play"])
 

File playerdo/backends/rhythmbox.py

View file
         return False
 
     # is_paused - no way to know
+    # is_playing - no way to know
 
     def play(self):
         process_retval(["rhythmbox-client", "--play"])

File playerdo/backends/shellfm.py

View file
         conf = open(rc_path).read()
         return re.search(r'^\s*unix\s*=\s*([^#\s]+)', conf, re.MULTILINE).groups()[0]
 
+    def _get_status(self):
+        # This only works with shell-fm 0.8 and greater
+        return self.send_socket_command("status", receive=10000).strip()
+
     def is_stopped(self):
-        return not os.path.isfile(os.path.join(os.environ['HOME'],
-                                               ".shell-fm",
-                                               "nowplaying"))
+        return self._get_status() == "STOPPED"
+
+    def is_paused(self):
+        return self._get_status() == "PAUSED"
+
+    def is_playing(self):
+        return self._get_status() == "PLAYING"
 
     def check_dependencies(self):
         retval = []
             raise PlayerException("Cannot play shell-fm when in a stopped state.")
 
     def pause(self):
-        self.send_socket_command("pause")
+        if not self.is_paused():
+            self.send_socket_command("pause")
 
     def unpause(self):
-        self.pause()
+        if self.is_paused():
+            self.send_socket_command("pause")
 
     def stop(self):
         self.send_socket_command("stop")

File playerdo/main.py

View file
 
         # Use the first one
         try:
-            player.do_command(command)
-            return
+            return player.do_command(command)
         except NotImplementedError:
             sys.stderr.write("Operation '%s' not supported for player '%s'.\n" %
                              (command, player.friendly_name))
     sys.exit(1)
 
 
+def is_playing(players):
+    retval = do_command("is_playing", players)
+    if retval:
+        sys.exit(0)
+    else:
+        sys.exit(1)
+
+
 def find_players():
     return sort_players([v() for v in globals().values()
                          if type(v) is type and issubclass(v, Player)])