def run(music_dir): music_dir = os.path.abspath(os.path.expanduser(music_dir)) if not os.path.isdir(music_dir): raise FileNotFoundError(music_dir) logger = logging.getLogger('mpd') global _mpd_dir _mpd_dir = TemporaryDirectory(prefix='e2e-mpd.') mpd_dir = _mpd_dir.name # create temporary config for mpd mpd_db = os.path.join(mpd_dir, 'db') os.mknod(mpd_db) mpd_socket = os.path.join(mpd_dir, 'socket') os.mknod(mpd_socket) mpd_conf = os.path.join(mpd_dir, 'mpd.conf') with open(mpd_conf, 'w') as f: f.write('db_file "{}"\n'.format(mpd_db)) f.write('log_file "syslog"\n') f.write('bind_to_address "{}"\n'.format(mpd_socket)) f.write('music_directory "{}"\n'.format(music_dir)) logger.debug('created mpd config in {}'.format(mpd_dir)) logger.debug('starting on {}...'.format(mpd_socket)) # run mpd global _mpd_proc _mpd_proc = Popen(['mpd', '--no-daemon', mpd_conf]) daemon.set_socket(mpd_socket) # wait for mpd to start while True: try: playback.get_status() break except (ConnectionRefusedError, FileNotFoundError): pass # wait for music database while 'db_update' not in daemon.get_dict("stats"): try: daemon.get_query("idle database", timeout=0.5) except socket.timeout: pass logger.info('ready')
def pause_toggle(): state = get_status().state if state == 'play': get_query('pause 1') elif state == 'pause': get_query('pause 0') elif state == 'stop': get_query('play')
def single_toggle(): if get_status().single: get_query('single 0') else: get_query('single 1')
def random_toggle(): if get_status().random: get_query('random 0') else: get_query('random 1')
def repeat_toggle(): if get_status().repeat: get_query('repeat 0') else: get_query('repeat 1')
def previous(): get_query('previous')
def next(): get_query('next')
def stop(): get_query('stop')
def play(index=0): get_query('play {}'.format(index))
def move(index, to): get_query('move {} {}'.format(index, to))
def remove(index): get_query('delete {}'.format(index))
def add_album(song): query = 'findadd AlbumArtist "{}" Date "{}" Album "{}"' get_query(query.format(song.albumartist, song.date, song.album))
def add(song): get_query('add "{}"'.format(song.path))
def clear(): get_query('clear')