| |
| """ |
| parse_design_files.py — Extract high-fidelity structured data from OpenRocket (.ork) |
| and RockSim (.rkt) files using the rocketsmith API. |
| |
| Output |
| ------ |
| source/designs/files/json/ork/{id}.json |
| source/designs/files/json/rkt/{id}.json |
| """ |
|
|
| import json |
| import logging |
| import sys |
| from pathlib import Path |
| from rocketsmith.openrocket.components import inspect_rocket_file |
| from rocketsmith.openrocket.utils import get_openrocket_path |
|
|
| |
| |
| |
|
|
| logging.basicConfig( |
| level=logging.INFO, |
| format="%(asctime)s %(levelname)s %(message)s", |
| handlers=[logging.StreamHandler(sys.stdout)], |
| ) |
| log = logging.getLogger(__name__) |
|
|
| |
| |
| |
|
|
| def main(): |
| try: |
| jar_path = get_openrocket_path() |
| log.info(f"Using OpenRocket JAR at: {jar_path}") |
| except FileNotFoundError as e: |
| log.error(f"Could not find OpenRocket JAR: {e}") |
| sys.exit(1) |
|
|
| root_dir = Path("source/designs/files") |
| dst_dir = root_dir / "json" |
| dst_dir.mkdir(parents=True, exist_ok=True) |
| |
| formats = ['ork', 'rkt'] |
| |
| for fmt in formats: |
| src_dir = root_dir / fmt |
| |
| if not src_dir.exists(): |
| log.warning(f"Source directory {src_dir} does not exist. Skipping.") |
| continue |
| |
| files = sorted(list(src_dir.glob(f"*.{fmt}"))) |
| total = len(files) |
| log.info(f"Processing {total} {fmt} files...") |
| |
| ok = failed = skipped = 0 |
| |
| for i, f in enumerate(files): |
| |
| dst_file = dst_dir / f"{f.stem}.json" |
| |
| if dst_file.exists(): |
| skipped += 1 |
| continue |
|
|
| try: |
| result = inspect_rocket_file(f, jar_path) |
| |
| result["file_extension"] = fmt |
| |
| with dst_file.open('w', encoding='utf-8') as out: |
| json.dump(result, out, indent=2, ensure_ascii=False) |
| ok += 1 |
| except Exception as e: |
| log.error(f"Failed to parse {f.name}: {e}") |
| failed += 1 |
| |
| if (i + 1) % 50 == 0 or (i + 1) == total: |
| log.info(f"Progress ({fmt}): {i+1}/{total} — ok={ok} skipped={skipped} failed={failed}") |
|
|
| log.info("Finished processing all design files.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|