Files
xlsx-clean/src/xlsx_clean/paths.py
diaco 95e0acbe71
Some checks failed
Mirror to Gitea / mirror (push) Has been cancelled
Add GUI refactor, modern theme, and configuration improvements
2026-08-10 14:28:49 +03:30

52 lines
1.6 KiB
Python

"""Config and path helpers for cross-platform workbook roots."""
from __future__ import annotations
import os
import sys
from pathlib import Path
def _package_dir() -> Path:
"""Directory containing package data (CSV/txt), including PyInstaller bundles."""
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
bundled = Path(sys._MEIPASS) / "xlsx_clean"
if bundled.is_dir():
return bundled
return Path(sys._MEIPASS)
return Path(__file__).resolve().parent
PACKAGE_DIR = _package_dir()
def package_file(name: str) -> Path:
return PACKAGE_DIR / name
def default_config_path() -> Path:
"""Resolve the default location for the TOML configuration file."""
if getattr(sys, "frozen", False):
return Path(sys.executable).resolve().parent / "config.toml"
return Path.home() / ".xlsx-clean" / "config.toml"
def default_backend() -> str:
return "com" if sys.platform == "win32" else "ooxml"
def resolve_backend(requested: str | None, config_backend: str | None = None) -> str:
"""Resolve backend using explicitly requested, config file, or system default."""
if requested:
backend = requested.lower()
elif config_backend:
backend = config_backend.lower()
else:
backend = 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