#!/usr/bin/env python3 # -*- coding: utf-8 -*- """แตก resource/data/schedules.json → resource/data/scheduleslist/schedules.json (1 ไฟล์ = 1 เส้นทาง) และเขียน schedules.json ให้เป็น "สารบัญ" (index) ชี้ไปไฟล์ย่อย ทำไม: ไฟล์เดียวรวม 14 สายอ่านยาก/แก้ยาก และเวลาแก้ทีละสายต้องเปิดทั้งไฟล์ → แยกเป็นไฟล์ต่อสาย รูปแบบไฟล์: scheduleslist/schedules101.json = { "schema": 2, "route_id": "101", "generated_from": "schedules.json", "station_order": [...], "directions": [ ... ] } (เนื้อในเหมือนเดิมเป๊ะ) schedules.json (สารบัญ) = { "schema": 2, "index": true, "dir": "scheduleslist/", "routes": [ { "route_id": "101", "file": "schedules101.json", ... } ] } รัน: python resource/tools/split-schedules.py # เขียนไฟล์จริง python resource/tools/split-schedules.py --check # ดูผลก่อน ไม่เขียน หมายเหตุ: ถ้า schedules.json เป็นสารบัญอยู่แล้ว คำสั่งนี้จะไม่ทำอะไร (กันเขียนทับไฟล์ย่อย) — ใช้ merge-schedules.py ย้อนกลับ """ import argparse import json import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[2] DATA = ROOT / "resource" / "data" LIST_DIR = DATA / "scheduleslist" def jdump(obj): return json.dumps(obj, ensure_ascii=False, indent=2).replace("\n", "\r\n") + "\r\n" def main(): ap = argparse.ArgumentParser() ap.add_argument("--check", action="store_true") args = ap.parse_args() src = DATA / "schedules.json" if not src.exists(): print("ไม่พบ schedules.json"); return 1 doc = json.loads(src.read_text(encoding="utf-8")) if doc.get("index") or "directions" not in json.dumps(doc)[:200]: if doc.get("index"): print("schedules.json เป็นสารบัญอยู่แล้ว — ไม่ต้องแตกไฟล์ซ้ำ") print("(ถ้าต้องการย้อนกลับเป็นไฟล์เดียว ใช้ resource/tools/merge-schedules.py)") return 0 routes = doc.get("routes") or [] if not routes: print("ไม่พบ routes[] ในไฟล์"); return 1 LIST_DIR.mkdir(exist_ok=True) index_rows = [] for r in routes: rid = str(r["route_id"]) sub = { "schema": doc.get("schema", 2), "route_id": r["route_id"], "generated_from": "schedules.json", "station_order": r.get("station_order", []), "directions": r.get("directions", []), } fname = "schedules" + rid + ".json" if not args.check: (LIST_DIR / fname).write_text(jdump(sub), encoding="utf-8", newline="") counts = {"n_directions": len(sub["directions"]), "n_stops": sum(len(d.get("stops") or []) for d in sub["directions"]), "n_trips": sum(len(s.get("times") or []) for d in sub["directions"] for s in (d.get("stops") or []))} index_rows.append({"route_id": r["route_id"], "file": fname, **counts}) index = { "schema": doc.get("schema", 2), "index": True, "note": "สารบัญชี้ไป scheduleslist/schedules.json (1 ไฟล์ = 1 เส้นทาง) — แก้ไฟล์ย่อยนั้นโดยตรง หรือใช้ resource/data/schedules-editor.html", "dir": "scheduleslist/", "routes": index_rows, } if not args.check: src.write_text(jdump(index), encoding="utf-8", newline="") print(("ตรวจเท่านั้น: " if args.check else "เขียนแล้ว: ") + f"{len(index_rows)} ไฟล์ใน scheduleslist/ + สารบัญ schedules.json") for row in index_rows: print(f" {row['file']:<26} ทิศ {row['n_directions']:>2} · จุด {row['n_stops']:>2} · เที่ยว {row['n_trips']:>3}") return 0 if __name__ == "__main__": sys.exit(main())