Skip to content

Commit 8cc1518

Browse files
author
FelixAbrahamsson
committed
improve: better errors when crashing
1 parent 9176b87 commit 8cc1518

File tree

6 files changed

+99
-76
lines changed

6 files changed

+99
-76
lines changed

kmm/header/car_direction.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ def car_direction(tree: ElementTree):
2020
car_direction = re.search(
2121
r"CarDirection = \"(.*)\"",
2222
start_tag,
23-
).group(1)
23+
)
24+
if car_direction is None:
25+
raise ValueError("""Did not find a "CarDirection" field under the Start tag.""")
26+
else:
27+
car_direction = car_direction.group(1)
2428

2529
if not any(car_direction == item.value for item in kmm.CarDirection):
2630
raise ValueError(f"Unknown car direction {car_direction}")

kmm/header/header.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ def from_path(path: Path):
1616
"""
1717
Loads header data from .hdr file.
1818
"""
19-
tree = ElementTree.parse(path)
19+
try:
20+
tree = ElementTree.parse(path)
21+
except ElementTree.ParseError as e:
22+
raise ValueError("Unable to parse header file, invalid XML.") from e
23+
2024
position, sync = kmm.header.position_sync(tree)
2125
return Header(
2226
position=position,

kmm/header/position_sync.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,27 @@ def position_sync(tree: ElementTree):
1212
]
1313

1414
if len(sync_tags) == 0:
15-
raise ValueError("Did not find a sync tag in header.")
15+
raise ValueError("Did not find a Sync tag in header.")
1616

1717
sync_tag = sync_tags[0]
1818

19-
position = int(re.search(
19+
position = re.search(
2020
r"Position = \"(\d*)\"",
2121
sync_tag,
22-
).group(1))
22+
)
23+
if position is not None:
24+
position = int(position.group(1))
25+
else:
26+
raise ValueError("""Did not find a "Position" field under the Sync tag.""")
2327

24-
sync = int(re.search(
28+
sync = re.search(
2529
r"Sync = \"(\d*)\"",
2630
sync_tag,
27-
).group(1))
31+
)
32+
33+
if sync is not None:
34+
sync = int(sync.group(1))
35+
else:
36+
raise ValueError("""Did not find a "Sync" field under the Sync tag.""")
2837

2938
return position, sync

kmm/positions/read_kmm.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,35 @@
66

77
@validate_arguments
88
def read_kmm(path: Path):
9-
return pd.read_csv(
10-
path,
11-
sep="\t",
12-
encoding="latin1",
13-
names=[
14-
"centimeter",
15-
"track_section",
16-
"kilometer",
17-
"meter",
18-
"track_lane",
19-
"1?",
20-
"2?",
21-
"3?",
22-
"4?",
23-
"5?",
24-
"sweref99_tm_x",
25-
"sweref99_tm_y",
26-
"8?",
27-
"9?",
28-
],
29-
dtype=dict(
30-
track_section=str,
31-
kilometer=np.int32,
32-
meter=np.int32,
33-
track_lane=str,
34-
sweref99_tm_x=np.float32,
35-
sweref99_tm_y=np.float32,
36-
),
37-
)
9+
try:
10+
return pd.read_csv(
11+
path,
12+
sep="\t",
13+
encoding="latin1",
14+
names=[
15+
"centimeter",
16+
"track_section",
17+
"kilometer",
18+
"meter",
19+
"track_lane",
20+
"1?",
21+
"2?",
22+
"3?",
23+
"4?",
24+
"5?",
25+
"sweref99_tm_x",
26+
"sweref99_tm_y",
27+
"8?",
28+
"9?",
29+
],
30+
dtype=dict(
31+
track_section=str,
32+
kilometer=np.int32,
33+
meter=np.int32,
34+
track_lane=str,
35+
sweref99_tm_x=np.float32,
36+
sweref99_tm_y=np.float32,
37+
),
38+
)
39+
except Exception as e:
40+
raise ValueError("Unable to parse kmm2 file, invalid csv.") from e

kmm/positions/read_kmm2.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,48 @@ def read_kmm2(path: Path):
1818
if pattern.match(line) or pattern2.match(line)
1919
]
2020

21-
return pd.read_csv(
22-
path,
23-
skiprows=[0] + skiprows,
24-
delimiter="\t",
25-
encoding="latin1",
26-
names=[
27-
"code",
28-
"centimeter",
29-
"track_section",
30-
"kilometer",
31-
"meter",
32-
"track_lane",
33-
"1?",
34-
"2?",
35-
"3?",
36-
"4?",
37-
"sweref99_tm_x",
38-
"sweref99_tm_y",
39-
"contact_wire_material",
40-
"rail_model",
41-
"sliper_model",
42-
"between_stations",
43-
"5?",
44-
"6?",
45-
"7?",
46-
"8?",
47-
"max_speed",
48-
],
49-
dtype=dict(
50-
centimeter=np.int64,
51-
track_section=str,
52-
kilometer=np.int32,
53-
meter=np.int32,
54-
track_lane=str,
55-
sweref99_tm_x=np.float32,
56-
sweref99_tm_y=np.float32,
57-
),
58-
low_memory=False,
59-
)
21+
try:
22+
return pd.read_csv(
23+
path,
24+
skiprows=[0] + skiprows,
25+
delimiter="\t",
26+
encoding="latin1",
27+
names=[
28+
"code",
29+
"centimeter",
30+
"track_section",
31+
"kilometer",
32+
"meter",
33+
"track_lane",
34+
"1?",
35+
"2?",
36+
"3?",
37+
"4?",
38+
"sweref99_tm_x",
39+
"sweref99_tm_y",
40+
"contact_wire_material",
41+
"rail_model",
42+
"sliper_model",
43+
"between_stations",
44+
"5?",
45+
"6?",
46+
"7?",
47+
"8?",
48+
"max_speed",
49+
],
50+
dtype=dict(
51+
centimeter=np.int64,
52+
track_section=str,
53+
kilometer=np.int32,
54+
meter=np.int32,
55+
track_lane=str,
56+
sweref99_tm_x=np.float32,
57+
sweref99_tm_y=np.float32,
58+
),
59+
low_memory=False,
60+
)
61+
except Exception as e:
62+
raise ValueError("Unable to parse kmm2 file, invalid csv.") from e
6063

6164

6265
def test_patterns():

kmm/positions/sync_frame_index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def sync_frame_index(positions: Positions, header: Header, adjustment: PositionA
3131
.assign(frame_index=frame_index[:-adjustment])
3232
)
3333
else:
34-
raise ValueError(header.car_direction)
34+
raise ValueError(f"Unsupported car direction {header.car_direction}")
3535

3636
return positions.replace(
3737
dataframe=dataframe.assign(car_direction=header.car_direction)

0 commit comments

Comments
 (0)