-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_parser.py
More file actions
69 lines (53 loc) · 2.6 KB
/
Copy pathpreview_parser.py
File metadata and controls
69 lines (53 loc) · 2.6 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
import re
def parse_duration_to_seconds(duration_text):
"""Convert a preview duration string into a total number of seconds."""
parts = [int(part) for part in duration_text.split(':')]
if len(parts) == 3:
hours, minutes, seconds = parts
return (hours * 3600) + (minutes * 60) + seconds
if len(parts) == 2:
minutes, seconds = parts
return (minutes * 60) + seconds
if len(parts) == 1:
return parts[0]
raise ValueError(f'Unsupported duration format: {duration_text}')
def parse_preview_output(preview_output):
"""Extract timing and distance metrics from AxiDraw preview console output."""
duration_match = re.search(r'Estimated print time:\s*([0-9:]+)', preview_output)
path_match = re.search(r'Length of path to draw:\s*([0-9]+(?:\.[0-9]+)?)\s*m', preview_output)
travel_match = re.search(r'Pen-up travel distance:\s*([0-9]+(?:\.[0-9]+)?)\s*m', preview_output)
if not duration_match or not path_match or not travel_match:
raise ValueError('Could not parse preview output')
return {
'plot_duration': parse_duration_to_seconds(duration_match.group(1)),
'plot_path': float(path_match.group(1)),
'plot_travel': float(travel_match.group(1)),
}
def parse_plot_output(plot_output):
"""Extract elapsed timing and distance/lift metrics from plot console output."""
elapsed_match = re.search(r'Elapsed time:\s*([0-9:]+)', plot_output)
path_match = re.search(r'Length of path drawn:\s*([0-9]+(?:\.[0-9]+)?)\s*m', plot_output)
distance_match = re.search(r'Total distance moved:\s*([0-9]+(?:\.[0-9]+)?)\s*m', plot_output)
lift_patterns = [
r'(?im)^.*number\s+of\s+pen\s+lifts?\s*[:=]?\s*([0-9][0-9,]*)\b',
r'(?im)^.*pen\s*-?\s*lifts?\s*[:=]?\s*([0-9][0-9,]*)\b',
r'(?im)^.*pen\s*lift\s*count\s*[:=]?\s*([0-9][0-9,]*)\b',
r'(?im)^.*pen\s*-?\s*down\s*events?\s*[:=]?\s*([0-9][0-9,]*)\b',
r'(?im)^.*lifts?\s*[:=]?\s*([0-9][0-9,]*)\b',
]
lifts_match = None
for pattern in lift_patterns:
lifts_match = re.search(pattern, plot_output, re.IGNORECASE)
if lifts_match:
break
if not elapsed_match:
raise ValueError('Could not parse elapsed plot duration')
lifts_value = 0
if lifts_match:
lifts_value = int(lifts_match.group(1).replace(',', ''))
return {
'plot_duration': parse_duration_to_seconds(elapsed_match.group(1)),
'plot_path': float(path_match.group(1)) if path_match else 0.0,
'plot_travel': float(distance_match.group(1)) if distance_match else 0.0,
'lifts': lifts_value,
}