-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
115 lines (80 loc) · 3.39 KB
/
Copy pathutils.py
File metadata and controls
115 lines (80 loc) · 3.39 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from Workout import Workout
from WorkoutStep import WorkoutStep
from setup import INTENSITY_DICT, PART_DICT
# -----------------------------------------------------------------------------
# Setup file loader
# -----------------------------------------------------------------------------
def load_workout(filename):
workout = Workout()
with open(filename, "r", encoding="utf-8") as fr:
for lineno, raw in enumerate(fr, start=1):
line = raw.strip()
# Empty line : ignore
if not line:
continue
# Comment line : ignore
if line.startswith("#"):
continue
upper = line.upper()
# WORKOUT title
if upper.startswith("WORKOUT"):
title = line[len("WORKOUT"):].strip()
if title:
workout.title = title
else:
workout.title = "Workout"
continue
# FIELD field
elif upper.startswith("FIELD"):
field = line[len("FIELD"):].strip()
if field:
workout.field = field
else:
workout.field = "Field"
continue
parts = line.split()
#Expected .wo file data format is : duration_float_minutes spm_int intensity_char
#hence 3 values mandatory per data line
#optionnaly a 4th parameter indicate a partial stroke (eg "only legs")
if len(parts) != 3 and len(parts) != 4:
raise ValueError(
f"Line {lineno}: expected 'minutes cpm intensity <part>'"
)
try:
minutes = float(parts[0])
cpm = int(parts[1])
intensity = str(parts[2])
part = str(parts[3]) if len(parts) == 4 else None
except ValueError:
raise ValueError(
f"Line {lineno}: invalid data type; must be: int_or_float int character"
)
if (minutes <= 0.0) or (minutes > 120.0):
raise ValueError(
f"Line {lineno}: duration must be > 0 and <= 120"
)
if (cpm <= 0) or (cpm > 50):
raise ValueError(
f"Line {lineno}: CPM must be > 0 and <= 50"
)
#If the intensity character is not one of the INTENSITY_DICT key, then error
if intensity not in INTENSITY_DICT:
raise ValueError(
f"Line {lineno}: intensity must be one of R, E, N, F or M"
)
if (part is not None) and (part not in PART_DICT):
raise ValueError(
f"Line {lineno}: part must be one of {PART_DICT.keys()}"
)
workout.steps.append(
WorkoutStep(minutes, cpm, intensity, part)
)
if not workout.steps:
raise ValueError("Workout is empty.")
return workout
# -----------------------------------------------------------------------------
def format_time(seconds):
seconds = max(0, int(seconds))
m = seconds // 60
s = seconds % 60
return f"{m:02d}:{s:02d}"