diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2023-02-11 17:33:34 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2023-02-11 17:47:58 +0100 |
commit | eaa1b5a3d73fdf295c38aea528732024202178c6 (patch) | |
tree | 20ba78c68d82e57c8c4b81f4454953f1d157e653 /monitor-menu.py | |
parent | 3cde5b6c7f4f9459bfed67f649582c136824a9cc (diff) |
Do not create a menu instance if we want to apply a profile
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'monitor-menu.py')
-rwxr-xr-x | monitor-menu.py | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/monitor-menu.py b/monitor-menu.py index f8baa7a..8a77c41 100755 --- a/monitor-menu.py +++ b/monitor-menu.py @@ -83,28 +83,25 @@ class Profile: subprocess.run(feh_cmd, check=False) -class MonitorMenu(): - def __init__(self, config_file='~/.config/monitor-profiles.json'): - with open(expanduser(config_file)) as fh: - data = json.load(fh) - self.profiles = [Profile.from_json_dict(item) for item in data] +class MonitorMenu: + def __init__(self, profiles: list[Profile]) -> None: + self.profiles = profiles self.d = dialog.Dialog(autowidgetsize=True) - def run(self, profile_idx=None): + def run(self): choices = [] - if profile_idx is None: - i = 0 - for p in self.profiles: - choices.append((str(i), p.name)) - i += 1 + i = 0 + for p in self.profiles: + choices.append((str(i), p.name)) + i += 1 - code, profile_idx = self.d.menu( - 'Select the profile you want to use.', - choices=choices) + code, profile_idx = self.d.menu( + 'Select the profile you want to use.', + choices=choices) - if code in (self.d.ESC, self.d.CANCEL): - return + if code in (self.d.ESC, self.d.CANCEL): + return try: self.profiles[int(profile_idx)].apply() @@ -119,12 +116,18 @@ def main(): args = parser.parse_args() logging.basicConfig(level=args.log_level.numerical_level) - menu = MonitorMenu() + config_file = '~/.config/monitor-profiles.json' + with open(expanduser(config_file)) as fh: + data = json.load(fh) + profiles = [Profile.from_json_dict(item) for item in data] - if args.index is not None: - menu.run(profile_idx=args.index) + if args.index is None: + MonitorMenu(profiles).run() else: - menu.run() + try: + profiles[args.index].apply() + except IndexError: + raise NoMatchingProfile from None if __name__ == '__main__': |