Spaces:
Running on Zero
Running on Zero
| #!/usr/bin/env python3 | |
| """Convert the Riverstone English/ language pack to JSON. | |
| Usage: | |
| python convert.py # English/ -> English_JSON/ | |
| python convert.py SRC DST # custom source / destination dirs | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| from converter.utils import is_skippable, toSafeEntityName | |
| from converter.docs import readLocalDoc | |
| from converter.sheets import readLocalSheet | |
| def _file_path_key(rel: Path) -> str: | |
| """Compute the normalised path key used by splitMarkers / avoidTransformMarkers.""" | |
| parts = [toSafeEntityName(p) for p in rel.with_suffix("").parts] | |
| return "/".join(parts) | |
| def main() -> None: | |
| base = Path(__file__).resolve().parent | |
| src_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else base / "English" | |
| dst_dir = Path(sys.argv[2]) if len(sys.argv) > 2 else base / "English_JSON" | |
| n_docx = n_xlsx = n_skipped = 0 | |
| for path in sorted(src_dir.rglob("*")): | |
| if not path.is_file(): | |
| continue | |
| ext = path.suffix.lower() | |
| if path.name.startswith("~$") or ext not in (".docx", ".xlsx"): | |
| continue | |
| if is_skippable(path.name): | |
| n_skipped += 1 | |
| continue | |
| rel = path.relative_to(src_dir) | |
| file_path = _file_path_key(rel) | |
| if ext == ".docx": | |
| data = readLocalDoc(path, file_path) | |
| n_docx += 1 | |
| else: | |
| data = readLocalSheet(path, file_path) | |
| n_xlsx += 1 | |
| out_path = dst_dir / rel.with_suffix(".json") | |
| out_path.parent.mkdir(parents=True, exist_ok=True) | |
| out_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8") | |
| total = n_docx + n_xlsx | |
| print(f"Converted {n_docx} docx + {n_xlsx} xlsx = {total} files ({n_skipped} skipped)") | |
| print(f"Output: {dst_dir}") | |
| if __name__ == "__main__": | |
| main() | |