Skip to content

Commit d8eca80

Browse files
committed
Follow XDG completely: Also load configurations from the system-wide directories
I doubt this will be used widely, but there clearly is a use-case for multi-user or centrally administrated systems. Per-user profiles always override system-wide profiles.
1 parent ca17202 commit d8eca80

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

autorandr.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -497,19 +497,21 @@ def main(argv):
497497
print(str(e))
498498
options = { "--help": True }
499499

500-
profile_dir = os.path.expanduser("~/.autorandr")
501-
if not os.path.isdir(profile_dir):
502-
profile_dir = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "autorandr")
503-
504-
profile_path = os.path.join(profile_dir, "profiles")
505-
500+
profiles = {}
506501
try:
507-
profiles = load_profiles(profile_path)
508-
except OSError as e:
509-
if e.errno == 2: # No such file or directory
510-
profiles = {}
511-
else:
512-
raise e
502+
# Load profiles from each XDG config directory
503+
for directory in os.environ.get("XDG_CONFIG_DIRS", "").split(":"):
504+
system_profile_path = os.path.join(directory, "autorandr")
505+
if os.path.isdir(system_profile_path):
506+
profiles.update(load_profiles(system_profile_path))
507+
# For the user's profiles, prefer the legacy ~/.autorandr if it already exists
508+
# profile_path is also used later on to store configurations
509+
profile_path = os.path.expanduser("~/.autorandr")
510+
if not os.path.isdir(profile_path):
511+
# Elsewise, follow the XDG specification
512+
profile_path = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "autorandr")
513+
if os.path.isdir(profile_path):
514+
profiles.update(load_profiles(profile_path))
513515
except Exception as e:
514516
print("Failed to load profiles:\n%s" % str(e), file=sys.stderr)
515517
sys.exit(1)

0 commit comments

Comments
 (0)