elliot-stack 1.0.13 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,173 @@
1
+ """
2
+ Pair filtered flights with the best shuttle option and output a ranked
3
+ markdown plans table.
4
+
5
+ Inputs:
6
+ --flights-json JSON list from filter_flights.py (file path or "-" for stdin)
7
+ --shuttles-json JSON dict with "shuttles" array (see references/shuttle_schedules.md)
8
+ --shuttle-costs Comma-separated AIRPORT:USD list (e.g. "ORD:60,IND:30"). Each
9
+ airport in your shuttle config must be listed here.
10
+ --tz-offsets Comma-separated AIRPORT:OFFSET_MIN list relative to home/origin
11
+ timezone (e.g. "ORD:-60,MDW:-60"). Airports omitted assume 0
12
+ (same timezone as the shuttle's `departs_local` zone).
13
+ --min-buffer-min Min minutes between shuttle arrival at airport and flight
14
+ departure. Default 90 (1.5h).
15
+ --max-wait-min Max acceptable wait at airport. Default 240 (4h).
16
+
17
+ Output: markdown table on stdout, one row per flight, sorted by viability
18
+ then total cost then departure time.
19
+
20
+ Timezones: shuttle entries record departure time in the home timezone
21
+ (`departs_local`) and arrival time in the airport's local timezone
22
+ (`arrives_local`). Pass --tz-offsets so this script can compare them on a
23
+ single timeline for buffer math.
24
+ """
25
+ import argparse
26
+ import json
27
+ import sys
28
+
29
+
30
+ def parse_kv_int(csv: str, value_label: str):
31
+ """Parse "KEY:VAL,KEY:VAL" into a dict[str,int]."""
32
+ out = {}
33
+ if not csv:
34
+ return out
35
+ for item in csv.split(","):
36
+ item = item.strip()
37
+ if not item:
38
+ continue
39
+ if ":" not in item:
40
+ print(f"ERROR: bad {value_label} entry {item!r} — expected KEY:VAL", file=sys.stderr)
41
+ sys.exit(2)
42
+ k, v = item.split(":", 1)
43
+ try:
44
+ out[k.strip()] = int(v.strip())
45
+ except ValueError:
46
+ print(f"ERROR: {value_label} {k!r} value {v!r} is not an int", file=sys.stderr)
47
+ sys.exit(2)
48
+ return out
49
+
50
+
51
+ def hm_to_min(hm: str) -> int:
52
+ h, m = hm.split(":")
53
+ return int(h) * 60 + int(m)
54
+
55
+
56
+ def viability(buffer_min: int, min_buffer: int, max_wait: int) -> str:
57
+ if buffer_min < min_buffer:
58
+ return "TOO_TIGHT"
59
+ if buffer_min > max_wait:
60
+ return "LONG_WAIT"
61
+ if buffer_min < min_buffer + 30:
62
+ return "TIGHT"
63
+ if buffer_min <= min_buffer + 90:
64
+ return "COMFORTABLE"
65
+ return "GENEROUS"
66
+
67
+
68
+ VIABILITY_RANK = {"COMFORTABLE": 0, "GENEROUS": 1, "TIGHT": 2, "LONG_WAIT": 3, "TOO_TIGHT": 4}
69
+ VIABILITY_NOTE = {
70
+ "COMFORTABLE": "Comfortable buffer",
71
+ "GENEROUS": "Comfortable, longer wait",
72
+ "TIGHT": "Tight - runs on shuttle being on time",
73
+ "LONG_WAIT": "Long airport wait",
74
+ "TOO_TIGHT": "Too tight - not viable",
75
+ }
76
+
77
+
78
+ def to_home_min(time_local: str, airport: str, tz_offsets: dict) -> int:
79
+ """Convert a local-airport HH:MM into home-timezone-equivalent minutes."""
80
+ return hm_to_min(time_local) - tz_offsets.get(airport, 0)
81
+
82
+
83
+ def best_shuttle_for_flight(flight, shuttles, min_buffer, max_wait, tz_offsets):
84
+ """Pick the shuttle with the best viability score for this flight."""
85
+ dep_airport = flight["from"]
86
+ flight_dep_min = to_home_min(flight["departs"], dep_airport, tz_offsets)
87
+ candidates = []
88
+ for s in shuttles:
89
+ if s.get("to") != dep_airport:
90
+ continue
91
+ arr = s.get("arrives_local", "")
92
+ if not arr:
93
+ continue
94
+ shuttle_arr_min = to_home_min(arr, dep_airport, tz_offsets)
95
+ buffer = flight_dep_min - shuttle_arr_min
96
+ if buffer < 0:
97
+ continue # shuttle arrives after flight leaves
98
+ v = viability(buffer, min_buffer, max_wait)
99
+ candidates.append((VIABILITY_RANK[v], buffer, v, s))
100
+ if not candidates:
101
+ return None, None, None
102
+ candidates.sort(key=lambda c: c[0])
103
+ _, buf, v, s = candidates[0]
104
+ return s, buf, v
105
+
106
+
107
+ def main() -> int:
108
+ p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
109
+ p.add_argument("--flights-json", required=True, help="Path to filter_flights.py output, or '-' for stdin")
110
+ p.add_argument("--shuttles-json", required=True)
111
+ p.add_argument("--shuttle-costs", default="",
112
+ help='Comma-separated "AIRPORT:USD" pairs (e.g. "ORD:60,IND:30")')
113
+ p.add_argument("--tz-offsets", default="",
114
+ help='Comma-separated "AIRPORT:OFFSET_MIN" pairs relative to home tz')
115
+ p.add_argument("--min-buffer-min", type=int, default=90)
116
+ p.add_argument("--max-wait-min", type=int, default=240)
117
+ args = p.parse_args()
118
+
119
+ shuttle_costs = parse_kv_int(args.shuttle_costs, "--shuttle-costs")
120
+ tz_offsets = parse_kv_int(args.tz_offsets, "--tz-offsets")
121
+
122
+ if args.flights_json == "-":
123
+ flights = json.load(sys.stdin)
124
+ else:
125
+ with open(args.flights_json, encoding="utf-8") as f:
126
+ flights = json.load(f)
127
+
128
+ with open(args.shuttles_json, encoding="utf-8") as f:
129
+ shuttles = json.load(f).get("shuttles", [])
130
+
131
+ rows = []
132
+ for fl in flights:
133
+ s, buf, v = best_shuttle_for_flight(
134
+ fl, shuttles, args.min_buffer_min, args.max_wait_min, tz_offsets,
135
+ )
136
+ if s is None:
137
+ continue
138
+ shuttle_cost = shuttle_costs.get(fl["from"], 0)
139
+ total = (fl["price"] or 0) + shuttle_cost
140
+ rows.append({
141
+ "flight": fl, "shuttle": s, "buffer_min": buf, "viability": v,
142
+ "shuttle_cost": shuttle_cost, "total": total,
143
+ })
144
+
145
+ # Sort: viability tier, then priority band, then total cost, then departure time
146
+ rows.sort(key=lambda r: (
147
+ VIABILITY_RANK[r["viability"]],
148
+ r.get("flight", {}).get("priority_band", 0),
149
+ r["total"],
150
+ r["flight"]["departs"],
151
+ ))
152
+
153
+ if not rows:
154
+ print("(no viable flight+shuttle pairings)")
155
+ return 0
156
+
157
+ print("| # | Date | Flight | From | To | Departs | Arrives | Shuttle | Shuttle Departs | Buffer | Flight $ | Shuttle $ | **Total** | Notes |")
158
+ print("|---|---|---|---|---|---|---|---|---|---|---|---|---|---|")
159
+ for i, r in enumerate(rows, 1):
160
+ f = r["flight"]; s = r["shuttle"]
161
+ depart_label = s.get("departs_local") or s.get("departs_et") or "?"
162
+ print(
163
+ f"| {i} | {f['date']} | {f['flight']} | {f['from']} | {f['to']} | "
164
+ f"{f['departs']} | {f['arrives']} | {s.get('company','?')} | "
165
+ f"{depart_label} | {r['buffer_min']} min | "
166
+ f"${f['price']} | ${r['shuttle_cost']} | **${r['total']}** | "
167
+ f"{VIABILITY_NOTE[r['viability']]} |"
168
+ )
169
+ return 0
170
+
171
+
172
+ if __name__ == "__main__":
173
+ sys.exit(main())