mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +01:00
Play plugin: use OO, don't pass log around
This commit is contained in:
parent
494990a7a2
commit
1d8160f9ff
1 changed files with 88 additions and 88 deletions
|
|
@ -17,8 +17,6 @@
|
||||||
from __future__ import (division, absolute_import, print_function,
|
from __future__ import (division, absolute_import, print_function,
|
||||||
unicode_literals)
|
unicode_literals)
|
||||||
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
from beets.plugins import BeetsPlugin
|
from beets.plugins import BeetsPlugin
|
||||||
from beets.ui import Subcommand
|
from beets.ui import Subcommand
|
||||||
from beets import config
|
from beets import config
|
||||||
|
|
@ -30,91 +28,6 @@ import shlex
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
|
|
||||||
def play_music(lib, opts, args, log):
|
|
||||||
"""Execute query, create temporary playlist and execute player
|
|
||||||
command passing that playlist.
|
|
||||||
"""
|
|
||||||
command_str = config['play']['command'].get()
|
|
||||||
use_folders = config['play']['use_folders'].get(bool)
|
|
||||||
relative_to = config['play']['relative_to'].get()
|
|
||||||
if relative_to:
|
|
||||||
relative_to = util.normpath(relative_to)
|
|
||||||
if command_str:
|
|
||||||
command = shlex.split(command_str)
|
|
||||||
else:
|
|
||||||
# If a command isn't set, then let the OS decide how to open the
|
|
||||||
# playlist.
|
|
||||||
sys_name = platform.system()
|
|
||||||
if sys_name == 'Darwin':
|
|
||||||
command = ['open']
|
|
||||||
elif sys_name == 'Windows':
|
|
||||||
command = ['start']
|
|
||||||
else:
|
|
||||||
# If not Mac or Windows, then assume Unixy.
|
|
||||||
command = ['xdg-open']
|
|
||||||
|
|
||||||
# Preform search by album and add folders rather then tracks to playlist.
|
|
||||||
if opts.album:
|
|
||||||
selection = lib.albums(ui.decargs(args))
|
|
||||||
paths = []
|
|
||||||
|
|
||||||
for album in selection:
|
|
||||||
if use_folders:
|
|
||||||
paths.append(album.item_dir())
|
|
||||||
else:
|
|
||||||
# TODO use core's sorting functionality
|
|
||||||
paths.extend([item.path for item in sorted(
|
|
||||||
album.items(), key=lambda item: (item.disc, item.track))])
|
|
||||||
item_type = 'album'
|
|
||||||
|
|
||||||
# Preform item query and add tracks to playlist.
|
|
||||||
else:
|
|
||||||
selection = lib.items(ui.decargs(args))
|
|
||||||
paths = [item.path for item in selection]
|
|
||||||
item_type = 'track'
|
|
||||||
|
|
||||||
item_type += 's' if len(selection) > 1 else ''
|
|
||||||
|
|
||||||
if not selection:
|
|
||||||
ui.print_(ui.colorize('text_warning',
|
|
||||||
'No {0} to play.'.format(item_type)))
|
|
||||||
return
|
|
||||||
|
|
||||||
# Warn user before playing any huge playlists.
|
|
||||||
if len(selection) > 100:
|
|
||||||
ui.print_(ui.colorize(
|
|
||||||
'text_warning',
|
|
||||||
'You are about to queue {0} {1}.'.format(len(selection), item_type)
|
|
||||||
))
|
|
||||||
|
|
||||||
if ui.input_options(('Continue', 'Abort')) == 'a':
|
|
||||||
return
|
|
||||||
|
|
||||||
# Create temporary m3u file to hold our playlist.
|
|
||||||
m3u = NamedTemporaryFile('w', suffix='.m3u', delete=False)
|
|
||||||
for item in paths:
|
|
||||||
if relative_to:
|
|
||||||
m3u.write(relpath(item, relative_to) + b'\n')
|
|
||||||
else:
|
|
||||||
m3u.write(item + b'\n')
|
|
||||||
m3u.close()
|
|
||||||
|
|
||||||
command.append(m3u.name)
|
|
||||||
|
|
||||||
# Invoke the command and log the output.
|
|
||||||
output = util.command_output(command)
|
|
||||||
if output:
|
|
||||||
log.debug(u'Output of {0}: {1}',
|
|
||||||
util.displayable_path(command[0]),
|
|
||||||
output.decode('utf8', 'ignore'))
|
|
||||||
else:
|
|
||||||
log.debug(u'no output')
|
|
||||||
|
|
||||||
ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))
|
|
||||||
|
|
||||||
util.remove(m3u.name)
|
|
||||||
|
|
||||||
|
|
||||||
class PlayPlugin(BeetsPlugin):
|
class PlayPlugin(BeetsPlugin):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -136,5 +49,92 @@ class PlayPlugin(BeetsPlugin):
|
||||||
action='store_true', default=False,
|
action='store_true', default=False,
|
||||||
help='query and load albums rather than tracks'
|
help='query and load albums rather than tracks'
|
||||||
)
|
)
|
||||||
play_command.func = partial(play_music, log=self._log)
|
play_command.func = self.play_music
|
||||||
return [play_command]
|
return [play_command]
|
||||||
|
|
||||||
|
def play_music(self, lib, opts, args):
|
||||||
|
"""Execute query, create temporary playlist and execute player
|
||||||
|
command passing that playlist.
|
||||||
|
"""
|
||||||
|
command_str = config['play']['command'].get()
|
||||||
|
use_folders = config['play']['use_folders'].get(bool)
|
||||||
|
relative_to = config['play']['relative_to'].get()
|
||||||
|
if relative_to:
|
||||||
|
relative_to = util.normpath(relative_to)
|
||||||
|
if command_str:
|
||||||
|
command = shlex.split(command_str)
|
||||||
|
else:
|
||||||
|
# If a command isn't set, then let the OS decide how to open the
|
||||||
|
# playlist.
|
||||||
|
sys_name = platform.system()
|
||||||
|
if sys_name == 'Darwin':
|
||||||
|
command = ['open']
|
||||||
|
elif sys_name == 'Windows':
|
||||||
|
command = ['start']
|
||||||
|
else:
|
||||||
|
# If not Mac or Windows, then assume Unixy.
|
||||||
|
command = ['xdg-open']
|
||||||
|
|
||||||
|
# Preform search by album and add folders rather than tracks to
|
||||||
|
# playlist.
|
||||||
|
if opts.album:
|
||||||
|
selection = lib.albums(ui.decargs(args))
|
||||||
|
paths = []
|
||||||
|
|
||||||
|
for album in selection:
|
||||||
|
if use_folders:
|
||||||
|
paths.append(album.item_dir())
|
||||||
|
else:
|
||||||
|
# TODO use core's sorting functionality
|
||||||
|
paths.extend([item.path for item in sorted(
|
||||||
|
album.items(),
|
||||||
|
key=lambda item: (item.disc, item.track))])
|
||||||
|
item_type = 'album'
|
||||||
|
|
||||||
|
# Preform item query and add tracks to playlist.
|
||||||
|
else:
|
||||||
|
selection = lib.items(ui.decargs(args))
|
||||||
|
paths = [item.path for item in selection]
|
||||||
|
item_type = 'track'
|
||||||
|
|
||||||
|
item_type += 's' if len(selection) > 1 else ''
|
||||||
|
|
||||||
|
if not selection:
|
||||||
|
ui.print_(ui.colorize('text_warning',
|
||||||
|
'No {0} to play.'.format(item_type)))
|
||||||
|
return
|
||||||
|
|
||||||
|
# Warn user before playing any huge playlists.
|
||||||
|
if len(selection) > 100:
|
||||||
|
ui.print_(ui.colorize(
|
||||||
|
'text_warning',
|
||||||
|
'You are about to queue {0} {1}.'.format(len(selection),
|
||||||
|
item_type)
|
||||||
|
))
|
||||||
|
|
||||||
|
if ui.input_options(('Continue', 'Abort')) == 'a':
|
||||||
|
return
|
||||||
|
|
||||||
|
# Create temporary m3u file to hold our playlist.
|
||||||
|
m3u = NamedTemporaryFile('w', suffix='.m3u', delete=False)
|
||||||
|
for item in paths:
|
||||||
|
if relative_to:
|
||||||
|
m3u.write(relpath(item, relative_to) + b'\n')
|
||||||
|
else:
|
||||||
|
m3u.write(item + b'\n')
|
||||||
|
m3u.close()
|
||||||
|
|
||||||
|
command.append(m3u.name)
|
||||||
|
|
||||||
|
# Invoke the command and log the output.
|
||||||
|
output = util.command_output(command)
|
||||||
|
if output:
|
||||||
|
self._log.debug(u'Output of {0}: {1}',
|
||||||
|
util.displayable_path(command[0]),
|
||||||
|
output.decode('utf8', 'ignore'))
|
||||||
|
else:
|
||||||
|
self._log.debug(u'no output')
|
||||||
|
|
||||||
|
ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))
|
||||||
|
|
||||||
|
util.remove(m3u.name)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue