From 96f1e073cd1aa6b7579256cbb6d42fa604a551b5 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 28 Jan 2025 13:07:06 +0100 Subject: ... --- overlays/worktime/worktime/__main__.py | 43 ++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'overlays/worktime') diff --git a/overlays/worktime/worktime/__main__.py b/overlays/worktime/worktime/__main__.py index 362c8da4..4c623acd 100755 --- a/overlays/worktime/worktime/__main__.py +++ b/overlays/worktime/worktime/__main__.py @@ -518,7 +518,7 @@ def format_days(worktime, days, date_format=None): return ', '.join(map(lambda group: ','.join(map(format_group, group)), groups)) -def worktime(**args): +def worktime(pull_forward_cutoff, **args): worktime = Worktime(**args) def format_worktime(worktime): @@ -562,7 +562,7 @@ def worktime(**args): else: return f"({difference_string})" - if worktime.time_pulled_forward >= timedelta(minutes = 15): + if worktime.time_pulled_forward >= pull_forward_cutoff: worktime_no_pulled_forward = deepcopy(worktime) worktime_no_pulled_forward.time_to_work -= worktime_no_pulled_forward.time_pulled_forward worktime_no_pulled_forward.time_pulled_forward = timedelta() @@ -798,6 +798,38 @@ def classification(classification_name, table, table_format, **args): def main(): def isotime(s): return datetime.fromisoformat(s).replace(tzinfo=tzlocal()) + def duration_minutes(s): + return timedelta(minutes = float(s)) + + def set_default_subparser(self, name, args=None, positional_args=0): + """default subparser selection. Call after setup, just before parse_args() + name: is the name of the subparser to call by default + args: if set is the argument list handed to parse_args() + + , tested with 2.7, 3.2, 3.3, 3.4 + it works with 2.6 assuming argparse is installed + """ + subparser_found = False + for arg in sys.argv[1:]: + if arg in ['-h', '--help']: # global help if no subparser + break + else: + for x in self._subparsers._actions: + if not isinstance(x, argparse._SubParsersAction): + continue + for sp_name in x._name_parser_map.keys(): + if sp_name in sys.argv[1:]: + subparser_found = True + if not subparser_found: + # insert default in last position before global positional + # arguments, this implies no global options are specified after + # first positional argument + if args is None: + sys.argv.insert(len(sys.argv) - positional_args, name) + else: + args.insert(len(args) - positional_args, name) + + argparse.ArgumentParser.set_default_subparser = set_default_subparser config = Worktime.config() @@ -807,8 +839,10 @@ def main(): parser.add_argument('--no-running', dest = 'include_running', action = 'store_false') parser.add_argument('--no-force-day-to-work', dest = 'force_day_to_work', action = 'store_false') subparsers = parser.add_subparsers(help = 'Subcommands') - parser.set_defaults(cmd = worktime) - time_worked_parser = subparsers.add_parser('time_worked', aliases = ['time', 'worked', 'today']) + worktime_parser = subparsers.add_parser('time_worked', aliases = ['time', 'worked']) + worktime_parser.add_argument('--pull-forward-cutoff', dest = 'pull_forward_cutoff', metavar = 'MINUTES', type = duration_minutes, default = timedelta(minutes = 15)) + worktime_parser.set_defaults(cmd = worktime) + time_worked_parser = subparsers.add_parser('today') time_worked_parser.add_argument('--no-round', dest = 'do_round', action = 'store_false') time_worked_parser.set_defaults(cmd = time_worked) diff_parser = subparsers.add_parser('diff') @@ -827,6 +861,7 @@ def main(): classification_parser.add_argument('--table', action = 'store_true') classification_parser.add_argument('--table-format', dest='table_format', type=str, default='fancy_grid') classification_parser.set_defaults(cmd = partial(classification, classification_name=classification_name)) + parser.set_default_subparser('time_worked') args = parser.parse_args() args.cmd(**vars(args)) -- cgit v1.2.3