From eaa1b5a3d73fdf295c38aea528732024202178c6 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 11 Feb 2023 17:33:34 +0100 Subject: Do not create a menu instance if we want to apply a profile Signed-off-by: Olivier Gayot --- monitor-menu.py | 43 +++++++++++++++++++++++-------------------- 1 file 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__': -- cgit v1.2.3