Commits

Luke Plant committed 9ec6350

Added sorting of players by a class attribute, and used this sorting when displaying help text (mainly for easy debugging)

Comments (0)

Files changed (3)

playerdo/backends/base.py

 
     process_name = None # used for pidof
     friendly_name = None # used for display in help
+    sort_order = 0 # set lower for higher priority
 
     def is_running(self):
         """

playerdo/backends/mpris.py

 
     _friendly_name = "Any MPRIS 1 player"
 
+    # This is a generic interface, so it less preferred
+
+    sort_order = 10
+
     @property
     def friendly_name(self):
         retval = self._friendly_name
 import sys
 
 
+def sort_players(players):
+    """
+    Returns list of players, sorted by priority.
+    """
+    # Try to find out which one is playing
+    states = []
+    orders = []
+    for p in players:
+        running = False
+        try:
+            running = p.is_running()
+        except NotImplementedError:
+            running = False
+
+        if not running:
+            state = 3
+        else:
+            try:
+                is_stopped = p.is_stopped()
+            except NotImplementedError:
+                is_stopped = None
+
+            if is_stopped == True:
+                state = 2
+            elif is_stopped == False:
+                state = 0
+            else:
+                # In-between value for unknowns, because a player that is
+                # definitely stopped is less preferred than one that *might* be
+                # playing.
+                state = 1
+
+        states.append(state)
+        orders.append(p.sort_order)
+
+    l = zip(states, orders, players)
+    l.sort()
+
+    return [x[2] for x in l]
+
 def get_running_players(players):
     """
     Given a list of Player classes, returns a list of players (instances) that
-    are running, sorted by priority they should be tried.
+    are running.
     """
-
     # Get running players
     running_ps = []
     for p in players:
         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]
-
+    return running_ps
 
 def do_test(players):
     """
         sys.exit(1)
 
 def find_players():
-    return [v() for v in globals().values()
-            if type(v) is type and issubclass(v, Player)]
+    return sort_players([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.