#!/usr/bin/env python## Wrapper for controlling various music players, so that you can define# keyboard shortcuts that work for whatever player you are using. Ideally it# will still work even if multiple players are running if all but one are# 'stopped', but some music players do not have such a state or it cannot be# determined.#fromplayerdo.mainimportdo_test,do_command,find_playersfromplayerdo.backends.baseimportPlayerfromplayerdoimportinstallimportsysdefusage(players):# Print help and list of supported playershelp="""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=lambdac:len(c[0]))[-1][0])forname,doc,fincommands:help+=" "+name+" "*(max_len-len(name)+2)+doc+"\n"help+="""Not all operations are supported or fully supported by all players.Currently supported players (in the order they will currently be used):"""forpinplayers:n=getattr(p,'friendly_name',None)ifnisnotNone:help+=" "+n+"\n"returnhelpdefprint_usage(players):sys.stdout.write(usage(players))# List of commands: (name, docstring, callable)# The callable must accept a single argument, a list of players.commands=[]forcin["play","pause","unpause","togglepause","playpause","stop","next","prev","osd"]:defmk_command(name):defcommand(players):returndo_command(name,players)returncommandcommands.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),("install_gnome","Install keybindings for GNOME and launch keybinding editor",lambda*args:install.install_gnome())])command_dict=dict((name,f)forname,doc,fincommands)defmain(argv):players=find_players()iflen(argv)<2:print_usage(players)sys.exit(1)name=argv[1]command=command_dict.get(name,None)ifcommandisNone:sys.stderr.write("Unrecognised command '%s'.\n\n"%name)print_usage(players)sys.exit(1)else:command(players)if__name__=='__main__':main(sys.argv)