Skip to content

config

Module related to managing reading and writing to the config file.

Default config - spotdl.utils.config.DEFAULT_CONFIG

ConfigError ¤

Bases: Exception

Base class for all exceptions related to config.

GlobalConfig ¤

Class to store global configuration

get_parameter(key) classmethod ¤

Get a parameter from the download config

Source code in spotdl/utils/config.py
266
267
268
269
270
271
272
@classmethod
def get_parameter(cls, key):
    """
    Get a parameter from the download config
    """

    return cls.parameters.get(key, None)

set_parameter(key, value) classmethod ¤

Set a parameter for the download config

Source code in spotdl/utils/config.py
258
259
260
261
262
263
264
@classmethod
def set_parameter(cls, key, value):
    """
    Set a parameter for the download config
    """

    cls.parameters[key] = value

create_settings(arguments) ¤

Create settings dicts for Spotify, Downloader and Web based on the arguments and config file (if enabled)

Arguments¤
  • arguments: Namespace from argparse
Returns¤
  • spotify_options: SpotifyOptions
  • downloader_options: DownloaderOptions
  • web_options: WebOptions
Source code in spotdl/utils/config.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def create_settings(
    arguments: Namespace,
) -> Tuple[SpotifyOptions, DownloaderOptions, WebOptions]:
    """
    Create settings dicts for Spotify, Downloader and Web
    based on the arguments and config file (if enabled)

    ### Arguments
    - arguments: Namespace from argparse

    ### Returns
    - spotify_options: SpotifyOptions
    - downloader_options: DownloaderOptions
    - web_options: WebOptions
    """

    # Get the config file
    # It will automatically load if the `load_config` is set to True
    # in the config file
    config = {}
    if arguments.config or (
        get_config_file().exists() and get_config().get("load_config")
    ):
        config = get_config()

    # Type: ignore because of the issues below
    # https://github.com/python/mypy/issues/8890
    # https://github.com/python/mypy/issues/5382
    spotify_options = SpotifyOptions(
        **create_settings_type(arguments, config, SPOTIFY_OPTIONS)  # type: ignore
    )
    downloader_options = DownloaderOptions(
        **create_settings_type(arguments, config, DOWNLOADER_OPTIONS)  # type: ignore
    )
    web_options = WebOptions(**create_settings_type(arguments, config, WEB_OPTIONS))  # type: ignore

    return spotify_options, downloader_options, web_options

create_settings_type(arguments, config, default) ¤

Create settings dict Argument value has always the priority, then the config file value, and if neither are set, use default value

Arguments¤
  • arguments: Namespace from argparse
  • default: dict
Returns¤
  • settings: dict
Source code in spotdl/utils/config.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def create_settings_type(
    arguments: Namespace,
    config: Dict[str, Any],
    default: Union[SpotifyOptions, DownloaderOptions, WebOptions],
) -> Dict[str, Any]:
    """
    Create settings dict
    Argument value has always the priority, then the config file
    value, and if neither are set, use default value

    ### Arguments
    - arguments: Namespace from argparse
    - default: dict

    ### Returns
    - settings: dict
    """

    settings = {}
    for key, default_value in default.items():
        argument_val = arguments.__dict__.get(key)
        config_val = config.get(key)

        if argument_val is not None:
            settings[key] = argument_val
        elif config_val is not None:
            settings[key] = config_val
        else:
            settings[key] = default_value

    return settings

get_cache_path() ¤

Get the path to the cache folder.

Returns¤
  • The path to the spotipy cache file.
Source code in spotdl/utils/config.py
85
86
87
88
89
90
91
92
93
def get_cache_path() -> Path:
    """
    Get the path to the cache folder.

    ### Returns
    - The path to the spotipy cache file.
    """

    return get_spotdl_path() / ".spotipy"

get_config() ¤

Get the config.

Returns¤
  • The dictionary with the config.
