@@ -148,7 +148,9 @@ def format_station(code: str, station: list[str]) -> dict:
148148
149149def build_stations () -> tuple [dict , dict ]:
150150 """Build the station dict from source file."""
151- stations , code_map = {}, {}
151+ code_map = {}
152+ with OUTPUT_PATH .open (encoding = "utf8" ) as fin :
153+ stations = json .load (fin )
152154 data = csv .reader (_SOURCE ["airports" ].splitlines ())
153155 next (data ) # Skip header
154156 for station in data :
@@ -170,6 +172,11 @@ def get_surface_type(surface: str) -> str | None:
170172 return next ((key for key , items in SURFACE_TYPES .items () if surface in items ), None )
171173
172174
175+ def has_runways (station : dict ) -> bool :
176+ """Check if the station has runway information."""
177+ return "runways" in station and station ["runways" ] is not None
178+
179+
173180def add_runways (stations : dict , code_map : dict ) -> dict :
174181 """Add runway information to station if availabale."""
175182 data = csv .reader (_SOURCE ["runways" ].splitlines ())
@@ -190,13 +197,13 @@ def add_runways(stations: dict, code_map: dict) -> dict:
190197 }
191198 code = code_map .get (runway [1 ], runway [2 ])
192199 with suppress (KeyError ):
193- if "runways" in stations [code ]:
200+ if has_runways ( stations [code ]) :
194201 stations [code ]["runways" ].append (out )
195202 else :
196203 stations [code ]["runways" ] = [out ]
197204 # Sort runways by longest length and add missing nulls
198205 for code in stations :
199- if "runways" in stations [code ]:
206+ if has_runways ( stations [code ]) :
200207 stations [code ]["runways" ].sort (key = lambda x : x ["length_ft" ], reverse = True )
201208 else :
202209 stations [code ]["runways" ] = None
@@ -252,13 +259,8 @@ def update_station_info_date() -> None:
252259
253260def save_station_data (stations : dict ) -> None :
254261 """Save stations to JSON package data."""
255- json .dump (
256- stations ,
257- OUTPUT_PATH .open ("w" , encoding = "utf8" ),
258- sort_keys = True ,
259- indent = 1 ,
260- ensure_ascii = False ,
261- )
262+ with OUTPUT_PATH .open ("w" , encoding = "utf8" ) as fout :
263+ json .dump (stations , fout , sort_keys = True , indent = 1 , ensure_ascii = False )
262264
263265
264266def main () -> int :
0 commit comments