From da0642a026838b93eb5151338cf1ea6bbe7f0be5 Mon Sep 17 00:00:00 2001 From: Florent Thoumie Date: Sat, 5 Jan 2013 23:00:00 +0000 Subject: [PATCH] inline: Add support for code blocks. This should be backwards compatible. In case the the path field isn't a statement, beets will assume it's a block of code that stores the value in a special '_' variable. --- beetsplug/inline.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/beetsplug/inline.py b/beetsplug/inline.py index 5a64184ba..a585c76fa 100644 --- a/beetsplug/inline.py +++ b/beetsplug/inline.py @@ -37,17 +37,33 @@ def compile_expr(expr): a Unicode string. If the expression cannot be compiled, then an error is logged and this function returns None. """ + code = None + try: code = compile(u'(%s)' % expr, 'inline', 'eval') except SyntaxError: - log.error(u'syntax error in field expression:\n%s' % - traceback.format_exc()) + pass + + if code == None: + try: + code = compile("""%s""" % expr, 'inline', 'exec') + except SyntaxError: + log.error(u'syntax error in field expression:\n%s' % + traceback.format_exc()) + + if code == None: return None def field_func(item): values = dict(item.record) try: - return eval(code, values) + ret = eval(code, values) + if ret == None: + ret = values.get('_', None) + if ret == None: + raise Exception('Expression must be a statement or a block of' \ + ' code storing the result in the "_" variable.') + return ret except Exception as exc: raise InlineError(expr, exc) return field_func