music-assistant-server

1.2 KBPY
descriptor.py
1.2 KB46 lines • python
1"""Configuration descriptor implementation for Nicovideo provider."""
2
3from __future__ import annotations
4
5from collections.abc import Callable
6from typing import TYPE_CHECKING, Protocol
7
8if TYPE_CHECKING:
9    from music_assistant_models.config_entries import ConfigEntry, ConfigValueType
10
11
12class ConfigReader(Protocol):
13    """Protocol for configuration readers."""
14
15    def get_value(self, key: str) -> ConfigValueType:
16        """Retrieve a configuration value by key."""
17        ...
18
19
20class ConfigDescriptor[T]:
21    """Typed config descriptor with embedded ConfigEntry."""
22
23    def __init__(
24        self,
25        cast: Callable[[ConfigValueType], T],
26        config_entry: ConfigEntry,
27    ) -> None:
28        """Initialize descriptor.
29
30        Args:
31            cast: Transformation/validation applied to raw value.
32            config_entry: ConfigEntry definition for this option.
33        """
34        self.cast = cast
35        self.config_entry = config_entry
36
37    @property
38    def key(self) -> str:
39        """Get the config key from the embedded ConfigEntry."""
40        return self.config_entry.key
41
42    def __get__(self, instance: ConfigReader, owner: type) -> T:
43        """Descriptor access."""
44        raw = instance.get_value(self.key)
45        return self.cast(raw)
46