Luke Plant avatar Luke Plant committed 1a6d987

Pulled out re-usable functions into playerdo/main.py

Comments (0)

Files changed (2)

 # some music players do not have such a state or it cannot be determined.
 #
 
+import playerdo.main
 import sys
 
 # Execute commands:
 
 def main(command, players):
     if command == "test":
-        do_test(players)
+        playerdo.main.do_test(players)
     elif command == "help":
-        print_help(players)
+        playerdo.main.print_help(players)
     else:
-        do_command(command, players)
+        playerdo.main.do_command(command, players)
 
 
-def do_test(players):
-    # Check dependencies of all players
-    pass
-
-
-def print_help(players):
-    # Print help and list of supported players
-    pass
-
-def do_command(command, players):
-    # Get running players
-    running_ps = []
-    for P in players:
-        p = P()
-        try:
-            running = p.is_running()
-            if running:
-                running_ps.append(p)
-        except NotImplementedError:
-            pass
-
-    if len(running_ps) == 0:
-        sys.stderr.write("No players running!\n")
-        sys.exit(1)
-
-
-    # Try to find out which one is playing
-    state = []
-    for p in running_ps:
-        try:
-            is_stopped = p.is_stopped()
-        except NotImplementedError:
-            is_stopped = None
-
-        if is_stopped == True:
-            state.append(2)
-        elif is_stopped == False:
-            state.append(0)
-        else:
-            # In-between value for unknowns, because a player that is definitely
-            # stopped is less preferred than one that *might* be playing.
-            state.append(1)
-
-    l = zip(state, running_ps)
-    l.sort()
-
-    # Use the first one
-    player = l[0][1]
-    try:
-        player.do_command(command)
-    except NotImplementedError:
-        sys.stderr.write("Operation '%s' not support for player '%s'." % (command, player.__name__))
-        sys.exit(1)
-
-from playerdo.backends import *
-from playerdo.backends.base import Player
-
-def find_players():
-    return [v for v in globals().values() if type(v) is type and issubclass(v, Player)]
-
 if __name__ == '__main__':
     import sys
-    main(sys.argv[1], find_players())
+    main(sys.argv[1], playerdo.main.find_players())
 
+from playerdo.backends import *
+from playerdo.backends.base import Player
+
+
+def get_running_players(players):
+    """
+    Returns a list of players that are running, sorted by priority
+    they should be used
+    """
+
+    # Get running players
+    running_ps = []
+    for P in players:
+        p = P()
+        try:
+            running = p.is_running()
+            if running:
+                running_ps.append(p)
+        except NotImplementedError:
+            pass
+
+
+    # Try to find out which one is playing
+    state = []
+    for p in running_ps:
+        try:
+            is_stopped = p.is_stopped()
+        except NotImplementedError:
+            is_stopped = None
+
+        if is_stopped == True:
+            state.append(2)
+        elif is_stopped == False:
+            state.append(0)
+        else:
+            # In-between value for unknowns, because a player that is definitely
+            # stopped is less preferred than one that *might* be playing.
+            state.append(1)
+
+    l = zip(state, running_ps)
+    l.sort()
+
+    return [x[1] for x in l]
+
+def do_test(players):
+    # Check dependencies of all players
+    pass
+
+
+def print_help(players):
+    # Print help and list of supported players
+    pass
+
+
+def do_command(command, players):
+    candidates = get_running_players(players)
+    if len(candidates) == 0:
+        sys.stderr.write("No players running!\n")
+        sys.exit(1)
+
+    # Use the first one
+    player = candidates[0]
+    try:
+        player.do_command(command)
+    except NotImplementedError:
+        sys.stderr.write("Operation '%s' not support for player '%s'." % (command, player.__name__))
+        sys.exit(1)
+
+
+def find_players():
+    return [v for v in globals().values() if type(v) is type and issubclass(v, Player)]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.