Errors¤
  • ConfigError: If the config file does not exist.
Source code in spotdl/utils/config.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def get_config() -> Dict[str, Any]:
    """
    Get the config.

    ### Returns
    - The dictionary with the config.

    ### Errors
    - ConfigError: If the config file does not exist.
    """

    config_path = get_config_file()

    if not config_path.exists():
        raise ConfigError(
            "Config file not found."
            "Please run `spotdl --generate-config` to create a config file."
        )

    with open(config_path, "r", encoding="utf-8") as config_file:
        return json.load(config_file)

get_config_file() ¤

Get config file path

Returns¤
  • The path to the config file.
Source code in spotdl/utils/config.py
74
75
76
77
78
79
80
81
82
def get_config_file() -> Path:
    """
    Get config file path

    ### Returns
    - The path to the config file.
    """

    return get_spotdl_path() / "config.json"

get_errors_path() ¤

Get the path to the errors folder.

Returns¤
  • The path to the errors folder.
Notes¤
  • If the errors directory does not exist, it will be created.
Source code in spotdl/utils/config.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def get_errors_path() -> Path:
    """
    Get the path to the errors folder.

    ### Returns
    - The path to the errors folder.

    ### Notes
    - If the errors directory does not exist, it will be created.
    """

    errors_path = get_spotdl_path() / "errors"

    if not errors_path.exists():
        os.mkdir(errors_path)

    return errors_path

get_spotdl_path() ¤

Get the path to the spotdl folder.

Returns¤
  • The path to the spotdl folder.
Notes¤
  • If the spotdl directory does not exist, it will be created.
Source code in spotdl/utils/config.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def get_spotdl_path() -> Path:
    """
    Get the path to the spotdl folder.

    ### Returns
    - The path to the spotdl folder.

    ### Notes
    - If the spotdl directory does not exist, it will be created.
    """

    # Check if os is linux
    if platform.system() == "Linux":
        # if platform is linux, and XDG DATA HOME spotdl folder exists, use it
        user_data_dir = Path(platformdirs.user_data_dir("spotdl", "spotDL"))
        if user_data_dir.exists():
            return user_data_dir

    spotdl_path = Path(os.path.expanduser("~"), ".spotdl")
    if not spotdl_path.exists():
        os.mkdir(spotdl_path)

    return spotdl_path

get_spotify_cache_path() ¤

Get the path to the spotify cache folder.

Returns¤
  • The path to the spotipy cache file.
Source code in spotdl/utils/config.py
 96
 97
 98
 99
100
101
102
103
104
def get_spotify_cache_path() -> Path:
    """
    Get the path to the spotify cache folder.

    ### Returns
    - The path to the spotipy cache file.
    """

    return get_spotdl_path() / ".spotify_cache"

get_temp_path() ¤

Get the path to the temp folder.

Returns¤
  • The path to the temp folder.
Source code in spotdl/utils/config.py
107
108
109
110
111
112
113
114
115
116
117
118
119
def get_temp_path() -> Path:
    """
    Get the path to the temp folder.

    ### Returns
    - The path to the temp folder.
    """

    temp_path = get_spotdl_path() / "temp"
    if not temp_path.exists():
        os.mkdir(temp_path)

    return temp_path

modernize_settings(options) ¤

Handle deprecated values in config file.

Arguments¤
  • options: DownloaderOptions to modernize
Source code in spotdl/utils/config.py
236
237
238
239
240
241
242
243
244
245
246
247
248
def modernize_settings(options: DownloaderOptions):
    """Handle deprecated values in config file.

    ### Arguments
    - options: DownloaderOptions to modernize
    """

    warning_msg = "Deprecated '%s' value found for '%s' setting in config file. Using '%s' instead."

    # Respect backward compatibility with old boolean --restrict flag
    if options["restrict"] is True:
        logger.warning(warning_msg, True, "restrict", "strict")
        options["restrict"] = "strict"