async def get(self, params: structs.Params) -> Response: """Returns reports along a flight path""" stations = await FlightRouter().fetch("station", params.distance, params.route) resp = [] for icao in stations: with suppress(BadStation): resp.append(asdict(Station.from_icao(icao))) resp = { "meta": handle.MetarHandler().make_meta(), "route": params.route, "results": resp, } return self.make_response(resp, params.format)
def MultiStation(values: str) -> list[Station]: """Validates a comma-separated list of station idents""" values = values.upper().split(",") if not values: raise Invalid("Could not find any stations in the request") if len(values) > 10: raise Invalid("Multi requests are limited to 10 stations or less") ret = [] for icao in values: try: ret.append(Station.from_icao(icao)) except BadStation as exc: raise Invalid(f"{icao} is not a valid ICAO station ident") from exc return ret
def MultiStation(stations: str) -> [Station]: """ Validates a comma-separated list of station idents """ stations = stations.upper().split(",") if not stations: raise Invalid("Could not find any stations in the request") if len(stations) > 10: raise Invalid("Multi requests are limited to 10 stations or less") ret = [] for stn in stations: try: ret.append(Station.from_icao(stn)) except BadStation: raise Invalid(f"{stn} is not a valid ICAO station ident") return ret
def to_coordinates(values: list[Union[Coord, str]], last_value: Optional[list[Coord]] = None) -> list[Coord]: """Convert any known idents found in a flight path into coordinates""" if not values: return values coord = values[0] if isinstance(coord, str): try: station = Station.from_icao(coord) coord = (station.latitude, station.longitude) except BadStation: coords = NAVAIDS[coord] if len(coords) == 1: coord = coords[0] else: new_coords = to_coordinates(values[1:], coords) new_coord = new_coords[0] if new_coords else None coord = _best_coord(last_value, coords, new_coord) return [coord] + new_coords return [coord] + to_coordinates(values[1:], coord)
def validator(loc: str) -> Station: loc = loc.upper().split(",") if len(loc) == 1: icao = loc[0] try: return Station.from_icao(icao) except BadStation: if icao in ICAO_WHITELIST: return Station(*([None] * 4), "DNE", icao, *([None] * 9)) raise Invalid(f"{icao} is not a valid ICAO station ident") elif len(loc) == 2: try: lat, lon = Latitude(loc[0]), Longitude(loc[1]) if coerce_station: return Station.nearest(lat, lon)[0] return lat, lon except: raise Invalid(f"{loc} is not a valid coordinate pair") else: raise Invalid(f"{loc} is not a valid station/coordinate pair")
def to_coordinates(values: list[Union[Coord, str]], last_value: Optional[list[Coord]] = None) -> list[Coord]: """Convert any known idents found in a flight path into coordinates""" if not values: return values coord = values[0] if isinstance(coord, str): try: # No IATA due to navaid conflict station = Station.from_icao(coord) coord = (station.latitude, station.longitude) except BadStation: coords = NAVAIDS.get(coord) if coords is None: # pylint: disable=raise-missing-from raise Invalid(f"Could not find coordinates for {coord}") if len(coords) == 1: coord = coords[0] else: new_coords = to_coordinates(values[1:], coords) new_coord = new_coords[0] if new_coords else None coord = _best_coord(last_value, coords, new_coord) return [coord] + new_coords return [coord] + to_coordinates(values[1:], coord)
def __init__(self, ident, index): self.idnet = ident self.index = index self.station = Station.from_icao(ident)
def _station_for(code: str) -> Station: if len(code) == 3: return Station.from_iata(code) return Station.from_icao(code)