From 57c89104033273451d448399d56d992c9d429512 Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Wed, 19 Feb 2020 18:36:26 +0000 Subject: [PATCH 1/2] Treat empty environment variables like unset ones As per the XDG spec. Previously, empty variables would be used as-is. --- appdirs.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/appdirs.py b/appdirs.py index 975e9bf..9d8cc8d 100644 --- a/appdirs.py +++ b/appdirs.py @@ -41,6 +41,12 @@ system = sys.platform +def getenv(name, default=None): + value = os.getenv(name) + if not value: + value = default + return value + def user_data_dir(appname=None, appauthor=None, version=None, roaming=False): r"""Return full path to the user-specific data dir for this application. @@ -89,7 +95,7 @@ def user_data_dir(appname=None, appauthor=None, version=None, roaming=False): if appname: path = os.path.join(path, appname) else: - path = os.getenv('XDG_DATA_HOME', os.path.expanduser("~/.local/share")) + path = getenv('XDG_DATA_HOME', os.path.expanduser("~/.local/share")) if appname: path = os.path.join(path, appname) if appname and version: @@ -144,7 +150,7 @@ def site_data_dir(appname=None, appauthor=None, version=None, multipath=False): else: # XDG default for $XDG_DATA_DIRS # only first, if multipath is False - path = os.getenv('XDG_DATA_DIRS', + path = getenv('XDG_DATA_DIRS', os.pathsep.join(['/usr/local/share', '/usr/share'])) pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)] if appname: @@ -199,7 +205,7 @@ def user_config_dir(appname=None, appauthor=None, version=None, roaming=False): if appname: path = os.path.join(path, appname) else: - path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config")) + path = getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config")) if appname: path = os.path.join(path, appname) if appname and version: @@ -248,7 +254,7 @@ def site_config_dir(appname=None, appauthor=None, version=None, multipath=False) else: # XDG default for $XDG_CONFIG_DIRS # only first, if multipath is False - path = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg') + path = getenv('XDG_CONFIG_DIRS', '/etc/xdg') pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)] if appname: if version: @@ -311,7 +317,7 @@ def user_cache_dir(appname=None, appauthor=None, version=None, opinion=True): if appname: path = os.path.join(path, appname) else: - path = os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache')) + path = getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache')) if appname: path = os.path.join(path, appname) if appname and version: @@ -353,7 +359,7 @@ def user_state_dir(appname=None, appauthor=None, version=None, roaming=False): if system in ["win32", "darwin"]: path = user_data_dir(appname, appauthor, None, roaming) else: - path = os.getenv('XDG_STATE_HOME', os.path.expanduser("~/.local/state")) + path = getenv('XDG_STATE_HOME', os.path.expanduser("~/.local/state")) if appname: path = os.path.join(path, appname) if appname and version: From 3a40b2be6c666f3d612884fd73be790816057b9a Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Thu, 20 Feb 2020 11:30:09 +0000 Subject: [PATCH 2/2] Empty environment variable: review comments * Shorten getenv function * Fix formatting --- appdirs.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/appdirs.py b/appdirs.py index 9d8cc8d..c1a9ba1 100644 --- a/appdirs.py +++ b/appdirs.py @@ -42,10 +42,7 @@ def getenv(name, default=None): - value = os.getenv(name) - if not value: - value = default - return value + return os.getenv(name) or default def user_data_dir(appname=None, appauthor=None, version=None, roaming=False): @@ -150,8 +147,9 @@ def site_data_dir(appname=None, appauthor=None, version=None, multipath=False): else: # XDG default for $XDG_DATA_DIRS # only first, if multipath is False - path = getenv('XDG_DATA_DIRS', - os.pathsep.join(['/usr/local/share', '/usr/share'])) + path = getenv( + 'XDG_DATA_DIRS', os.pathsep.join(['/usr/local/share', '/usr/share']) + ) pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)] if appname: if version: