Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change order of list returned by config_dirs() #168

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions confuse/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,20 @@ def find_package_path(name):
return os.path.dirname(os.path.abspath(filepath))


def xdg_config_home():
"""Return the value of the XDG_CONFIG_HOME
environment variable if it is set and nonempty,
and UNIX_DIR_FALLBACK otherwise
"""
config_path = os.getenv('XDG_CONFIG_HOME', UNIX_DIR_FALLBACK)
return config_path if config_path != '' else UNIX_DIR_FALLBACK

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string is falsy. Use as a boolean.

Suggested change
return config_path if config_path != '' else UNIX_DIR_FALLBACK
return config_path or UNIX_DIR_FALLBACK


def xdg_config_dirs():
"""Returns a list of paths taken from the XDG_CONFIG_DIRS
and XDG_CONFIG_HOME environment varibables if they exist
environment variable if it exists and is nonempty
"""
paths = []
if 'XDG_CONFIG_HOME' in os.environ:
paths.append(os.environ['XDG_CONFIG_HOME'])
if 'XDG_CONFIG_DIRS' in os.environ:
if 'XDG_CONFIG_DIRS' in os.environ and os.environ['XDG_CONFIG_DIRS'] != '':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to look before leap here, just use the default fallback behavior (will return None if not present and empty string is falsy):

Suggested change
if 'XDG_CONFIG_DIRS' in os.environ and os.environ['XDG_CONFIG_DIRS'] != '':
if os.environ.get('XDG_CONFIG_DIRS'):

paths.extend(os.environ['XDG_CONFIG_DIRS'].split(':'))
else:
paths.append('/etc/xdg')
Expand All @@ -155,7 +161,7 @@ def config_dirs():
paths = []

if platform.system() == 'Darwin':
paths.append(UNIX_DIR_FALLBACK)
paths.append(xdg_config_home())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XDG does not apply to Darwin. These files should end up somewhere in ~/Library on that platform.

paths.append(MAC_DIR)
paths.extend(xdg_config_dirs())

Expand All @@ -166,13 +172,13 @@ def config_dirs():

else:
# Assume Unix.
paths.append(UNIX_DIR_FALLBACK)
paths.append(xdg_config_home())
paths.extend(xdg_config_dirs())

# Expand and deduplicate paths.
# Expand and paths and remove duplicates and relative paths.
out = []
for path in paths:
path = os.path.abspath(os.path.expanduser(path))
if path not in out:
path = os.path.expanduser(path)
if path not in out and os.path.isabs(path):
out.append(path)
return out