Esempio n. 1
0
def listall(context, uri=None):
    """
    *musicpd.org, music database section:*

        ``listall [URI]``

        Lists all songs and directories in ``URI``.
    """
    result = []
    root_path = translator.normalize_path(uri)
    # TODO: doesn't the dispatcher._call_handler have enough info to catch
    # the error this can produce, set the command and then 'raise'?
    try:
        uri = context.directory_path_to_uri(root_path)
    except MpdNoExistError as e:
        e.command = 'listall'
        e.message = 'Not found'
        raise
    browse_futures = [(root_path, context.core.library.browse(uri))]

    while browse_futures:
        base_path, future = browse_futures.pop()
        for ref in future.get():
            if ref.type == Ref.DIRECTORY:
                path = '/'.join([base_path, ref.name.replace('/', '')])
                result.append(('directory', path))
                browse_futures.append(
                    (path, context.core.library.browse(ref.uri)))
            elif ref.type == Ref.TRACK:
                result.append(('file', ref.uri))

    if not result:
        raise MpdNoExistError('Not found')

    return [('directory', root_path)] + result
Esempio n. 2
0
def listall(context, uri=None):
    """
    *musicpd.org, music database section:*

        ``listall [URI]``

        Lists all songs and directories in ``URI``.
    """
    result = []
    root_path = translator.normalize_path(uri)
    # TODO: doesn't the dispatcher._call_handler have enough info to catch
    # the error this can produce, set the command and then 'raise'?
    try:
        uri = context.directory_path_to_uri(root_path)
    except MpdNoExistError as e:
        e.command = 'listall'
        e.message = 'Not found'
        raise
    browse_futures = [(root_path, context.core.library.browse(uri))]

    while browse_futures:
        base_path, future = browse_futures.pop()
        for ref in future.get():
            if ref.type == Ref.DIRECTORY:
                path = '/'.join([base_path, ref.name.replace('/', '')])
                result.append(('directory', path))
                browse_futures.append(
                    (path, context.core.library.browse(ref.uri)))
            elif ref.type == Ref.TRACK:
                result.append(('file', ref.uri))

    if not result:
        raise MpdNoExistError('Not found')

    return [('directory', root_path)] + result
Esempio n. 3
0
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

    tl_tracks = context.core.tracklist.add(uri=uri).get()
    if tl_tracks:
        return

    try:
        uri = context.directory_path_to_uri(translator.normalize_path(uri))
    except MpdNoExistError as e:
        e.command = 'add'
        e.message = 'directory or file not found'
        raise

    browse_futures = [context.core.library.browse(uri)]
    lookup_futures = []
    while browse_futures:
        for ref in browse_futures.pop().get():
            if ref.type == ref.DIRECTORY:
                browse_futures.append(context.core.library.browse(ref.uri))
            else:
                lookup_futures.append(context.core.library.lookup(ref.uri))

    tracks = []
    for future in lookup_futures:
        tracks.extend(future.get())

    if not tracks:
        raise MpdNoExistError('directory or file not found')

    context.core.tracklist.add(tracks=tracks)
Esempio n. 4
0
def listallinfo(context, uri=None):
    """
    *musicpd.org, music database section:*

        ``listallinfo [URI]``

        Same as ``listall``, except it also returns metadata info in the
        same format as ``lsinfo``.
    """
    dirs_and_futures = []
    result = []
    root_path = translator.normalize_path(uri)
    try:
        uri = context.directory_path_to_uri(root_path)
    except MpdNoExistError as e:
        e.command = 'listallinfo'
        e.message = 'Not found'
        raise
    browse_futures = [(root_path, context.core.library.browse(uri))]

    while browse_futures:
        base_path, future = browse_futures.pop()
        for ref in future.get():
            if ref.type == Ref.DIRECTORY:
                path = '/'.join([base_path, ref.name.replace('/', '')])
                future = context.core.library.browse(ref.uri)
                browse_futures.append((path, future))
                dirs_and_futures.append(('directory', path))
            elif ref.type == Ref.TRACK:
                # TODO Lookup tracks in batch for better performance
                dirs_and_futures.append(context.core.library.lookup(ref.uri))

    result = []
    for obj in dirs_and_futures:
        if hasattr(obj, 'get'):
            for track in obj.get():
                result.extend(translator.track_to_mpd_format(track))
        else:
            result.append(obj)

    if not result:
        raise MpdNoExistError('Not found')

    return [('directory', root_path)] + result
Esempio n. 5
0
def listallinfo(context, uri=None):
    """
    *musicpd.org, music database section:*

        ``listallinfo [URI]``

        Same as ``listall``, except it also returns metadata info in the
        same format as ``lsinfo``.
    """
    dirs_and_futures = []
    result = []
    root_path = translator.normalize_path(uri)
    try:
        uri = context.directory_path_to_uri(root_path)
    except MpdNoExistError as e:
        e.command = 'listallinfo'
        e.message = 'Not found'
        raise
    browse_futures = [(root_path, context.core.library.browse(uri))]

    while browse_futures:
        base_path, future = browse_futures.pop()
        for ref in future.get():
            if ref.type == Ref.DIRECTORY:
                path = '/'.join([base_path, ref.name.replace('/', '')])
                future = context.core.library.browse(ref.uri)
                browse_futures.append((path, future))
                dirs_and_futures.append(('directory', path))
            elif ref.type == Ref.TRACK:
                # TODO Lookup tracks in batch for better performance
                dirs_and_futures.append(context.core.library.lookup(ref.uri))

    result = []
    for obj in dirs_and_futures:
        if hasattr(obj, 'get'):
            for track in obj.get():
                result.extend(translator.track_to_mpd_format(track))
        else:
            result.append(obj)

    if not result:
        raise MpdNoExistError('Not found')

    return [('directory', root_path)] + result
Esempio n. 6
0
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

    tl_tracks = context.core.tracklist.add(uri=uri).get()
    if tl_tracks:
        return

    try:
        uri = context.directory_path_to_uri(translator.normalize_path(uri))
    except MpdNoExistError as e:
        e.command = 'add'
        e.message = 'directory or file not found'
        raise

    browse_futures = [context.core.library.browse(uri)]
    lookup_futures = []
    while browse_futures:
        for ref in browse_futures.pop().get():
            if ref.type == ref.DIRECTORY:
                browse_futures.append(context.core.library.browse(ref.uri))
            else:
                lookup_futures.append(context.core.library.lookup(ref.uri))

    tracks = []
    for future in lookup_futures:
        tracks.extend(future.get())

    if not tracks:
        raise MpdNoExistError('directory or file not found')

    context.core.tracklist.add(tracks=tracks)
Esempio n. 7
0
def lsinfo(context, uri=None):
    """
    *musicpd.org, music database section:*

        ``lsinfo [URI]``

        Lists the contents of the directory ``URI``.

        When listing the root directory, this currently returns the list of
        stored playlists. This behavior is deprecated; use
        ``listplaylists`` instead.

    MPD returns the same result, including both playlists and the files and
    directories located at the root level, for both ``lsinfo``, ``lsinfo
    ""``, and ``lsinfo "/"``.
    """
    result = []
    root_path = translator.normalize_path(uri, relative=True)
    try:
        uri = context.directory_path_to_uri(root_path)
    except MpdNoExistError as e:
        e.command = 'lsinfo'
        e.message = 'Not found'
        raise

    if uri is None:
        result.extend(stored_playlists.listplaylists(context))

    for ref in context.core.library.browse(uri).get():
        if ref.type == Ref.DIRECTORY:
            path = '/'.join([root_path, ref.name.replace('/', '')])
            result.append(('directory', path.lstrip('/')))
        elif ref.type == Ref.TRACK:
            # TODO Lookup tracks in batch for better performance
            tracks = context.core.library.lookup(ref.uri).get()
            if tracks:
                result.extend(translator.track_to_mpd_format(tracks[0]))
    return result
Esempio n. 8
0
def lsinfo(context, uri=None):
    """
    *musicpd.org, music database section:*

        ``lsinfo [URI]``

        Lists the contents of the directory ``URI``.

        When listing the root directory, this currently returns the list of
        stored playlists. This behavior is deprecated; use
        ``listplaylists`` instead.

    MPD returns the same result, including both playlists and the files and
    directories located at the root level, for both ``lsinfo``, ``lsinfo
    ""``, and ``lsinfo "/"``.
    """
    result = []
    root_path = translator.normalize_path(uri, relative=True)
    try:
        uri = context.directory_path_to_uri(root_path)
    except MpdNoExistError as e:
        e.command = 'lsinfo'
        e.message = 'Not found'
        raise

    if uri is None:
        result.extend(stored_playlists.listplaylists(context))

    for ref in context.core.library.browse(uri).get():
        if ref.type == Ref.DIRECTORY:
            path = '/'.join([root_path, ref.name.replace('/', '')])
            result.append(('directory', path.lstrip('/')))
        elif ref.type == Ref.TRACK:
            # TODO Lookup tracks in batch for better performance
            tracks = context.core.library.lookup(ref.uri).get()
            if tracks:
                result.extend(translator.track_to_mpd_format(tracks[0]))
    return result