-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_reader.py
62 lines (46 loc) · 1.95 KB
/
config_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from configparser import ConfigParser
from pathlib import Path
# TODO: improve tests, then re-architectre this to not re-instantiate ConfigParser
# and abstract out saving from set_refresh_tokej
def read_config_file():
path_of_this_dir, _ = os.path.split(os.path.realpath(__file__))
config_file_name = "config.ini"
config_file_path = os.path.join(path_of_this_dir, config_file_name)
config_parser = ConfigParser()
num_read_files = config_parser.read(config_file_path)
assert len(num_read_files) > 0, f'could not read {config_file_path}'
return config_parser
CONFIG = read_config_file()
def get_read_errors_out_loud():
read_erors_out_loud = CONFIG["OTHER"]["READ_ERRORS_OUT_LOUD"]
return (read_erors_out_loud == "True" or read_erors_out_loud == "true" or read_erors_out_loud == True)
def get_spotify_client_id():
client_id = CONFIG["SPOTIFY"]["CLIENT_ID"]
validate_id_or_secret(client_id)
return client_id
def get_spotify_client_secret():
client_secret = CONFIG["SPOTIFY"]["CLIENT_SECRET"]
validate_id_or_secret(client_secret)
return client_secret
def get_spotify_playable_uri():
spotify_uri = CONFIG["SPOTIFY"]["PLAYABLE_URI"]
return spotify_uri
def get_spotify_device_id():
device_id = CONFIG["SPOTIFY"]["DEVICE_ID"]
return device_id
def get_spotify_refresh_token():
token = CONFIG["SPOTIFY"]["REFRESH_TOKEN"]
# assert len(token) > 60
return token
def set_spotify_refresh_token(token):
path_of_this_dir, _ = os.path.split(os.path.realpath(__file__))
config_file_name = "config.ini"
config_file_path = os.path.join(path_of_this_dir, config_file_name)
config_parser = ConfigParser()
config_parser.read(config_file_name)
config_parser["SPOTIFY"]["REFRESH_TOKEN"] = token
with open(config_file_name, 'w') as configfile:
config_parser.write(configfile)
def validate_id_or_secret(value):
assert len(value) == 32