"""Core business logic for xlsx_clean.""" from __future__ import annotations import csv import shutil import tempfile from dataclasses import dataclass from pathlib import Path import tomllib from xlsx_clean.app_logging import get_logger from xlsx_clean.paths import ( default_config_path, package_file, resolve_backend, ) DEFAULT_CONFIG_CONTENT = """\ # Configuration for xlsx_clean # backend options: # - "com": Use Microsoft Excel COM (Windows only, keeps Excel visible) # - "ooxml": Surgical zip edit (Cross-platform, very fast, preserves non-worksheet data) backend = "com" [sets] "Kimia Razi" = "D:\\\\OpenCloud\\\\Kimia Razi" "inco" = "D:\\\\OpenCloud\\\\inco" """ NEW_VALUE = "" YEAR_TOKEN = "[YEAR]" @dataclass(frozen=True) class CreateResult: ok: bool message: str template: Path | None = None destination: Path | None = None backend: str | None = None skipped: bool = False def find_last_workbook(files: list[str]) -> str: return sorted(files)[-1] def _split_year_placeholder(dir_with_placeholder: Path) -> tuple[Path, tuple[str, ...]] | None: parts = dir_with_placeholder.parts if YEAR_TOKEN not in parts: return None idx = parts.index(YEAR_TOKEN) return Path(*parts[:idx]), parts[idx + 1 :] def _digit_year_dirs(base: Path) -> list[Path]: if not base.is_dir(): return [] return [c for c in base.iterdir() if c.is_dir() and c.name.isdigit()] def resolve_year_dir(dir_with_placeholder: Path, pattern: str) -> Path: """Largest digit year dir that has matching template sheets for this ink.""" split = _split_year_placeholder(dir_with_placeholder) if split is None: return dir_with_placeholder base, suffix = split search_pattern = pattern.replace("[SERIAL]", "*") years: list[int] = [] for child in _digit_year_dirs(base): ink_dir = child.joinpath(*suffix) if suffix else child if any(p.is_file() for p in ink_dir.glob(search_pattern)): years.append(int(child.name)) if not years: raise FileNotFoundError( f"No year directory under {base} with workbooks matching {search_pattern!r}" ) resolved = base / str(max(years)) return resolved.joinpath(*suffix) if suffix else resolved def resolve_write_year_dir(dir_with_placeholder: Path) -> Path: """Largest digit year dir under the QC parent (existence only, no sheet check).""" split = _split_year_placeholder(dir_with_placeholder) if split is None: return dir_with_placeholder base, suffix = split years = [int(c.name) for c in _digit_year_dirs(base)] if not years: raise FileNotFoundError(f"No year directory under {base}") resolved = base / str(max(years)) return resolved.joinpath(*suffix) if suffix else resolved def get_workbook_names( files: list[str], batch_serial: str, dest_dir: Path, pattern: str, ) -> tuple[str, Path]: last_workbook = find_last_workbook(files) return last_workbook, dest_dir / pattern.replace( "[SERIAL]", batch_serial.split("/")[0] ) def load_config() -> tuple[list[str], list[dict], dict]: config_path = default_config_path() if not config_path.exists(): config_path.parent.mkdir(parents=True, exist_ok=True) config_path.write_text(DEFAULT_CONFIG_CONTENT, encoding="utf-8") with open(config_path, "rb") as f: config = tomllib.load(f) strings_path = package_file("strings.txt") with open(strings_path, encoding="utf-8") as f: content = [line.strip() for line in f.readlines()] csv_path = package_file("file_data.csv") rows = [] set_roots = config.get("sets", {}) with open(csv_path, encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: set_name = row["set_name"] rel_dir = row["dir"].replace("\\", "/") row["rel_dir"] = rel_dir root_str = set_roots.get(set_name, "") if root_str: row["dir"] = str(Path(root_str) / rel_dir) else: row["dir"] = str(Path(rel_dir)) rows.append(row) return content, rows, config def list_sets(rows: list[dict]) -> list[str]: return list(dict.fromkeys(r["set_name"] for r in rows)) import re def _sort_key(name: str): match = re.search(r'\d+', name) num = int(match.group()) if match else 0 return (num, name) def list_ink_colors(rows: list[dict], selected_set: str) -> list[str]: inks = (Path(r["dir"]).stem for r in rows if r["set_name"] == selected_set) return sorted(inks, key=_sort_key) def create_datasheet( selected_set: str, selected_dir: str, batch_serial: str, backend: str | None = None, rows: list[dict] | None = None, addin_paths: list[str] | None = None, config: dict | None = None, ) -> CreateResult: log = get_logger() ctx = f"set={selected_set!r} ink={selected_dir!r} serial={batch_serial!r}" if not selected_set: message = "Select a set." log.error("Create failed: %s (%s)", message, ctx) return CreateResult(ok=False, message=message) if not selected_dir: message = "Select an ink color." log.error("Create failed: %s (%s)", message, ctx) return CreateResult(ok=False, message=message) if not batch_serial or not str(batch_serial).strip(): message = "Enter a serial." log.error("Create failed: %s (%s)", message, ctx) return CreateResult(ok=False, message=message) batch_serial = str(batch_serial).strip() ctx = f"set={selected_set!r} ink={selected_dir!r} serial={batch_serial!r}" if config is None: content, loaded_rows, config = load_config() if rows is None: rows = loaded_rows if addin_paths is None: addin_paths = content[3:5] if len(content) >= 5 else [] try: resolved_backend = resolve_backend(backend, config.get("backend")) except (ValueError, RuntimeError) as exc: log.error("Create failed: %s (%s)", exc, ctx) return CreateResult(ok=False, message=str(exc)) ctx = f"{ctx} backend={resolved_backend}" matches = [r for r in rows if r["set_name"] == selected_set and r["dir"].endswith(selected_dir)] if not matches: message = f"No config row for set={selected_set!r} ink={selected_dir!r}." log.error("Create failed: %s (%s)", message, ctx) return CreateResult(ok=False, message=message) row = matches[0] pattern = row["pattern"] placeholder_dir = Path(row["dir"]) try: read_dir = resolve_year_dir(placeholder_dir, pattern) write_dir = resolve_write_year_dir(placeholder_dir) except FileNotFoundError as exc: message = str(exc) log.error("Create failed: %s (%s)", message, ctx) return CreateResult(ok=False, message=message, backend=resolved_backend) search_pattern = pattern.replace("[SERIAL]", "*") files = [str(x) for x in read_dir.glob(search_pattern) if x.is_file()] if not files: message = f"No workbooks matching {search_pattern!r} in {read_dir}" log.error("Create failed: %s (%s)", message, ctx) return CreateResult( ok=False, message=message, backend=resolved_backend, ) ref_workbook_name, new_workbook_name = get_workbook_names( files, batch_serial, write_dir, pattern ) template = Path(ref_workbook_name) if new_workbook_name.is_file(): message = f"Destination already exists, skipped: {new_workbook_name}" log.info( "Create skipped: %s (template=%s destination=%s %s)", message, template, new_workbook_name, ctx, ) return CreateResult( ok=True, skipped=True, message=message, template=template, destination=new_workbook_name, backend=resolved_backend, ) cells_to_clear = row["cells_to_clear"] notes_cell = row["notes_cell"] serial_cell = row["serial_cell"] try: if resolved_backend == "com": from xlsx_clean.com_backend import clean_workbook_com with tempfile.TemporaryDirectory(prefix="xlsx_clean_") as tmp: temp_workbook = Path(tmp) / "temp_workbook.xlsx" shutil.copyfile(ref_workbook_name, temp_workbook) clean_workbook_com( source=temp_workbook, destination=new_workbook_name, cells_to_clear=cells_to_clear, notes_cell=notes_cell, serial_cell=serial_cell, batch_serial=batch_serial, addin_paths=addin_paths or [], notes_value=NEW_VALUE, ) else: from xlsx_clean.ooxml_backend import clean_workbook_ooxml clean_workbook_ooxml( source=ref_workbook_name, destination=new_workbook_name, cells_to_clear=cells_to_clear, notes_cell=notes_cell, serial_cell=serial_cell, batch_serial=batch_serial, notes_value=NEW_VALUE, ) except Exception as exc: # noqa: BLE001 - surface to CLI/UI message = f"Failed to create workbook: {exc}" log.exception( "Create failed: %s (template=%s destination=%s %s)", message, template, new_workbook_name, ctx, ) return CreateResult( ok=False, message=message, template=template, destination=new_workbook_name, backend=resolved_backend, ) message = f"Wrote {new_workbook_name}" log.info( "Create succeeded: %s (template=%s destination=%s %s)", message, template, new_workbook_name, ctx, ) return CreateResult( ok=True, message=message, template=template, destination=new_workbook_name, backend=resolved_backend, )