Esempio n. 1
0
    def open(self, lib, opts, args):
        query = ui.decargs(args)

        if opts.album:
            items = lib.albums(query)
        else:
            items = lib.items(query)

        if not items:
            raise ui.UserError("nothing to open")

        cmd = util.open_anything()
        if opts.args:
            cmd += " " + opts.args
        paths = [item.path for item in items]
        if opts.reveal:
            paths = [os.path.dirname(p) for p in paths]
        self._log.debug("invoking command: {} {}", cmd,
                        subprocess.list2cmdline(paths))

        item_type = "album" if opts.album else "track"
        item_type += "s" if len(items) > 1 else ""
        if opts.reveal:
            action = "Revealing"
        else:
            action = "Opening"
        ui.print_("{} {} {}.".format(action, len(items), item_type))

        try:
            util.interactive_open(paths, cmd)
        except OSError as exc:
            raise ui.UserError("failed to invoke {}: {}".format(cmd, exc))
Esempio n. 2
0
    def test_interactive_open(self, mock_open, mock_execlp):
        mock_open.return_value = u'tagada'
        util.interactive_open(['foo'], util.open_anything())
        mock_execlp.assert_called_once_with(u'tagada', u'tagada', u'foo')
        mock_execlp.reset_mock()

        util.interactive_open(['foo'], u'bar')
        mock_execlp.assert_called_once_with(u'bar', u'bar', u'foo')
Esempio n. 3
0
    def test_interactive_open(self, mock_open, mock_execlp):
        mock_open.return_value = u'tagada'
        util.interactive_open(['foo'], util.open_anything())
        mock_execlp.assert_called_once_with(u'tagada', u'tagada', u'foo')
        mock_execlp.reset_mock()

        util.interactive_open(['foo'], u'bar')
        mock_execlp.assert_called_once_with(u'bar', u'bar', u'foo')
Esempio n. 4
0
    def test_interactive_open(self, mock_open, mock_execlp):
        mock_open.return_value = 'tagada'
        util.interactive_open('foo')
        mock_execlp.assert_called_once_with('tagada', 'tagada', 'foo')
        mock_execlp.reset_mock()

        util.interactive_open('foo', 'bar')
        mock_execlp.assert_called_once_with('bar', 'bar', 'foo')
Esempio n. 5
0
    def test_interactive_open(self, mock_open, mock_execlp):
        mock_open.return_value = 'tagada'
        util.interactive_open('foo')
        mock_execlp.assert_called_once_with('tagada', 'tagada', 'foo')
        mock_execlp.reset_mock()

        util.interactive_open('foo', 'bar')
        mock_execlp.assert_called_once_with('bar', 'bar', 'foo')
Esempio n. 6
0
def play(command_str, selection, paths, open_args, log, item_type='track',
         keep_open=False):
    """Play items in paths with command_str and optional arguments. If
    keep_open, return to beets, otherwise exit once command runs.
    """
    # Print number of tracks or albums to be played, log command to be run.
    item_type += 's' if len(selection) > 1 else ''
    ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))
    log.debug(u'executing command: {} {!r}', command_str, open_args)

    try:
        if keep_open:
            command = util.shlex_split(command_str)
            command = command + open_args
            subprocess.call(command)
        else:
            util.interactive_open(open_args, command_str)
    except OSError as exc:
        raise ui.UserError(
            "Could not play the query: {0}".format(exc))
Esempio n. 7
0
    def play_music(self, lib, opts, args):
        """Execute query, create temporary playlist and execute player
        command passing that playlist, at request insert optional arguments.
        """
        command_str = config['play']['command'].get()
        if not command_str:
            command_str = util.open_anything()
        use_folders = config['play']['use_folders'].get(bool)
        relative_to = config['play']['relative_to'].get()
        raw = config['play']['raw'].get(bool)
        warning_threshold = config['play']['warning_threshold'].get(int)
        # We use -2 as a default value for warning_threshold to detect if it is
        # set or not. We can't use a falsey value because it would have an
        # actual meaning in the configuration of this plugin, and we do not use
        # -1 because some people might use it as a value to obtain no warning,
        # which wouldn't be that bad of a practice.
        if warning_threshold == -2:
            # if warning_threshold has not been set by user, look for
            # warning_treshold, to preserve backwards compatibility. See #1803.
            # warning_treshold has the correct default value of 100.
            warning_threshold = config['play']['warning_treshold'].get(int)

        if relative_to:
            relative_to = util.normpath(relative_to)

        # Add optional arguments to the player command.
        if opts.args:
            if ARGS_MARKER in command_str:
                command_str = command_str.replace(ARGS_MARKER, opts.args)
            else:
                command_str = u"{} {}".format(command_str, opts.args)

        # Perform search by album and add folders rather than tracks to
        # playlist.
        if opts.album:
            selection = lib.albums(ui.decargs(args))
            paths = []

            sort = lib.get_default_album_sort()
            for album in selection:
                if use_folders:
                    paths.append(album.item_dir())
                else:
                    paths.extend(item.path
                                 for item in sort.sort(album.items()))
            item_type = 'album'

        # Perform item query and add tracks to playlist.
        else:
            selection = lib.items(ui.decargs(args))
            paths = [item.path for item in selection]
            if relative_to:
                paths = [relpath(path, relative_to) for path in paths]
            item_type = 'track'

        item_type += 's' if len(selection) > 1 else ''

        if not selection:
            ui.print_(ui.colorize('text_warning',
                                  u'No {0} to play.'.format(item_type)))
            return

        # Warn user before playing any huge playlists.
        if warning_threshold and len(selection) > warning_threshold:
            ui.print_(ui.colorize(
                'text_warning',
                u'You are about to queue {0} {1}.'.format(
                    len(selection), item_type)))

            if ui.input_options((u'Continue', u'Abort')) == 'a':
                return

        ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))
        if raw:
            open_args = paths
        else:
            open_args = [self._create_tmp_playlist(paths)]

        self._log.debug(u'executing command: {} {!r}', command_str, open_args)
        try:
            util.interactive_open(open_args, command_str)
        except OSError as exc:
            raise ui.UserError(
                "Could not play the query: {0}".format(exc))
Esempio n. 8
0
    def play_music(self, lib, opts, args):
        """Execute query, create temporary playlist and execute player
        command passing that playlist, at request insert optional arguments.
        """
        command_str = config["play"]["command"].get()
        use_folders = config["play"]["use_folders"].get(bool)
        relative_to = config["play"]["relative_to"].get()
        raw = config["play"]["raw"].get(bool)
        if relative_to:
            relative_to = util.normpath(relative_to)

        # Add optional arguments to the player command.
        if opts.args:
            if ARGS_MARKER in command_str:
                command_str = command_str.replace(ARGS_MARKER, opts.args)
            else:
                command_str = "{} {}".format(command_str, opts.args)

        # Perform search by album and add folders rather than tracks to
        # playlist.
        if opts.album:
            selection = lib.albums(ui.decargs(args))
            paths = []

            sort = lib.get_default_album_sort()
            for album in selection:
                if use_folders:
                    paths.append(album.item_dir())
                else:
                    paths.extend(item.path for item in sort.sort(album.items()))
            item_type = "album"

        # Perform item query and add tracks to playlist.
        else:
            selection = lib.items(ui.decargs(args))
            paths = [item.path for item in selection]
            if relative_to:
                paths = [relpath(path, relative_to) for path in paths]
            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

        ui.print_("Playing {0} {1}.".format(len(selection), item_type))
        if raw:
            open_args = paths
        else:
            open_args = self._create_tmp_playlist(paths)

        self._log.debug("executing command: {} {}", command_str, b'"' + b" ".join(open_args) + b'"')
        try:
            util.interactive_open(open_args, command_str)
        except OSError as exc:
            raise ui.UserError("Could not play the music playlist: " "{0}".format(exc))
        finally:
            if not raw:
                self._log.debug("Removing temporary playlist: {}", open_args[0])
                util.remove(open_args[0])
Esempio n. 9
0
    def play_music(self, lib, opts, args):
        """Execute query, create temporary playlist and execute player
        command passing that playlist, at request insert optional arguments.
        """
        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)

        # Add optional arguments to the player command.
        if opts.args:
            command_str = "{} {}".format(command_str, opts.args)

        # Perform search by album and add folders rather than tracks to
        # playlist.
        if opts.album:
            selection = lib.albums(ui.decargs(args))
            paths = []

            sort = lib.get_default_album_sort()
            for album in selection:
                if use_folders:
                    paths.append(album.item_dir())
                else:
                    paths.extend(item.path
                                 for item in sort.sort(album.items()))
            item_type = 'album'

        # Perform 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()

        ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))

        self._log.debug('executing command: {} {}', command_str, m3u.name)
        try:
            util.interactive_open(m3u.name, command_str)
        except OSError as exc:
            raise ui.UserError("Could not play the music playlist: "
                               "{0}".format(exc))
        finally:
            util.remove(m3u.name)
Esempio n. 10
0
    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)

        # Perform search by album and add folders rather than tracks to
        # playlist.
        if opts.album:
            selection = lib.albums(ui.decargs(args))
            paths = []

            sort = lib.get_default_album_sort()
            for album in selection:
                if use_folders:
                    paths.append(album.item_dir())
                else:
                    paths.extend(item.path
                                 for item in sort.sort(album.items()))
            item_type = 'album'

        # Perform 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()

        ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))

        try:
            util.interactive_open(m3u.name, command_str)
        except OSError as exc:
            raise ui.UserError("Could not play the music playlist: "
                               "{0}".format(exc))
        finally:
            util.remove(m3u.name)
Esempio n. 11
0
    def play_music(self, lib, opts, args):
        """Execute query, create temporary playlist and execute player
        command passing that playlist, at request insert optional arguments.
        """
        command_str = config['play']['command'].get()
        if not command_str:
            command_str = util.open_anything()
        use_folders = config['play']['use_folders'].get(bool)
        relative_to = config['play']['relative_to'].get()
        raw = config['play']['raw'].get(bool)
        warning_treshold = config['play']['warning_treshold'].get(int)
        if relative_to:
            relative_to = util.normpath(relative_to)

        # Add optional arguments to the player command.
        if opts.args:
            if ARGS_MARKER in command_str:
                command_str = command_str.replace(ARGS_MARKER, opts.args)
            else:
                command_str = "{} {}".format(command_str, opts.args)

        # Perform search by album and add folders rather than tracks to
        # playlist.
        if opts.album:
            selection = lib.albums(ui.decargs(args))
            paths = []

            sort = lib.get_default_album_sort()
            for album in selection:
                if use_folders:
                    paths.append(album.item_dir())
                else:
                    paths.extend(item.path
                                 for item in sort.sort(album.items()))
            item_type = 'album'

        # Perform item query and add tracks to playlist.
        else:
            selection = lib.items(ui.decargs(args))
            paths = [item.path for item in selection]
            if relative_to:
                paths = [relpath(path, relative_to) for path in paths]
            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 warning_treshold and len(selection) > warning_treshold:
            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

        ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))
        if raw:
            open_args = paths
        else:
            open_args = [self._create_tmp_playlist(paths)]

        self._log.debug('executing command: {} {}', command_str,
                        b' '.join(open_args))
        try:
            util.interactive_open(open_args, command_str)
        except OSError as exc:
            raise ui.UserError("Could not play the query: " "{0}".format(exc))
Esempio n. 12
0
    def play_music(self, lib, opts, args):
        """Execute query, create temporary playlist and execute player
        command passing that playlist, at request insert optional arguments.
        """
        command_str = config['play']['command'].get()
        if not command_str:
            command_str = util.open_anything()
        use_folders = config['play']['use_folders'].get(bool)
        relative_to = config['play']['relative_to'].get()
        raw = config['play']['raw'].get(bool)
        warning_threshold = config['play']['warning_threshold'].get(int)
        # We use -2 as a default value for warning_threshold to detect if it is
        # set or not. We can't use a falsey value because it would have an
        # actual meaning in the configuration of this plugin, and we do not use
        # -1 because some people might use it as a value to obtain no warning,
        # which wouldn't be that bad of a practice.
        if warning_threshold == -2:
            # if warning_threshold has not been set by user, look for
            # warning_treshold, to preserve backwards compatibility. See #1803.
            # warning_treshold has the correct default value of 100.
            warning_threshold = config['play']['warning_treshold'].get(int)

        if relative_to:
            relative_to = util.normpath(relative_to)

        # Add optional arguments to the player command.
        if opts.args:
            if ARGS_MARKER in command_str:
                command_str = command_str.replace(ARGS_MARKER, opts.args)
            else:
                command_str = u"{} {}".format(command_str, opts.args)

        # Perform search by album and add folders rather than tracks to
        # playlist.
        if opts.album:
            selection = lib.albums(ui.decargs(args))
            paths = []

            sort = lib.get_default_album_sort()
            for album in selection:
                if use_folders:
                    paths.append(album.item_dir())
                else:
                    paths.extend(item.path
                                 for item in sort.sort(album.items()))
            item_type = 'album'

        # Perform item query and add tracks to playlist.
        else:
            selection = lib.items(ui.decargs(args))
            paths = [item.path for item in selection]
            if relative_to:
                paths = [relpath(path, relative_to) for path in paths]
            item_type = 'track'

        item_type += 's' if len(selection) > 1 else ''

        if not selection:
            ui.print_(
                ui.colorize('text_warning',
                            u'No {0} to play.'.format(item_type)))
            return

        # Warn user before playing any huge playlists.
        if warning_threshold and len(selection) > warning_threshold:
            ui.print_(
                ui.colorize(
                    'text_warning', u'You are about to queue {0} {1}.'.format(
                        len(selection), item_type)))

            if ui.input_options(('Continue', 'Abort')) == 'a':
                return

        ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))
        if raw:
            open_args = paths
        else:
            open_args = [self._create_tmp_playlist(paths)]

        self._log.debug(u'executing command: {} {}', command_str,
                        b' '.join(open_args))
        try:
            util.interactive_open(open_args, command_str)
        except OSError as exc:
            raise ui.UserError("Could not play the query: {0}".format(exc))
Esempio n. 13
0
File: play.py Progetto: Ifiht/beets
    def play_music(self, lib, opts, args):
        """Execute query, create temporary playlist and execute player
        command passing that playlist, at request insert optional arguments.
        """
        command_str = config['play']['command'].get()
        use_folders = config['play']['use_folders'].get(bool)
        relative_to = config['play']['relative_to'].get()
        raw = config['play']['raw'].get(bool)
        warning_treshold = config['play']['warning_treshold'].get(int)
        if relative_to:
            relative_to = util.normpath(relative_to)

        # Add optional arguments to the player command.
        if opts.args:
            if ARGS_MARKER in command_str:
                command_str = command_str.replace(ARGS_MARKER, opts.args)
            else:
                command_str = "{} {}".format(command_str, opts.args)

        # Perform search by album and add folders rather than tracks to
        # playlist.
        if opts.album:
            selection = lib.albums(ui.decargs(args))
            paths = []

            sort = lib.get_default_album_sort()
            for album in selection:
                if use_folders:
                    paths.append(album.item_dir())
                else:
                    paths.extend(item.path
                                 for item in sort.sort(album.items()))
            item_type = 'album'

        # Perform item query and add tracks to playlist.
        else:
            selection = lib.items(ui.decargs(args))
            paths = [item.path for item in selection]
            if relative_to:
                paths = [relpath(path, relative_to) for path in paths]
            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 warning_treshold and len(selection) > warning_treshold:
            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

        ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))
        if raw:
            open_args = paths
        else:
            open_args = [self._create_tmp_playlist(paths)]

        self._log.debug('executing command: {} {}', command_str,
                        b' '.join(open_args))
        try:
            util.interactive_open(open_args, command_str)
        except OSError as exc:
            raise ui.UserError("Could not play the music playlist: "
                               "{0}".format(exc))
        finally:
            if not raw:
                self._log.debug('Removing temporary playlist: {}',
                                open_args[0])
                util.remove(open_args[0])