From f82d466b7748d36f6cab4bd129c9ce3d8cc5eebe Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 17 Sep 2013 08:44:26 -0700 Subject: [PATCH] eliminate lib argument to `Item.evaluate_template` --- beets/library.py | 23 ++++++++++++++--------- beets/ui/__init__.py | 5 +---- beetsplug/smartplaylist.py | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/beets/library.py b/beets/library.py index d98583262..ac4de7a36 100644 --- a/beets/library.py +++ b/beets/library.py @@ -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': diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index f7288141f..cdc29cc28 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -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.""" diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index 164029a41..a8f22ddbe 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -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] = []