Step 3: Generate category-split JSON files for data layer.
(standards: list[dict])
| 328 | |
| 329 | |
| 330 | def generate_json(standards: list[dict]): |
| 331 | """Step 3: Generate category-split JSON files for data layer.""" |
| 332 | print("\n=== Step 3: Generating data layer JSON files ===") |
| 333 | |
| 334 | if not standards: |
| 335 | standards = _load_from_cache() |
| 336 | if not standards: |
| 337 | return |
| 338 | |
| 339 | # Classify all standards (in case loaded from cache without classification) |
| 340 | for s in standards: |
| 341 | if "category" not in s or s["category"] == "other": |
| 342 | s["category"] = classify_standard(s) |
| 343 | |
| 344 | # Group by category |
| 345 | categories = {} |
| 346 | for s in standards: |
| 347 | cat = s.get("category", "other") |
| 348 | if cat not in categories: |
| 349 | categories[cat] = [] |
| 350 | categories[cat].append(s) |
| 351 | |
| 352 | DATA_DIR.mkdir(parents=True, exist_ok=True) |
| 353 | now = datetime.now().strftime("%Y-%m-%d") |
| 354 | |
| 355 | # Generate per-category files |
| 356 | for cat_key, cat_standards in sorted(categories.items()): |
| 357 | cat_name = CATEGORY_NAMES.get(cat_key, {"en": cat_key, "zh": cat_key}) |
| 358 | entries = [] |
| 359 | for s in cat_standards: |
| 360 | entry = { |
| 361 | "id": f"eu_mdr-hs-{s['number'].replace(' ', '-').replace(':', '-').replace('/', '-').lower()}", |
| 362 | "number": s["number"], |
| 363 | "title": s.get("title", ""), |
| 364 | "scope": "eu_mdr", |
| 365 | "status": "active", |
| 366 | "source_url": "https://eur-lex.europa.eu/eli/dec_impl/2021/1182", |
| 367 | "source_verified": now, |
| 368 | "category": cat_key, |
| 369 | } |
| 370 | if s.get("amendments"): |
| 371 | entry["amendments"] = s["amendments"] |
| 372 | if s.get("needs_review"): |
| 373 | entry["needs_review"] = True |
| 374 | entries.append(entry) |
| 375 | |
| 376 | output = { |
| 377 | "category": cat_key, |
| 378 | "category_name": cat_name, |
| 379 | "regulation": "eu_mdr", |
| 380 | "source": "Implementing Decision (EU) 2021/1182 (consolidated)", |
| 381 | "source_url": "https://eur-lex.europa.eu/eli/dec_impl/2021/1182", |
| 382 | "last_updated": now, |
| 383 | "count": len(entries), |
| 384 | "entries": entries, |
| 385 | } |
| 386 | |
| 387 | out_file = DATA_DIR / f"standards-{cat_key}.json" |
no test coverage detected