def addid(context, uri, songpos=None):
    """
    *musicpd.org, current playlist section:*

        ``addid {URI} [POSITION]``

        Adds a song to the playlist (non-recursive) and returns the song id.

        ``URI`` is always a single file or URL. For example::

            addid "foo.mp3"
            Id: 999
            OK

    *Clarifications:*

    - ``addid ""`` should return an error.
    """
    if not uri:
        raise exceptions.MpdNoExistError("No such song")

    length = context.core.tracklist.get_length()
    if songpos is not None and songpos > length.get():
        raise exceptions.MpdArgError("Bad song index")

    tl_tracks = context.core.tracklist.add(uris=[uri],
                                           at_position=songpos).get()

    if not tl_tracks:
        raise exceptions.MpdNoExistError("No such song")
    return ("Id", tl_tracks[0].tlid)
def add(context, uri):
    """
    *musicpd.org, current playlist section:*

        ``add {URI}``

        Adds the file ``URI`` to the playlist (directories add recursively).
        ``URI`` can also be a single file.

    *Clarifications:*

    - ``add ""`` should add all tracks in the library to the current playlist.
    """
    if not uri.strip("/"):
        return

    # If we have an URI just try and add it directly without bothering with
    # jumping through browse...
    if urllib.parse.urlparse(uri).scheme != "":
        if context.core.tracklist.add(uris=[uri]).get():
            return

    try:
        uris = []
        for _path, ref in context.browse(uri, lookup=False):
            if ref:
                uris.append(ref.uri)
    except exceptions.MpdNoExistError as exc:
        exc.message = (  # noqa B306: Our own exception
            "directory or file not found")
        raise

    if not uris:
        raise exceptions.MpdNoExistError("directory or file not found")
    context.core.tracklist.add(uris=uris).get()
Example #3
0
def listall(context, uri=None):
    """
    *musicpd.org, music database section:*

        ``listall [URI]``

        Lists all songs and directories in ``URI``.

        Do not use this command. Do not manage a client-side copy of MPD's
        database. That is fragile and adds huge overhead. It will break with
        large databases. Instead, query MPD whenever you need something.


    .. warning:: This command is disabled by default in Mopidy installs.
    """
    result = []
    for path, track_ref in context.browse(uri, lookup=False):
        if not track_ref:
            result.append(("directory", path.lstrip("/")))
        else:
            result.append(("file", track_ref.uri))

    if not result:
        raise exceptions.MpdNoExistError("Not found")
    return result
Example #4
0
def _get_playlist(context, name, must_exist=True):
    playlist = None
    uri = context.lookup_playlist_uri_from_name(name)
    if uri:
        playlist = context.core.playlists.lookup(uri).get()
    if must_exist and playlist is None:
        raise exceptions.MpdNoExistError("No such playlist")
    return playlist
Example #5
0
    def browse(self, path, recursive=True, lookup=True):
        """
        Browse the contents of a given directory path.

        Returns a sequence of two-tuples ``(path, data)``.

        If ``recursive`` is true, it returns results for all entries in the
        given path.

        If ``lookup`` is true and the ``path`` is to a track, the returned
        ``data`` is a future which will contain the results from looking up
        the URI with :meth:`mopidy.core.LibraryController.lookup`. If
        ``lookup`` is false and the ``path`` is to a track, the returned
        ``data`` will be a :class:`mopidy.models.Ref` for the track.

        For all entries that are not tracks, the returned ``data`` will be
        :class:`None`.
        """

        path_parts = re.findall(r"[^/]+", path or "")
        root_path = "/".join([""] + path_parts)

        uri = self._uri_map.uri_from_name(root_path)
        if uri is None:
            for part in path_parts:
                for ref in self.core.library.browse(uri).get():
                    if ref.type != ref.TRACK and ref.name == part:
                        uri = ref.uri
                        break
                else:
                    raise exceptions.MpdNoExistError("Not found")
            root_path = self._uri_map.insert(root_path, uri)

        if recursive:
            yield (root_path, None)

        path_and_futures = [(root_path, self.core.library.browse(uri))]
        while path_and_futures:
            base_path, future = path_and_futures.pop()
            for ref in future.get():
                if ref.name is None or ref.uri is None:
                    continue

                path = "/".join([base_path, ref.name.replace("/", "")])
                path = self._uri_map.insert(path, ref.uri)

                if ref.type == ref.TRACK:
                    if lookup:
                        # TODO: can we lookup all the refs at once now?
                        yield (path, self.core.library.lookup(uris=[ref.uri]))
                    else:
                        yield (path, ref)
                else:
                    yield (path, None)
                    if recursive:
                        path_and_futures.append(
                            (path, self.core.library.browse(ref.uri)))
def deleteid(context, tlid):
    """
    *musicpd.org, current playlist section:*

        ``deleteid {SONGID}``

        Deletes the song ``SONGID`` from the playlist
    """
    tl_tracks = context.core.tracklist.remove({"tlid": [tlid]}).get()
    if not tl_tracks:
        raise exceptions.MpdNoExistError("No such song")
Example #7
0
def rm(context, name):
    """
    *musicpd.org, stored playlists section:*

        ``rm {NAME}``

        Removes the playlist ``NAME.m3u`` from the playlist directory.
    """
    _check_playlist_name(name)
    uri = context.lookup_playlist_uri_from_name(name)
    if not uri:
        raise exceptions.MpdNoExistError("No such playlist")
    context.core.playlists.delete(uri).get()
Example #8
0
def disableoutput(context, outputid):
    """
    *musicpd.org, audio output section:*

        ``disableoutput {ID}``

        Turns an output off.
    """
    if outputid == 0:
        success = context.core.mixer.set_mute(False).get()
        if not success:
            raise exceptions.MpdSystemError("problems disabling output")
    else:
        raise exceptions.MpdNoExistError("No such audio output")
def swapid(context, tlid1, tlid2):
    """
    *musicpd.org, current playlist section:*

        ``swapid {SONG1} {SONG2}``

        Swaps the positions of ``SONG1`` and ``SONG2`` (both song ids).
    """
    tl_tracks1 = context.core.tracklist.filter({"tlid": [tlid1]}).get()
    tl_tracks2 = context.core.tracklist.filter({"tlid": [tlid2]}).get()
    if not tl_tracks1 or not tl_tracks2:
        raise exceptions.MpdNoExistError("No such song")
    position1 = context.core.tracklist.index(tl_tracks1[0]).get()
    position2 = context.core.tracklist.index(tl_tracks2[0]).get()
    swap(context, position1, position2)
def moveid(context, tlid, to):
    """
    *musicpd.org, current playlist section:*

        ``moveid {FROM} {TO}``

        Moves the song with ``FROM`` (songid) to ``TO`` (playlist index) in
        the playlist. If ``TO`` is negative, it is relative to the current
        song in the playlist (if there is one).
    """
    tl_tracks = context.core.tracklist.filter({"tlid": [tlid]}).get()
    if not tl_tracks:
        raise exceptions.MpdNoExistError("No such song")
    position = context.core.tracklist.index(tl_tracks[0]).get()
    context.core.tracklist.move(position, position + 1, to)
Example #11
0
def toggleoutput(context, outputid):
    """
    *musicpd.org, audio output section:*

        ``toggleoutput {ID}``

        Turns an output on or off, depending on the current state.
    """
    if outputid == 0:
        mute_status = context.core.mixer.get_mute().get()
        success = context.core.mixer.set_mute(not mute_status)
        if not success:
            raise exceptions.MpdSystemError("problems toggling output")
    else:
        raise exceptions.MpdNoExistError("No such audio output")
def playlistid(context, tlid=None):
    """
    *musicpd.org, current playlist section:*

        ``playlistid {SONGID}``

        Displays a list of songs in the playlist. ``SONGID`` is optional
        and specifies a single song to display info for.
    """
    if tlid is not None:
        tl_tracks = context.core.tracklist.filter({"tlid": [tlid]}).get()
        if not tl_tracks:
            raise exceptions.MpdNoExistError("No such song")
        position = context.core.tracklist.index(tl_tracks[0]).get()
        return translator.track_to_mpd_format(tl_tracks[0], position=position)
    else:
        return translator.tracks_to_mpd_format(
            context.core.tracklist.get_tl_tracks().get())
Example #13
0
def playid(context, tlid):
    """
    *musicpd.org, playback section:*

        ``playid [SONGID]``

        Begins playing the playlist at song ``SONGID``.

    *Clarifications:*

    - ``playid "-1"`` when playing is ignored.
    - ``playid "-1"`` when paused resumes playback.
    - ``playid "-1"`` when stopped with a current track starts playback at the
      current track.
    - ``playid "-1"`` when stopped without a current track, e.g. after playlist
      replacement, starts playback at the first track.
    """
    if tlid == -1:
        return _play_minus_one(context)
    tl_tracks = context.core.tracklist.filter({"tlid": [tlid]}).get()
    if not tl_tracks:
        raise exceptions.MpdNoExistError("No such song")
    return context.core.playback.play(tl_tracks[0]).get()