-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_aircraft.py
More file actions
276 lines (219 loc) · 9.58 KB
/
sync_aircraft.py
File metadata and controls
276 lines (219 loc) · 9.58 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
Aircraft Registry Synchronization Script
This script fetches aircraft data from the Swiss BAZL (Federal Office of Civil Aviation)
and processes it into our aircraft registry format.
Usage:
python3 sync_aircraft.py
"""
import json
import csv
import io
import requests
from datetime import datetime, timezone
from typing import Dict, List, Optional
import jsonschema
import os
class AircraftSyncer:
"""Handles synchronization of aircraft data from BAZL."""
BAZL_ENDPOINT = "https://app02.bazl.admin.ch/web/bazl-backend/lfr/csv"
QUERY_PAYLOAD = {
"sort_list": "registration",
"language": "en",
"queryProperties": {
"aircraftStatus": [
"Registered",
"Reserved",
"Reservation Expired",
"Registration in Progress"
]
}
}
# CSV column mappings (note the spaces in column names)
COLUMN_MAPPING = {
"registration": " Registration",
"icao_aircraft_type": " ICAO Aircraft Type",
"aircraft_type": " Aircraft Type",
"mtom": " MTOM"
}
def __init__(self, overrides_file: Optional[str] = None):
"""Initialize the syncer with optional overrides file."""
self.overrides_file = overrides_file
self.overrides = self._load_overrides() if overrides_file else {}
def _load_overrides(self) -> Dict:
"""Load custom aircraft overrides from file."""
if not os.path.exists(self.overrides_file):
return {}
try:
with open(self.overrides_file, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, FileNotFoundError) as e:
print(f"Warning: Could not load overrides file {self.overrides_file}: {e}")
return {}
def fetch_aircraft_data(self) -> str:
"""Fetch raw CSV data from BAZL endpoint."""
print("Fetching aircraft data from BAZL...")
try:
response = requests.post(
self.BAZL_ENDPOINT,
json=self.QUERY_PAYLOAD,
headers={'Content-Type': 'application/json'},
timeout=30
)
response.raise_for_status()
# Handle UTF-16 encoding (check for BOM)
content = response.content
if content.startswith(b'\xff\xfe') or content.startswith(b'\xfe\xff'):
# UTF-16 with BOM
csv_text = content.decode('utf-16')
elif content.startswith(b'\xff\xfe\x00\x00') or content.startswith(b'\x00\x00\xfe\xff'):
# UTF-32 with BOM
csv_text = content.decode('utf-32')
else:
# Default to UTF-8
csv_text = content.decode('utf-8')
return csv_text
except requests.exceptions.RequestException as e:
raise Exception(f"Failed to fetch data from BAZL: {e}")
except UnicodeDecodeError as e:
raise Exception(f"Failed to decode CSV data: {e}")
def parse_csv_data(self, csv_content: str) -> List[Dict]:
"""Parse CSV content and extract relevant aircraft information."""
print("Parsing CSV data...")
aircraft_list = []
csv_reader = csv.DictReader(
io.StringIO(csv_content),
delimiter=';'
)
row_count = 0
for row in csv_reader:
row_count += 1
try:
# Extract relevant fields
registration = row.get(self.COLUMN_MAPPING["registration"], "").strip()
icao_type = row.get(self.COLUMN_MAPPING["icao_aircraft_type"], "").strip()
aircraft_type = row.get(self.COLUMN_MAPPING["aircraft_type"], "").strip()
mtom_str = row.get(self.COLUMN_MAPPING["mtom"], "").strip()
# Skip rows with missing essential data
if not registration or not icao_type or not aircraft_type:
continue
# Parse MTOM as integer, handle empty/invalid values
mtom = None
if mtom_str:
try:
mtom = int(mtom_str)
except ValueError:
pass # Keep as None if not a valid integer
aircraft_data = {
"registration": registration,
"icao_aircraft_type": icao_type,
"aircraft_type": aircraft_type,
"mtom": mtom
}
aircraft_list.append(aircraft_data)
except Exception as e:
print(f"Warning: Error processing row for {row.get(self.COLUMN_MAPPING['registration'], 'unknown')}: {e}")
continue
return aircraft_list
def apply_overrides(self, aircraft_list: List[Dict]) -> List[Dict]:
"""Apply custom overrides and add additional aircraft."""
if not self.overrides:
return aircraft_list
print(f"Applying {len(self.overrides)} custom overrides...")
# Create a lookup dict by registration for efficient updates
aircraft_dict = {aircraft["registration"]: aircraft for aircraft in aircraft_list}
# Apply overrides (skip comment fields that start with _)
for reg, override_data in self.overrides.items():
if reg.startswith('_'):
continue # Skip comment fields
if reg in aircraft_dict:
# Update existing aircraft
aircraft_dict[reg].update(override_data)
print(f"Updated aircraft {reg} with custom data")
else:
# Add new aircraft
new_aircraft = {"registration": reg}
new_aircraft.update(override_data)
aircraft_dict[reg] = new_aircraft
print(f"Added new aircraft {reg} from overrides")
return list(aircraft_dict.values())
def create_registry(self, aircraft_list: List[Dict]) -> Dict:
"""Create the final registry structure."""
# Sort by registration
aircraft_list.sort(key=lambda x: x["registration"])
registry = {
"version": "1.0.0",
"last_updated": datetime.now(timezone.utc).isoformat(),
"total_count": len(aircraft_list),
"aircraft": aircraft_list
}
return registry
def validate_registry(self, registry: Dict) -> bool:
"""Validate registry against schema."""
print("Validating registry against schema...")
try:
with open('schema.json', 'r', encoding='utf-8') as f:
schema = json.load(f)
jsonschema.validate(registry, schema)
print("✓ Registry validation successful")
return True
except jsonschema.ValidationError as e:
print(f"✗ Registry validation failed: {e}")
return False
except FileNotFoundError:
print("Warning: schema.json not found, skipping validation")
return True
except Exception as e:
print(f"Warning: Error during validation: {e}")
return True
def save_registry(self, registry: Dict, filename: str = "aircraft-staging.json"):
"""Save registry to file."""
print(f"Saving registry to {filename}...")
with open(filename, 'w', encoding='utf-8') as f:
json.dump(registry, f, indent=2, ensure_ascii=False)
print(f"✓ Saved {registry['total_count']} aircraft to {filename}")
def sync(self) -> bool:
"""Main synchronization method."""
try:
# Fetch data
csv_content = self.fetch_aircraft_data()
# Parse CSV
aircraft_list = self.parse_csv_data(csv_content)
if len(aircraft_list) == 0:
print(f"CSV download contained 0 aircraft, which cannot be correct. Aborting...")
exit(1)
else:
print(f"Parsed {len(aircraft_list)} aircraft from CSV")
# Apply overrides
aircraft_list = self.apply_overrides(aircraft_list)
# Create registry
registry = self.create_registry(aircraft_list)
# Validate
if not self.validate_registry(registry):
print("⚠ Continuing despite validation errors...")
# Save to staging
self.save_registry(registry)
print(f"✓ Synchronization completed successfully!")
print(f" Total aircraft: {registry['total_count']}")
print(f" Last updated: {registry['last_updated']}")
return True
except Exception as e:
print(f"✗ Synchronization failed: {e}")
return False
def main():
"""Main entry point."""
import argparse
parser = argparse.ArgumentParser(description='Sync aircraft data from BAZL')
parser.add_argument(
'--overrides',
help='Path to JSON file containing aircraft overrides',
default='aircraft-overrides.json'
)
args = parser.parse_args()
# Create syncer
syncer = AircraftSyncer(overrides_file=args.overrides)
# Run sync
success = syncer.sync()
exit(0 if success else 1)
if __name__ == "__main__":
main()