Add cross-platform OOXML backend alongside Windows Excel COM
Linux/macOS default to surgical zip edits that clear/set cells while preserving queries, styles, and other non-worksheet parts. Windows keeps the COM + add-in path. Paths remapped via XLSX_CLEAN_ROOT; pywin32 is Windows-only. Co-authored-by: Diaco Soltanpour <s-diaco@users.noreply.github.com>
This commit is contained in:
63
src/xlsx_clean/paths.py
Normal file
63
src/xlsx_clean/paths.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""Config and path helpers for cross-platform workbook roots."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
PACKAGE_DIR = Path(__file__).resolve().parent
|
||||
|
||||
# Historical Windows root used in file_data.csv
|
||||
DEFAULT_OPENCLOUD_ROOT = Path(r"D:\OpenCloud")
|
||||
OPENCLOUD_MARKER = "OpenCloud"
|
||||
|
||||
|
||||
def package_file(name: str) -> Path:
|
||||
return PACKAGE_DIR / name
|
||||
|
||||
|
||||
def get_opencloud_root() -> Path:
|
||||
"""Workbook tree root from XLSX_CLEAN_ROOT, else D:\\OpenCloud on Windows."""
|
||||
env = os.environ.get("XLSX_CLEAN_ROOT")
|
||||
if env:
|
||||
return Path(env)
|
||||
return DEFAULT_OPENCLOUD_ROOT
|
||||
|
||||
|
||||
def remap_opencloud_path(path_str: str, root: Path | None = None) -> Path:
|
||||
"""Map a CSV path under OpenCloud onto the configured root.
|
||||
|
||||
Examples:
|
||||
D:\\OpenCloud\\inco\\... + root=/data -> /data/inco/...
|
||||
Relative paths without OpenCloud are returned as-is (Path).
|
||||
"""
|
||||
if root is None:
|
||||
root = get_opencloud_root()
|
||||
|
||||
normalized = path_str.replace("\\", "/")
|
||||
# Case-insensitive marker search
|
||||
lower = normalized.lower()
|
||||
marker = f"{OPENCLOUD_MARKER.lower()}/"
|
||||
idx = lower.find(marker)
|
||||
if idx != -1:
|
||||
rel = normalized[idx + len(marker) :]
|
||||
return root / rel
|
||||
|
||||
# Already relative to root-style path without drive
|
||||
return Path(path_str)
|
||||
|
||||
|
||||
def default_backend() -> str:
|
||||
return "com" if sys.platform == "win32" else "ooxml"
|
||||
|
||||
|
||||
def resolve_backend(requested: str | None) -> str:
|
||||
backend = (requested or default_backend()).lower()
|
||||
if backend not in {"com", "ooxml"}:
|
||||
raise ValueError(f"Unknown backend {backend!r}; use 'com' or 'ooxml'")
|
||||
if backend == "com" and sys.platform != "win32":
|
||||
raise RuntimeError(
|
||||
"COM backend requires Windows with Microsoft Excel and pywin32"
|
||||
)
|
||||
return backend
|
||||
Reference in New Issue
Block a user