playerdo / playerdo / main.py

from playerdo.backends import *
from playerdo.backends.base import Player
from playerdo.utils import PlayerException
import sys


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.
    """

    # 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):
    """
    Checks that all backends have required dependencies, printing any failures
    """
    for P in players:
        p = P()
        failures = p.check_dependencies()
        if len(failures) > 0:
            sys.stdout.write("Player '%s' has missing dependencies:\n" % p.friendly_name)
            for l in failures:
                sys.stdout.write("  " + l + "\n")


def do_command(command, players):
    """
    Execute the given command, given a list of Player classes
    """
    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 supported for player '%s'.\n" %
                         (command, player.friendly_name))
        sys.exit(1)
    except PlayerException, e:
        sys.stderr.write(e.message + "\n")
        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.