Upload convert.py
Browse files- convert.py +61 -0
convert.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Convert the Riverstone English/ language pack to JSON.
|
| 3 |
+
|
| 4 |
+
Usage:
|
| 5 |
+
python convert.py # English/ -> English_JSON/
|
| 6 |
+
python convert.py SRC DST # custom source / destination dirs
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
import sys
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
from converter.utils import is_skippable, toSafeEntityName
|
| 14 |
+
from converter.docs import readLocalDoc
|
| 15 |
+
from converter.sheets import readLocalSheet
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _file_path_key(rel: Path) -> str:
|
| 19 |
+
"""Compute the normalised path key used by splitMarkers / avoidTransformMarkers."""
|
| 20 |
+
parts = [toSafeEntityName(p) for p in rel.with_suffix("").parts]
|
| 21 |
+
return "/".join(parts)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def main() -> None:
|
| 25 |
+
base = Path(__file__).resolve().parent
|
| 26 |
+
src_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else base / "English"
|
| 27 |
+
dst_dir = Path(sys.argv[2]) if len(sys.argv) > 2 else base / "English_JSON"
|
| 28 |
+
|
| 29 |
+
n_docx = n_xlsx = n_skipped = 0
|
| 30 |
+
|
| 31 |
+
for path in sorted(src_dir.rglob("*")):
|
| 32 |
+
if not path.is_file():
|
| 33 |
+
continue
|
| 34 |
+
ext = path.suffix.lower()
|
| 35 |
+
if path.name.startswith("~$") or ext not in (".docx", ".xlsx"):
|
| 36 |
+
continue
|
| 37 |
+
if is_skippable(path.name):
|
| 38 |
+
n_skipped += 1
|
| 39 |
+
continue
|
| 40 |
+
|
| 41 |
+
rel = path.relative_to(src_dir)
|
| 42 |
+
file_path = _file_path_key(rel)
|
| 43 |
+
|
| 44 |
+
if ext == ".docx":
|
| 45 |
+
data = readLocalDoc(path, file_path)
|
| 46 |
+
n_docx += 1
|
| 47 |
+
else:
|
| 48 |
+
data = readLocalSheet(path, file_path)
|
| 49 |
+
n_xlsx += 1
|
| 50 |
+
|
| 51 |
+
out_path = dst_dir / rel.with_suffix(".json")
|
| 52 |
+
out_path.parent.mkdir(parents=True, exist_ok=True)
|
| 53 |
+
out_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
| 54 |
+
|
| 55 |
+
total = n_docx + n_xlsx
|
| 56 |
+
print(f"Converted {n_docx} docx + {n_xlsx} xlsx = {total} files ({n_skipped} skipped)")
|
| 57 |
+
print(f"Output: {dst_dir}")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
main()
|