Add method apply_config to sources

This commit is contained in:
Christoph Stahl 2025-02-02 23:47:04 +01:00
parent de74d9ecd7
commit 0df93ac758
5 changed files with 20 additions and 23 deletions

View file

@ -35,10 +35,7 @@ class FileBasedSource(Source):
"build_index": ConfigOption(BoolOption(), "Build index on startup", True),
}
def __init__(self, config: dict[str, Any]):
"""Initialize the file module."""
super().__init__(config)
def apply_config(self, config: dict[str, Any]) -> None:
self.extensions: list[str] = config["extensions"] if "extensions" in config else ["mp3+cdg"]
self.extra_mpv_options = {"scale": "oversample"}

View file

@ -25,10 +25,8 @@ class FilesSource(FileBasedSource):
# "index_file": ("file", "Index file", os.path.join(user_cache_dir("syng"), "files-index")),
}
def __init__(self, config: dict[str, Any]):
"""Initialize the file module."""
super().__init__(config)
def apply_config(self, config: dict[str, Any]) -> None:
super().apply_config(config)
self.dir = config["dir"] if "dir" in config else "."
async def get_file_list(self) -> list[str]:

View file

@ -57,10 +57,8 @@ class S3Source(FileBasedSource):
),
}
def __init__(self, config: dict[str, Any]):
"""Create the source."""
super().__init__(config)
def apply_config(self, config: dict[str, Any]) -> None:
super().apply_config(config)
if (
MINIO_AVAILABE
and "endpoint" in config

View file

@ -22,7 +22,6 @@ from typing import Type
from abc import ABC, abstractmethod
from ..log import logger
from ..entry import Entry
from ..result import Result
@ -130,6 +129,7 @@ class Source(ABC):
self.extra_mpv_options: dict[str, str] = {}
self._skip_next = False
self.build_index = config.get("build_index", False)
self.apply_config(config)
async def get_entry(
self,
@ -437,5 +437,18 @@ class Source(ABC):
self._index = []
self._index += config["index"]
@abstractmethod
def apply_config(self, config: dict[str, Any]) -> None:
"""
Apply the a config to the source.
This should be implemented by each source individually.
:param config: The part of the config to apply.
:type config: dict[str, Any]
:rtype: None
"""
pass
available_sources: dict[str, Type[Source]] = {}

View file

@ -193,16 +193,7 @@ class YoutubeSource(Source):
),
}
# pylint: disable=too-many-instance-attributes
def __init__(self, config: dict[str, Any]):
"""
Create the YouTube source.
:param config: The configuration for the source.
:type config: dict[str, Any]
"""
super().__init__(config)
def apply_config(self, config: dict[str, Any]) -> None:
self.channels: list[str] = config["channels"] if "channels" in config else []
self.tmp_dir: str = config["tmp_dir"] if "tmp_dir" in config else "/tmp/syng"
try: