Revert "Remove class variables for template fields and funcs"

This reverts commit a7033fe63b.
This commit is contained in:
asardaes 2025-11-20 21:54:25 +01:00 committed by Alexis Sardá
parent 23a19e9409
commit be0b71043c

View file

@ -151,10 +151,9 @@ class BeetsPlugin(metaclass=abc.ABCMeta):
list list
) )
listeners: ClassVar[dict[EventType, list[Listener]]] = defaultdict(list) listeners: ClassVar[dict[EventType, list[Listener]]] = defaultdict(list)
template_funcs: ClassVar[TFuncMap[str]] = {}
template_funcs: TFuncMap[str] template_fields: ClassVar[TFuncMap[Item]] = {}
template_fields: TFuncMap[Item] album_template_fields: ClassVar[TFuncMap[Album]] = {}
album_template_fields: TFuncMap[Album]
name: str name: str
config: ConfigView config: ConfigView
@ -220,9 +219,14 @@ class BeetsPlugin(metaclass=abc.ABCMeta):
self.name = name or self.__module__.split(".")[-1] self.name = name or self.__module__.split(".")[-1]
self.config = beets.config[self.name] self.config = beets.config[self.name]
self.template_funcs = {} # Set class attributes if they are not already set
self.template_fields = {} # for the type of plugin.
self.album_template_fields = {} if not self.template_funcs:
self.template_funcs = {} # type: ignore[misc]
if not self.template_fields:
self.template_fields = {} # type: ignore[misc]
if not self.album_template_fields:
self.album_template_fields = {} # type: ignore[misc]
self.early_import_stages = [] self.early_import_stages = []
self.import_stages = [] self.import_stages = []
@ -356,6 +360,33 @@ class BeetsPlugin(metaclass=abc.ABCMeta):
self._set_log_level_and_params(logging.WARNING, func) self._set_log_level_and_params(logging.WARNING, func)
) )
@classmethod
def template_func(cls, name: str) -> Callable[[TFunc[str]], TFunc[str]]:
"""Decorator that registers a path template function. The
function will be invoked as ``%name{}`` from path format
strings.
"""
def helper(func: TFunc[str]) -> TFunc[str]:
cls.template_funcs[name] = func
return func
return helper
@classmethod
def template_field(cls, name: str) -> Callable[[TFunc[Item]], TFunc[Item]]:
"""Decorator that registers a path template field computation.
The value will be referenced as ``$name`` from path format
strings. The function must accept a single parameter, the Item
being formatted.
"""
def helper(func: TFunc[Item]) -> TFunc[Item]:
cls.template_fields[name] = func
return func
return helper
def get_plugin_names() -> list[str]: def get_plugin_names() -> list[str]:
"""Discover and return the set of plugin names to be loaded. """Discover and return the set of plugin names to be loaded.