Commits

Luke Plant  committed 1543776

Added help text.

  • Participants
  • Parent commits e15729f

Comments (0)

Files changed (4)

 # some music players do not have such a state or it cannot be determined.
 #
 
-import playerdo.main
+from optparse import OptionParser
+from playerdo.main import do_test, do_command, find_players
+from playerdo.backends.base import Player
 import sys
 
-# Execute commands:
 
-def main(command, players):
-    if command == "test":
-        playerdo.main.do_test(players)
-    elif command == "help":
-        playerdo.main.print_help(players)
-    else:
-        playerdo.main.do_command(command, players)
+
+def usage(players):
+    # Print help and list of supported players
+    help = """Usage: player_do <command>
+
+  Media players that are currently running will be detected, and the command will
+  be passed on to the first, giving priority to players that seem to be active.
+
+<command> is one of:
+"""
+    max_len = len(sorted(commands, key=lambda c: len(c[0]))[-1][0])
+    for name, doc, f in commands:
+        help += " " + name +  " "*(max_len - len(name) + 2) + doc + "\n"
+
+    help += """
+Not all operations are supported or fully supported by all players.
+
+Currently supported players are:
+"""
+    for P in players:
+        help += " " + P.process_name + "\n"
+
+    return help
+
+
+def print_usage(players):
+    sys.stdout.write(usage(players))
+
+
+# List of commands: (name, docstring, callable)
+
+commands = []
+
+for c in ["play", "pause", "unpause", "togglepause", "playpause", "stop", "next", "prev", "osd"]:
+    def mk_command(name):
+        def command(players):
+            return do_command(name, players)
+        return command
+    commands.append((c,
+                     getattr(Player, c).__doc__.strip().replace("\n", " "),
+                     mk_command(c)))
+
+commands.extend([
+    ("test", "Tests that all dependencies are available.", do_test),
+    ("help", "Prints help.", print_usage),
+])
+
+
+command_dict = dict((name, f) for name, doc, f in commands)
 
 
 if __name__ == '__main__':
-    import sys
-    main(sys.argv[1], playerdo.main.find_players())
+    players = find_players()
+    if len(sys.argv) < 2:
+        print_usage(players)
+        sys.exit(1)
 
+    name = sys.argv[1]
+    command = command_dict.get(name, None)
+    if command is None:
+        sys.stderr.write("Unrecognised command '%s'.\n\n" % name)
+        print_usage(players)
+        sys.exit(1)
+    else:
+        command(players)
+

File playerdo/backends/base.py

 
     process_name = None
 
+    @classmethod
+    def is_concrete(cls):
+        """
+        Returns True if this class corresponds to a real program
+        """
+        return cls.process_name is not None
+
     def is_running(self):
         """
         Returns true if the player program is running.
 
     # Commands
     def play(self):
+        """
+        Plays current track in media player.
+        """
         raise NotImplementedError
 
     def pause(self):
+        """
+        Pauses current track in media player.
+        """
         raise NotImplementedError
 
     def unpause(self):
+        """
+        Continues playing current track if paused.
+        """
         raise NotImplementedError
 
     def togglepause(self):
         """
-        Play if paused, pause if playing
+        Plays if paused, pauses if playing.
         """
         is_paused = catch_unimplemented(self.is_paused)
 
 
     def playpause(self):
         """
-        Play if stopped/paused, pause if playing
+        Plays if stopped/paused, pauses if playing.
         """
         is_stopped = catch_unimplemented(self.is_stopped)
 
             self.togglepause()
 
     def stop(self):
+        """
+        Stops playing.
+        """
         raise NotImplementedError
 
     def next(self):
+        """
+        Plays next track.
+        """
         raise NotImplementedError
 
     def prev(self):
+        """
+        Plays previous track.
+        """
         raise NotImplementedError
 
     def osd(self):
+        """
+        Shows OSD (on screen display).
+        """
         raise NotImplementedError
 

File playerdo/backends/exaile.py

 
 class Exaile(MprisPlayer):
 
-    name = "Exaile"
+    process_name = "exaile"
     bus_name = "org.mpris.exaile"
 
     def is_stopped(self):

File playerdo/main.py

 from playerdo.backends import *
 from playerdo.backends.base import Player
-
+import sys
 
 def get_running_players(players):
     """
     pass
 
 
-def print_help(players):
-    # Print help and list of supported players
-    pass
-
-
 def do_command(command, players):
     """
     Execute the given command, given a list of Player classes
     try:
         player.do_command(command)
     except NotImplementedError:
-        sys.stderr.write("Operation '%s' not support for player '%s'." % (command, player.__name__))
+        sys.stderr.write("Operation '%s' not supported for player '%s'.\n" % (command, player.process_name))
         sys.exit(1)
 
 
 def find_players():
-    return [v for v in globals().values() if type(v) is type and issubclass(v, Player)]
+    return [v for v in globals().values() if type(v) is type and issubclass(v, Player) and v.is_concrete()]