From ea8ca4cfa40a998e33f1d85288314fe824c669e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 26 Oct 2017 00:04:28 +0200 Subject: [PATCH] add 'util.expand_path()' --- gallery_dl/cache.py | 4 ++-- gallery_dl/config.py | 2 +- gallery_dl/util.py | 9 ++++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/gallery_dl/cache.py b/gallery_dl/cache.py index 742dbea3..19b775ef 100644 --- a/gallery_dl/cache.py +++ b/gallery_dl/cache.py @@ -14,7 +14,7 @@ import time import tempfile import os.path import functools -from . import config +from . import config, util class CacheInvalidError(Exception): @@ -111,7 +111,7 @@ class DatabaseCache(CacheModule): path = config.get(("cache", "file"), path_default) if path is None: raise RuntimeError() - path = os.path.expanduser(os.path.expandvars(path)) + path = util.expand_path(path) self.db = sqlite3.connect(path, timeout=30, check_same_thread=False) self.db.execute( "CREATE TABLE IF NOT EXISTS data (" diff --git a/gallery_dl/config.py b/gallery_dl/config.py index 1f92eda3..6066078c 100644 --- a/gallery_dl/config.py +++ b/gallery_dl/config.py @@ -55,7 +55,7 @@ def load(*files, format="json", strict=False): for conf in configfiles: try: - path = os.path.expanduser(os.path.expandvars(conf)) + path = util.expand_path(conf) with open(path) as file: confdict = parsefunc(file) if not _config: diff --git a/gallery_dl/util.py b/gallery_dl/util.py index cb016f3b..980f4c53 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -104,6 +104,13 @@ def safe_int(value, default=0): return default +def expand_path(path): + """Expand environment variables and tildes (~)""" + if not path: + return path + return os.path.expandvars(os.path.expanduser(path)) + + def code_to_language(code, default=None): """Map an ISO 639-1 language code to its actual name""" return CODES.get((code or "").lower(), default) @@ -325,7 +332,7 @@ class PathFormat(): bdir = extractor.config("base-directory", (".", "gallery-dl")) if not isinstance(bdir, str): bdir = os.path.join(*bdir) - self.basedirectory = os.path.expanduser(os.path.expandvars(bdir)) + self.basedirectory = expand_path(bdir) skipmode = extractor.config("skip", True) if skipmode == "abort":