print a better error for exceptions raised from inline snippets

This commit is contained in:
Adrian Sampson 2012-01-29 13:27:47 -08:00
parent fe33926038
commit 7b265e353d
2 changed files with 17 additions and 1 deletions

View file

@ -18,9 +18,19 @@ import logging
import traceback
from beets.plugins import BeetsPlugin
from beets import ui
log = logging.getLogger('beets')
class InlineError(Exception):
"""Raised when a runtime error occurs in an inline expression.
"""
def __init__(self, expr, exc):
super(InlineError, self).__init__(
(u"error in inline path field expression:\n" \
u"%s\n%s: %s") % (expr, type(exc).__name__, unicode(exc))
)
def compile_expr(expr):
"""Given a Python expression, compile it as a path field function.
The returned function takes a single argument, an Item, and returns
@ -35,7 +45,11 @@ def compile_expr(expr):
return None
def field_func(item):
return eval(code, dict(item.record))
values = dict(item.record)
try:
return eval(code, values)
except Exception, exc:
raise InlineError(expr, exc)
return field_func
class InlinePlugin(BeetsPlugin):

View file

@ -15,6 +15,8 @@ Changelog
instead of 24.
* The autotagger now also tolerates tracks whose track artists tags are set
to "Various Artists".
* The ``inline`` plugin now prints a more comprehensible error when exceptions
occur in Python snippets.
* Fix a bug in the ``rewrite`` plugin that broke the use of multiple rules for
a single field.
* Fix a crash with non-ASCII characters in bytestring metadata fields (e.g.,