diff options
| -rwxr-xr-x | overlays/worktime/worktime/__main__.py | 43 |
1 files changed, 39 insertions, 4 deletions
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): | |||
| 518 | return ', '.join(map(lambda group: ','.join(map(format_group, group)), groups)) | 518 | return ', '.join(map(lambda group: ','.join(map(format_group, group)), groups)) |
| 519 | 519 | ||
| 520 | 520 | ||
| 521 | def worktime(**args): | 521 | def worktime(pull_forward_cutoff, **args): |
| 522 | worktime = Worktime(**args) | 522 | worktime = Worktime(**args) |
| 523 | 523 | ||
| 524 | def format_worktime(worktime): | 524 | def format_worktime(worktime): |
| @@ -562,7 +562,7 @@ def worktime(**args): | |||
| 562 | else: | 562 | else: |
| 563 | return f"({difference_string})" | 563 | return f"({difference_string})" |
| 564 | 564 | ||
| 565 | if worktime.time_pulled_forward >= timedelta(minutes = 15): | 565 | if worktime.time_pulled_forward >= pull_forward_cutoff: |
| 566 | worktime_no_pulled_forward = deepcopy(worktime) | 566 | worktime_no_pulled_forward = deepcopy(worktime) |
| 567 | worktime_no_pulled_forward.time_to_work -= worktime_no_pulled_forward.time_pulled_forward | 567 | worktime_no_pulled_forward.time_to_work -= worktime_no_pulled_forward.time_pulled_forward |
| 568 | worktime_no_pulled_forward.time_pulled_forward = timedelta() | 568 | worktime_no_pulled_forward.time_pulled_forward = timedelta() |
| @@ -798,6 +798,38 @@ def classification(classification_name, table, table_format, **args): | |||
| 798 | def main(): | 798 | def main(): |
| 799 | def isotime(s): | 799 | def isotime(s): |
| 800 | return datetime.fromisoformat(s).replace(tzinfo=tzlocal()) | 800 | return datetime.fromisoformat(s).replace(tzinfo=tzlocal()) |
| 801 | def duration_minutes(s): | ||
| 802 | return timedelta(minutes = float(s)) | ||
| 803 | |||
| 804 | def set_default_subparser(self, name, args=None, positional_args=0): | ||
| 805 | """default subparser selection. Call after setup, just before parse_args() | ||
| 806 | name: is the name of the subparser to call by default | ||
| 807 | args: if set is the argument list handed to parse_args() | ||
| 808 | |||
| 809 | , tested with 2.7, 3.2, 3.3, 3.4 | ||
| 810 | it works with 2.6 assuming argparse is installed | ||
| 811 | """ | ||
| 812 | subparser_found = False | ||
| 813 | for arg in sys.argv[1:]: | ||
| 814 | if arg in ['-h', '--help']: # global help if no subparser | ||
| 815 | break | ||
| 816 | else: | ||
| 817 | for x in self._subparsers._actions: | ||
| 818 | if not isinstance(x, argparse._SubParsersAction): | ||
| 819 | continue | ||
| 820 | for sp_name in x._name_parser_map.keys(): | ||
| 821 | if sp_name in sys.argv[1:]: | ||
| 822 | subparser_found = True | ||
| 823 | if not subparser_found: | ||
| 824 | # insert default in last position before global positional | ||
| 825 | # arguments, this implies no global options are specified after | ||
| 826 | # first positional argument | ||
| 827 | if args is None: | ||
| 828 | sys.argv.insert(len(sys.argv) - positional_args, name) | ||
| 829 | else: | ||
| 830 | args.insert(len(args) - positional_args, name) | ||
| 831 | |||
| 832 | argparse.ArgumentParser.set_default_subparser = set_default_subparser | ||
| 801 | 833 | ||
| 802 | config = Worktime.config() | 834 | config = Worktime.config() |
| 803 | 835 | ||
| @@ -807,8 +839,10 @@ def main(): | |||
| 807 | parser.add_argument('--no-running', dest = 'include_running', action = 'store_false') | 839 | parser.add_argument('--no-running', dest = 'include_running', action = 'store_false') |
| 808 | parser.add_argument('--no-force-day-to-work', dest = 'force_day_to_work', action = 'store_false') | 840 | parser.add_argument('--no-force-day-to-work', dest = 'force_day_to_work', action = 'store_false') |
| 809 | subparsers = parser.add_subparsers(help = 'Subcommands') | 841 | subparsers = parser.add_subparsers(help = 'Subcommands') |
| 810 | parser.set_defaults(cmd = worktime) | 842 | worktime_parser = subparsers.add_parser('time_worked', aliases = ['time', 'worked']) |
| 811 | time_worked_parser = subparsers.add_parser('time_worked', aliases = ['time', 'worked', 'today']) | 843 | worktime_parser.add_argument('--pull-forward-cutoff', dest = 'pull_forward_cutoff', metavar = 'MINUTES', type = duration_minutes, default = timedelta(minutes = 15)) |
| 844 | worktime_parser.set_defaults(cmd = worktime) | ||
| 845 | time_worked_parser = subparsers.add_parser('today') | ||
| 812 | time_worked_parser.add_argument('--no-round', dest = 'do_round', action = 'store_false') | 846 | time_worked_parser.add_argument('--no-round', dest = 'do_round', action = 'store_false') |
| 813 | time_worked_parser.set_defaults(cmd = time_worked) | 847 | time_worked_parser.set_defaults(cmd = time_worked) |
| 814 | diff_parser = subparsers.add_parser('diff') | 848 | diff_parser = subparsers.add_parser('diff') |
| @@ -827,6 +861,7 @@ def main(): | |||
| 827 | classification_parser.add_argument('--table', action = 'store_true') | 861 | classification_parser.add_argument('--table', action = 'store_true') |
| 828 | classification_parser.add_argument('--table-format', dest='table_format', type=str, default='fancy_grid') | 862 | classification_parser.add_argument('--table-format', dest='table_format', type=str, default='fancy_grid') |
| 829 | classification_parser.set_defaults(cmd = partial(classification, classification_name=classification_name)) | 863 | classification_parser.set_defaults(cmd = partial(classification, classification_name=classification_name)) |
| 864 | parser.set_default_subparser('time_worked') | ||
| 830 | args = parser.parse_args() | 865 | args = parser.parse_args() |
| 831 | 866 | ||
| 832 | args.cmd(**vars(args)) | 867 | args.cmd(**vars(args)) |
