From d08c797ae7dd8c4ab69cc17cde69a4b427cc1897 Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Tue, 19 Mar 2024 23:21:18 +0300 Subject: [PATCH] [~] Refactor --- dotfiles/.local/bin/commands | 62 +++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/dotfiles/.local/bin/commands b/dotfiles/.local/bin/commands index 11f91ce..a574c86 100755 --- a/dotfiles/.local/bin/commands +++ b/dotfiles/.local/bin/commands @@ -1361,17 +1361,59 @@ def numpy_linspace(a, b, count): def pm_service(argv): parser = optparse.OptionParser() + parser.add_option( + '--events', + dest='events', + default=[], + action='append', + help='pb,tp,kb', + ) + parser.add_option( + '--verbose', + dest='verbose', + type=str, + default=None, + help='true,false', + ) options, args = parser.parse_args(argv) + if options.verbose is None: + options.verbose = False + else: + val = json.loads(options.verbose) + assert isinstance(val, bool) + options.verbose = val + + if len(options.events) == 0: + options.events.extend([ + 'pb', + #'tp', 'kb' + ]) + + assert all([ + o in [ + 'pb','tp', 'kb' + ] + for o in options.events + ]) + assert sys.platform == 'darwin' + wu = 0 + while True: subprocess.check_call([ 'osascript', '-e', 'tell application "Finder" to sleep' ]) - subprocess.check_call(['pmset', 'sleepnow']) + subprocess.check_call( + ['pmset','sleepnow'], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + + wu += 1 sample = ( @@ -1397,9 +1439,9 @@ def pm_service(argv): if not cmd is None and 'TurnedOn' in cmd: if ( - 'AppleMultitouchDevice' in cmd or - 'AppleACPIButton' in cmd or - 'eventType:29' in cmd + ('AppleMultitouchDevice' in cmd and 'tp' in options.events) or + ('AppleACPIButton' in cmd and 'pb' in options.events) or + ('eventType:29' in cmd and 'kb' in options.events) ): action = 'wake-up' break @@ -1407,7 +1449,15 @@ def pm_service(argv): action = 'sleep' break - logging.error(json.dumps(dict(cmd=cmd, action=action,))) + + if options.verbose: + logging.error(json.dumps(dict(cmd=cmd, action=action,))) + else: + print('\r%s wu : %d, la : %s' % ( + datetime.datetime.now().isoformat(), + wu, + action + ), end='') if action == 'wake-up': break @@ -1416,6 +1466,8 @@ def pm_service(argv): else: raise NotImplementedError + print('') + def desktop_services(argv): parser = optparse.OptionParser() parser.add_option(