File: inline.py

package info (click to toggle)
beets 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,016 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (129 lines) | stat: -rw-r--r-- 4,349 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Allows inline path template customization code in the config file."""

import itertools
import traceback

from beets import config
from beets.plugins import BeetsPlugin

FUNC_NAME = "__INLINE_FUNC__"


class InlineError(Exception):
    """Raised when a runtime error occurs in an inline expression."""

    def __init__(self, code, exc):
        super().__init__(
            f"error in inline path field code:\n{code}\n{type(exc).__name__}: {exc}"
        )


def _compile_func(body):
    """Given Python code for a function body, return a compiled
    callable that invokes that code.
    """
    body = body.replace("\n", "\n    ")
    body = f"def {FUNC_NAME}():\n    {body}"
    code = compile(body, "inline", "exec")
    env = {}
    eval(code, env)
    return env[FUNC_NAME]


class InlinePlugin(BeetsPlugin):
    def __init__(self):
        super().__init__()

        config.add(
            {
                "pathfields": {},  # Legacy name.
                "item_fields": {},
                "album_fields": {},
            }
        )

        # Item fields.
        for key, view in itertools.chain(
            config["item_fields"].items(), config["pathfields"].items()
        ):
            self._log.debug("adding item field {}", key)
            func = self.compile_inline(view.as_str(), False)
            if func is not None:
                self.template_fields[key] = func

        # Album fields.
        for key, view in config["album_fields"].items():
            self._log.debug("adding album field {}", key)
            func = self.compile_inline(view.as_str(), True)
            if func is not None:
                self.album_template_fields[key] = func

    def compile_inline(self, python_code, album):
        """Given a Python expression or function body, compile it as a path
        field function. The returned function takes a single argument, an
        Item, and returns a Unicode string. If the expression cannot be
        compiled, then an error is logged and this function returns None.
        """
        # First, try compiling as a single function.
        try:
            code = compile(f"({python_code})", "inline", "eval")
        except SyntaxError:
            # Fall back to a function body.
            try:
                func = _compile_func(python_code)
            except SyntaxError:
                self._log.error(
                    "syntax error in inline field definition:\n{}",
                    traceback.format_exc(),
                )
                return
            else:
                is_expr = False
        else:
            is_expr = True

        def _dict_for(obj):
            out = dict(obj)
            if album:
                out["items"] = list(obj.items())
            return out

        if is_expr:
            # For expressions, just evaluate and return the result.
            def _expr_func(obj):
                values = _dict_for(obj)
                try:
                    return eval(code, values)
                except Exception as exc:
                    raise InlineError(python_code, exc)

            return _expr_func
        else:
            # For function bodies, invoke the function with values as global
            # variables.
            def _func_func(obj):
                old_globals = dict(func.__globals__)
                func.__globals__.update(_dict_for(obj))
                try:
                    return func()
                except Exception as exc:
                    raise InlineError(python_code, exc)
                finally:
                    func.__globals__.clear()
                    func.__globals__.update(old_globals)

            return _func_func