-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgarmin_planner.py
85 lines (69 loc) · 4.76 KB
/
garmin_planner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env python
import sys
import argparse
import logging
from planner.fartlek import cmd_fartlek
from planner.import_export import cmd_import_workouts
from planner.import_export import cmd_export_workouts
from planner.import_export import cmd_delete_workouts
from planner.schedule import cmd_schedule_workouts
from planner.schedule import cmd_unschedule_workouts
from planner.garmin_client import cmd_login
def parse_args(argv):
parser = argparse.ArgumentParser()
# common options
parser.add_argument('--dry-run', action='store_true', default=False, help='Do not modify anything, only show what would be done.')
parser.add_argument('--oauth-folder', default='~/.garth', help='Folder where the Garmin oauth token is stored.')
parser.add_argument('--treadmill', action='store_true', default=False, help='Convert distance end conditions to time end conditions where possible (treadmill mode).')
parser.add_argument('--log-level', required=False,
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
default='INFO',
help='set log level')
# add sub commands
subparsers = parser.add_subparsers(help='available commands')
garmin_login = subparsers.add_parser('login', help='refresh or create Oauth credentials for your Garmin account')
garmin_login.set_defaults(func=cmd_login)
import_wo = subparsers.add_parser('import', help='import workouts')
import_wo.set_defaults(func=cmd_import_workouts)
import_wo.add_argument('--workouts-file', required=True, help='yaml file containing the workouts to create')
import_wo.add_argument('--name-filter', required=False, help='only import the workouts whose name matches the filter.')
import_wo.add_argument('--replace', action='store_true', default=False, help='replace any existing workouts with the same name (only if workout was created)')
export_wos = subparsers.add_parser('export', help='export current workouts')
export_wos.set_defaults(func=cmd_export_workouts)
export_wos.add_argument('--export-file', default='', help='yaml file containing the workouts to create')
export_wos.add_argument('--format', required=False,
choices=['JSON', 'YAML'],
default=None,
help='format of the export file')
export_wos.add_argument('--clean', required=False, action='store_true', default=False,
help='remove null items and useless data')
export_wos.add_argument('--name-filter', required=False, help='name (or part of the name) of workshop to export. Accepts regular expressions.')
delete_wo = subparsers.add_parser('delete', help='delete workouts')
delete_wo.set_defaults(func=cmd_delete_workouts)
delete_wo.add_argument('--workout-ids', required=False, help='comma separated list of workouts to delete')
delete_wo.add_argument('--name-filter', required=False, help='name (or part of the name) of workshop to delete. Accepts regular expressions.')
schedule = subparsers.add_parser('schedule', help='schedule workouts in a training plan. Workouts should have previously been added, and be named: [PLAN_ID] W01S01 [DESCRIPTION]')
schedule.set_defaults(func=cmd_schedule_workouts)
schedule.add_argument('--race-day', required=True, help='the date of the race. Should correspond to the last workout of the training plan.')
schedule.add_argument('--training-plan', required=True, help='the training plan ID. Corresponds to the common prefix of all workouts in the plan.')
unschedule = subparsers.add_parser('unschedule', help='unschedule workouts from calendar.')
unschedule.set_defaults(func=cmd_unschedule_workouts)
unschedule.add_argument('--start-date', required=False, help='the date from which to start looking for workouts in the calendar.')
unschedule.add_argument('--training-plan', required=True, help='the training plan ID. Corresponds to the common prefix of all workouts in the plan.')
fartlek = subparsers.add_parser('fartlek', help='create a random fartlek workout')
fartlek.set_defaults(func=cmd_fartlek)
fartlek.add_argument('--duration', required=True, help='workout duration in mm:ss')
fartlek.add_argument('--target-pace', required=True, help='target pace in mm:ss')
fartlek.add_argument('--schedule', required=False, help='schedule this workout (today, tomorrow, YYY-MM-DD)')
return parser.parse_args(argv)
def get_or_throw(d, key, error):
try:
return d[key]
except: # noqa: E722
raise Exception(error)
if __name__ == '__main__':
args = parse_args(sys.argv[1:])
logging.getLogger().setLevel(getattr(logging, args.log_level))
FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
logging.basicConfig(format=FORMAT)
args.func(args)