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.

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
194
195
196
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
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
161
162
163
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
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
82
83
84
85
86
87
88
89
90
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
71
72
73
74
75
76
77
78
79
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
 93
 94
 95
 96
 97
 98
 99
100
101
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
104
105
106
107
108
109
110
111
112
113
114
115
116
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