Beispiel #1
0
def list_items(lib, query, album, path, fmt):
    """Print out items in lib matching query. If album, then search for
    albums instead of single items. If path, print the matched objects'
    paths instead of human-readable information about them.
    """
    if fmt is None:
        # If no specific template is supplied, use a default.
        if album:
            fmt = u'$albumartist - $album'
        else:
            fmt = u'$artist - $album - $title'
    template = Template(fmt)

    if album:
        for album in lib.albums(query):
            if path:
                print_(album.item_dir())
            elif fmt is not None:
                print_(template.substitute(album._record))
    else:
        for item in lib.items(query):
            if path:
                print_(item.path)
            elif fmt is not None:
                print_(template.substitute(item.record))
Beispiel #2
0
    def destination(self, item, pathmod=None, in_album=False, fragment=False, basedir=None):
        """Returns the path in the library directory designated for item
        item (i.e., where the file ought to be). in_album forces the
        item to be treated as part of an album. fragment makes this
        method return just the path fragment underneath the root library
        directory; the path is also returned as Unicode instead of
        encoded as a bytestring. basedir can override the library's base
        directory for the destination.
        """
        pathmod = pathmod or os.path

        # Use a path format based on a query, falling back on the
        # default.
        for query, path_format in self.path_formats:
            if query == PF_KEY_DEFAULT:
                continue
            query = AndQuery.from_string(query)
            if in_album:
                # If we're treating this item as a member of the item,
                # hack the query so that singleton queries always
                # observe the item to be non-singleton.
                for i, subquery in enumerate(query):
                    if isinstance(subquery, SingletonQuery):
                        query[i] = FalseQuery() if subquery.sense else TrueQuery()
            if query.match(item):
                # The query matches the item! Use the corresponding path
                # format.
                break
        else:
            # No query matched; fall back to default.
            for query, path_format in self.path_formats:
                if query == PF_KEY_DEFAULT:
                    break
            else:
                assert False, "no default path format"
        subpath_tmpl = Template(path_format)

        # Get the item's Album if it has one.
        album = self.get_album(item)

        # Build the mapping for substitution in the path template,
        # beginning with the values from the database.
        mapping = {}
        for key in ITEM_KEYS_META:
            # Get the values from either the item or its album.
            if key in ALBUM_KEYS_ITEM and album is not None:
                # From album.
                value = getattr(album, key)
            else:
                # From Item.
                value = getattr(item, key)
            mapping[key] = util.sanitize_for_path(value, pathmod, key)

        # Use the album artist if the track artist is not set and
        # vice-versa.
        if not mapping["artist"]:
            mapping["artist"] = mapping["albumartist"]
        if not mapping["albumartist"]:
            mapping["albumartist"] = mapping["artist"]

        # Get values from plugins.
        for key, value in plugins.template_values(item).iteritems():
            mapping[key] = util.sanitize_for_path(value, pathmod, key)

        # Perform substitution.
        funcs = DefaultTemplateFunctions(self, item).functions()
        funcs.update(plugins.template_funcs())
        subpath = subpath_tmpl.substitute(mapping, funcs)

        # Encode for the filesystem, dropping unencodable characters.
        if isinstance(subpath, unicode) and not fragment:
            encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
            subpath = subpath.encode(encoding, "replace")

        # Truncate components and remove forbidden characters.
        subpath = util.sanitize_path(subpath, pathmod, self.replacements)

        # Preserve extension.
        _, extension = pathmod.splitext(item.path)
        subpath += extension.lower()

        if fragment:
            return subpath
        else:
            basedir = basedir or self.directory
            return normpath(os.path.join(basedir, subpath))
Beispiel #3
0
    def destination(self, item, pathmod=None, in_album=False,
                    fragment=False, basedir=None):
        """Returns the path in the library directory designated for item
        item (i.e., where the file ought to be). in_album forces the
        item to be treated as part of an album. fragment makes this
        method return just the path fragment underneath the root library
        directory; the path is also returned as Unicode instead of
        encoded as a bytestring. basedir can override the library's base
        directory for the destination.
        """
        pathmod = pathmod or os.path

        # Use a path format based on a query, falling back on the
        # default.
        for query, path_format in self.path_formats:
            if query == PF_KEY_DEFAULT:
                continue
            query = AndQuery.from_string(query)
            if in_album:
                # If we're treating this item as a member of the item,
                # hack the query so that singleton queries always
                # observe the item to be non-singleton.
                for i, subquery in enumerate(query):
                    if isinstance(subquery, SingletonQuery):
                        query[i] = FalseQuery() if subquery.sense \
                                   else TrueQuery()
            if query.match(item):
                # The query matches the item! Use the corresponding path
                # format.
                break
        else:
            # No query matched; fall back to default.
            for query, path_format in self.path_formats:
                if query == PF_KEY_DEFAULT:
                    break
            else:
                assert False, "no default path format"
        subpath_tmpl = Template(path_format)

        # Get the item's Album if it has one.
        album = self.get_album(item)

        # Build the mapping for substitution in the path template,
        # beginning with the values from the database.
        mapping = {}
        for key in ITEM_KEYS_META:
            # Get the values from either the item or its album.
            if key in ALBUM_KEYS_ITEM and album is not None:
                # From album.
                value = getattr(album, key)
            else:
                # From Item.
                value = getattr(item, key)
            mapping[key] = util.sanitize_for_path(value, pathmod, key)

        # Use the album artist if the track artist is not set and
        # vice-versa.
        if not mapping['artist']:
            mapping['artist'] = mapping['albumartist']
        if not mapping['albumartist']:
            mapping['albumartist'] = mapping['artist']

        # Get values from plugins.
        for key, value in plugins.template_values(item).iteritems():
            mapping[key] = util.sanitize_for_path(value, pathmod, key)

        # Perform substitution.
        funcs = DefaultTemplateFunctions(self, item).functions()
        funcs.update(plugins.template_funcs())
        subpath = subpath_tmpl.substitute(mapping, funcs)

        # Encode for the filesystem, dropping unencodable characters.
        if isinstance(subpath, unicode) and not fragment:
            encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
            subpath = subpath.encode(encoding, 'replace')

        # Truncate components and remove forbidden characters.
        subpath = util.sanitize_path(subpath, pathmod, self.replacements)

        # Preserve extension.
        _, extension = pathmod.splitext(item.path)
        subpath += extension.lower()

        if fragment:
            return subpath
        else:
            basedir = basedir or self.directory
            return normpath(os.path.join(basedir, subpath))