respect XDG_CONFIG_HOME environment variable (#312)

This fixes two bugs:

1. When XDG_CONFIG_HOME was set the scdl.cfg file was created at
   XDG_CONFIG_HOME/scdl.cfg not XDG_CONFIG_HOME/scdl/scdl.cfg
2. get_config() did not check for the XDG_CONFIG_HOME environment
   variable and read from ~/.config/scdl/scdl.cfg instead
master
Charlie Vieth 2020-03-30 13:45:34 -04:00 committed by GitHub
parent 71a8867403
commit 41d4503722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 14 deletions

View File

@ -9,19 +9,21 @@ CLIENT_ID = 'a3e059563d7fd3372b49b37f00a00bcf'
ALT_CLIENT_ID = '2t9loNQH90kzJcsFCODdigxfp325aq4z'
ALT2_CLIENT_ID = 'NONE'
dir_path_to_conf = os.path.join(os.path.expanduser('~'), '.config/scdl')
if 'XDG_CONFIG_HOME' in os.environ:
dir_path_to_conf = os.environ['XDG_CONFIG_HOME']
file_path_to_conf = os.path.join(dir_path_to_conf, 'scdl.cfg')
text = """[scdl]
default_config = """[scdl]
auth_token =
path = .
"""
if not os.path.exists(dir_path_to_conf):
os.makedirs(dir_path_to_conf)
if 'XDG_CONFIG_HOME' in os.environ:
config_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'scdl')
else:
config_dir = os.path.join(os.path.expanduser('~'), '.config', 'scdl')
if not os.path.exists(file_path_to_conf):
with open(file_path_to_conf, 'w') as f:
f.write(text)
config_file = os.path.join(config_dir, 'scdl.cfg')
if not os.path.exists(config_file):
if not os.path.exists(config_dir):
os.makedirs(config_dir)
with open(config_file, 'w') as f:
f.write(default_config)

View File

@ -204,7 +204,16 @@ def get_config():
"""
global token
config = configparser.ConfigParser()
config.read(os.path.join(os.path.expanduser('~'), '.config/scdl/scdl.cfg'), "utf8")
if 'XDG_CONFIG_HOME' in os.environ:
config_file = os.path.join(
os.environ['XDG_CONFIG_HOME'], 'scdl', 'scdl.cfg',
)
else:
config_file = os.path.join(
os.path.expanduser('~'), '.config', 'scdl', 'scdl.cfg',
)
config.read(config_file, 'utf8')
try:
token = config['scdl']['auth_token']
path = config['scdl']['path']
@ -454,7 +463,7 @@ def download_original_file(track, title):
if r.status_code == 401:
logger.info('The original file has no download left.')
return None
if r.status_code == 404:
logger.info('Could not get name from stream - using basic name')
return None
@ -494,7 +503,7 @@ def download_original_file(track, title):
newfilename = filename[:-4] + ".flac"
new = shlex.quote(newfilename)
old = shlex.quote(filename)
commands = ['ffmpeg', '-i', old, new, '-loglevel', 'fatal']
logger.debug("Commands: {}".format(commands))
subprocess.call(commands)