Skip to content
Snippets Groups Projects
Verified Commit 703f0b42 authored by Marco Aceti's avatar Marco Aceti
Browse files

Better handle partially cancelled trains

...with no arriving trains
parent ddd19019
Branches
No related tags found
No related merge requests found
Pipeline #2673 passed
......@@ -40,13 +40,13 @@ def test_stop_time_assumption():
@pytest.mark.parametrize(
"data_file, expected_repr",
[
("train-stop_10860.json", "@ Piacenza 09:07 ~ 09:07 +0.5m [5 ~ 5]"),
("train-stop_3073.json", "@ Arquata Scrivia 17:43 --> 17:44 [5]"),
("train-stop_10860.json", "@ (P) Piacenza 09:07 ~ 09:07 +0.5m [5 ~ 5]"),
("train-stop_3073.json", "@ (F) Arquata Scrivia 17:43 --> 17:44 [5]"),
(
"train-stop_555.json",
"@ Latina 14:58 ~ 15:01 +3.5m --> 15:00 ~ 15:03 +3.5m [? ~ 2]",
"@ (F) Latina 14:58 ~ 15:01 +3.5m --> 15:00 ~ 15:03 +3.5m [? ~ 2]",
),
("train-stop_22662.json", "@ Treviglio 17:50 [2 TR Ovest]"),
("train-stop_22662.json", "@ (A) Treviglio 17:50 [2 TR Ovest]"),
],
)
def test_stop_repr(data_file, expected_repr):
......
......@@ -168,10 +168,21 @@ class Train:
# Assertion: there should always be at least two stops - the first and the last.
assert len(self.stops) >= 2
# ViaggiaTreno bug: sometimes, the last stop is not marked as last
# API bug: sometimes, the last stop is not marked as last
logging.warning(f"{self.category} {self.number} has not a last stop.")
if len(list(filter(lambda s: s.stop_type == tr_st.TrainStopType.LAST, self.stops))) == 0: # fmt: skip
self.stops[len(self.stops) - 1].stop_type = tr_st.TrainStopType.LAST
i = len(self.stops) - 1
while i > 0:
if self.stops[i] != tr_st.TrainStopType.CANCELLED and isinstance(
self.stops[i].arrival, tr_st.TrainStopTime
):
break
i -= 1
if i < 2:
self.cancelled = True
return
self.stops[i].stop_type = tr_st.TrainStopType.LAST
def fetch_trenord(self) -> None:
"""Try fetch more details about the train, using Trenord API."""
......@@ -260,6 +271,9 @@ class Train:
if not self._fetched or self._phantom:
return None
if self.cancelled:
return True
assert isinstance(self.stops, list)
arriving_stop: tr_st.TrainStop = next(
filter(lambda s: s.stop_type == tr_st.TrainStopType.LAST, self.stops)
......
......@@ -213,7 +213,7 @@ class TrainStop:
if stop_type_raw == "O":
stop_type = TrainStopType.FIRST
elif stop_type_raw == "F":
stop_type = (TrainStopType.STOP,)
stop_type = TrainStopType.STOP
elif stop_type_raw == "D":
stop_type = TrainStopType.LAST
else:
......@@ -238,7 +238,7 @@ class TrainStop:
)
def __repr__(self) -> str:
ret = f"@ {self.station.name} "
ret = f"@ ({self.stop_type.value}) {self.station.name} "
if self.stop_type == TrainStopType.FIRST:
ret += f"{self.departure}"
elif self.stop_type == TrainStopType.LAST:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment