diff options
Diffstat (limited to 'monitor_menu')
-rw-r--r-- | monitor_menu/__main__.py | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/monitor_menu/__main__.py b/monitor_menu/__main__.py index 91942e2..87719e7 100644 --- a/monitor_menu/__main__.py +++ b/monitor_menu/__main__.py @@ -5,7 +5,7 @@ import dataclasses import enum import json import logging -from os.path import expanduser +import pathlib import subprocess from typing import Any import yaml @@ -151,10 +151,35 @@ class MonitorMenu: raise NoMatchingProfile from None +def load_profiles_config(path: pathlib.Path | None): + if path is not None: + with path.open() as fh: + if path.suffix == ".json": + return json.load(fh) + # Expect YAML by default + return yaml.safe_load(fh) + + # Try monitor-profiles.yaml and revert to monitor-profiles.json if the + # first one does not exist. + try: + with pathlib.Path("~/.config/monitor-profiles.yaml").expanduser().open() as fh: + return yaml.safe_load(fh) + except FileNotFoundError: + with pathlib.Path("~/.config/monitor-profiles.json").expanduser().open() as fh: + return json.load(fh) + + def main(): parser = argparse.ArgumentParser() parser.add_argument("--log-level", "--loglevel", type=LogLevel, default="info") - parser.add_argument("--graphical-server", choices=("wayland", "xorg")) + parser.add_argument("--graphical-server", choices=("wayland", "xorg"), type=GraphicalServer) + parser.add_argument( + "--profiles-config", + metavar="PATH", + type=pathlib.Path, + help="Configuration file to use." + " By default $HOME/.config/monitor-profiles.yaml (preferred)" + " or $HOME/.config/monitor-profiles.json") subparser = parser.add_subparsers(dest="command") run_parser = subparser.add_parser("run") run_parser.add_argument("--apply-exit", action="store_true") @@ -166,12 +191,7 @@ def main(): args = parser.parse_args() logging.basicConfig(level=args.log_level.numerical_level) - try: - with open(expanduser("~/.config/monitor-profiles.yaml")) as fh: - data = yaml.safe_load(fh) - except FileNotFoundError: - with open(expanduser("~/.config/monitor-profiles.json")) as fh: - data = json.load(fh) + data = load_profiles_config(args.profiles_config) profiles = [Profile.from_json_dict(item) for item in data] |