diff options
| author | Olivier Gayot <olivier.gayot@sigexec.com> | 2023-05-14 19:04:25 +0200 | 
|---|---|---|
| committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2023-05-14 19:29:59 +0200 | 
| commit | f46620d94d6a76e4b3f6f8cb41c89994879482ec (patch) | |
| tree | 3427d32301158b6a9c1ba1d27e0487c3938c387a | |
| parent | 73382682c8cfb3c87cae9d46d00c25829bbb9150 (diff) | |
Allow to specify the profiles file to use
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
| -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] | 
