Configuration
Importing and exporting application credentials.
Read configuration from environment variables. |
|
Read configuration from a config file. |
|
Write configuration to a config file. |
|
Missing value read from configuration. |
|
Configuration variable name for a client ID. |
|
Configuration variable name for a client secret. |
|
Configuration variable name for a redirect URI. |
|
Configuration variable name for a user refresh token. |
Environment variables and configuration files can be used to provide application and user credentials. See also Options.
- tekore.config_from_environment(return_refresh=False)
Read configuration from environment variables.
- Parameters:
return_refresh (bool) – return user refresh token
- Returns:
(client ID, client secret, redirect URI), None if not found. If return_refresh is True, also return user refresh token.
- Return type:
tuple
- Raises:
MissingConfigurationWarning – when a missing value is encountered
Examples
client_id, client_secret, redirect_uri = tk.config_from_environment() conf = tk.config_from_environment(return_refresh=True) client_id, client_secret, redirect_uri, user_refresh = conf
- tekore.config_from_file(file_path, section='DEFAULT', return_refresh=False)
Read configuration from a config file.
The configuration must be in INI format as accepted by
configparser.ConfigParser
.- Parameters:
file_path (str) – path of the file containing the credential variables
section (str) – name of the section to read variables from
return_refresh (bool) – return user refresh token
- Returns:
(client ID, client secret, redirect URI), None if not found. If return_refresh is True, also return user refresh token.
- Return type:
tuple
- Raises:
MissingConfigurationWarning – when a missing value is encountered
Examples
client_id, client_secret, redirect_uri = tk.config_from_file(filename) conf = tk.config_from_file(filename, return_refresh=True) client_id, client_secret, redirect_uri, user_refresh = conf
- tekore.config_to_file(file_path, values, section='DEFAULT')
Write configuration to a config file.
Existing configuration is preserved if it’s not in conflict.
- Parameters:
file_path (str) – path of the configuration file
values (Iterable | dict) – configuration values to write, dict or iterable, see below for examples
section (str) – name of the section to write to
- Return type:
None
Examples
Configuration can be written in different ways. Pass in an iterable to use the preset variable names. The values should be ordered as returned when reading configuration:
client_id, client_secret, redirect_uri, user_refresh
.conf = (client_id, client_secret, redirect_uri, user_refresh) config_to_file(filename, conf)
A shorter iterable or one containing
None
values may be passed. Items missing from the end are ignored andNone
values are discarded.# Write partial information config_to_file(filename, (client_id, client_secret)) # Fill the missing configuration conf = (None, None, redirect_uri, user_refresh) config_to_file(filename, conf)
A dictionary is also accepted. In this case the keys are used instead of preset variable names.
config_to_file(filename, {'REFRESH_TOKEN': refresh_token})
- class tekore.MissingConfigurationWarning
Bases:
RuntimeWarning
Missing value read from configuration.
Options
Configuration values are read from and written to preset names. Those names can be changed to your liking.
import tekore as tk
tk.client_id_var = 'your_client_id_var'
tk.client_secret_var = 'your_client_secret_var'
tk.redirect_uri_var = 'your_redirect_uri_var'
tk.user_refresh_var = 'your_user_refresh_var'
Note
Changing values requires importing Tekore as a module as above.
- tekore.client_id_var: str = 'SPOTIFY_CLIENT_ID'
Configuration variable name for a client ID.
- tekore.client_secret_var: str = 'SPOTIFY_CLIENT_SECRET'
Configuration variable name for a client secret.
- tekore.redirect_uri_var: str = 'SPOTIFY_REDIRECT_URI'
Configuration variable name for a redirect URI.
- tekore.user_refresh_var: str = 'SPOTIFY_USER_REFRESH'
Configuration variable name for a user refresh token.