eliminate lib argument to Item.evaluate_template

This commit is contained in:
Adrian Sampson 2013-09-17 08:44:26 -07:00
parent 22d5e564c6
commit f82d466b77
3 changed files with 16 additions and 14 deletions

View file

@ -473,6 +473,13 @@ class Item(LibModel):
if self.mtime == 0 and 'mtime' in values:
self.mtime = values['mtime']
def get_album(self):
"""Get the Album object that this item belongs to, if any, or
None if the item is a singleton.
"""
self._check_db()
return self._lib.get_album(self.id)
# Interaction with file metadata.
@ -568,7 +575,7 @@ class Item(LibModel):
# Remove the album if it is empty.
if with_album:
album = self._lib.get_album(self)
album = self.get_album()
if album and not album.items():
album.remove(delete, False)
@ -612,7 +619,7 @@ class Item(LibModel):
# If this item is in an album, move its art.
if with_album:
album = self._lib.get_album(self)
album = self.get_album()
if album:
album.move_art(copy)
album.store()
@ -624,18 +631,16 @@ class Item(LibModel):
# Templating.
def evaluate_template(self, template, lib=None, sanitize=False,
def evaluate_template(self, template, sanitize=False,
pathmod=None):
"""Evaluates a Template object using the item's fields. If `lib`
is provided, it is used to map some fields to the item's album
(if available) and is made available to template functions. If
"""Evaluates a Template object using the item's fields. If
`sanitize`, then each value will be sanitized for inclusion in a
file path.
"""
pathmod = pathmod or os.path
# Get the item's Album if it has one.
album = lib.get_album(self)
album = self.get_album()
# Build the mapping for substitution in the template,
# beginning with the values from the database.
@ -683,7 +688,7 @@ class Item(LibModel):
mapping[key] = value
# Get template functions.
funcs = DefaultTemplateFunctions(self, lib, pathmod).functions()
funcs = DefaultTemplateFunctions(self, self._lib, pathmod).functions()
funcs.update(plugins.template_funcs())
# Perform substitution.
@ -1668,7 +1673,7 @@ class Library(object):
subpath_tmpl = Template(path_format)
# Evaluate the selected template.
subpath = item.evaluate_template(subpath_tmpl, self, True, pathmod)
subpath = item.evaluate_template(subpath_tmpl, True, pathmod)
# Prepare path for output: normalize Unicode characters.
if platform == 'darwin':

View file

@ -507,10 +507,7 @@ def print_obj(obj, lib, fmt=None):
template = fmt
else:
template = Template(fmt)
if album:
print_(obj.evaluate_template(template))
else:
print_(obj.evaluate_template(template, lib=lib))
print_(obj.evaluate_template(template))
def term_width():
"""Get the width (columns) of the terminal."""

View file

@ -42,7 +42,7 @@ def update_playlists(lib):
# As we allow tags in the m3u names, we'll need to iterate through
# the items and generate the correct m3u file names.
for item in items:
m3u_name = item.evaluate_template(Template(basename), lib=lib,
m3u_name = item.evaluate_template(Template(basename),
sanitize=True)
if not (m3u_name in m3us):
m3us[m3u_name] = []