Exemple #1
0
 def _template_funcs(self):
     funcs = DefaultTemplateFunctions(self, self._db).functions()
     funcs.update(plugins.template_funcs())
     return funcs
Exemple #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']
     
     # Perform substitution.
     mapping.update(plugins.template_values(item))
     funcs = dict(TEMPLATE_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
     
     if fragment:
         return subpath
     else:
         basedir = basedir or self.directory
         return normpath(os.path.join(basedir, subpath))   
Exemple #3
0
 def _template_funcs(self):
     funcs = DefaultTemplateFunctions(self, self._db).functions()
     funcs.update(plugins.template_funcs())
     return funcs
Exemple #4
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']
     
     # Perform substitution.
     mapping.update(plugins.template_values(item))
     funcs = dict(TEMPLATE_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
     
     if fragment:
         return subpath
     else:
         basedir = basedir or self.directory
         return normpath(os.path.join(basedir, subpath))