From 2b8197e1068234e4d62275648b4a9428b5502d1c Mon Sep 17 00:00:00 2001 From: Maximilian Zettler Date: Wed, 24 Nov 2021 10:13:13 +0100 Subject: [PATCH] add smart-punch command --- timebot.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/timebot.py b/timebot.py index c66d38c..60d0e6e 100644 --- a/timebot.py +++ b/timebot.py @@ -212,6 +212,14 @@ if __name__ == '__main__': f"(default: now; unset for default)") parser_list_entries.add_argument("--items", help="max items to request per page", default=20, type=int) + # subparser command: `smart-punch` + parser_smart_punch = subparsers.add_parser("smart-punch", + help="use this command to auto punch in, punch out and create break " + "entries; this command tries to detect your last action and will " + "create entries in to following order: " + "punch_in(now) -> break_start(now) -> break_end(now) " + "-> punch_out(now)") + args = parser.parse_args() if args.v: @@ -237,7 +245,29 @@ if __name__ == '__main__': )) getattr(tb, args.t)(punch_datetime) elif args.subparser_name == "smart-punch": - pass + now = datetime.datetime.now() + last_punch = tb.get_entries(1, now.replace(hour=0, minute=0, second=0, microsecond=0)) + method = None + if not last_punch: + logger.info("could not detect any time entry for today... punching in") + method = "punch_in" + elif last_punch[0]["entryNumber"] == COMING_ENTRY_CODE_ID: + logger.info("your last entry was `punch_in`... starting break") + method = "break_start" + elif last_punch[0]["entryNumber"] == BREAK_START_ENTRY_CODE_ID: + logger.info("your last entry was `break_start`... ending break") + method = "break_end" + elif last_punch[0]["entryNumber"] == BREAK_END_ENTRY_CODE_ID: + logger.info("your last entry was `break_end`... punching out") + method = "punch_out" + elif last_punch[0]["entryNumber"] == LEAVING_ENTRY_CODE_ID: + logger.error("your last entry was `punch_out`... punching in again with this command is not supported") + sys.exit(1) + if method is None: + logger.error("hit an unknown situation... detection failed; run with `-v` for more info") + logger.debug(f"last entry was: {last_punch}") + exit(1) + getattr(tb, method)(now) elif args.subparser_name == "list-entries": end_date = None if args.end_date: