Resolve [YEAR] separately for template read and write destinations.
Some checks failed
Mirror to Gitea / mirror (push) Has been cancelled
Some checks failed
Mirror to Gitea / mirror (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,7 +6,6 @@ import csv
|
||||
import shutil
|
||||
import tempfile
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import tomllib
|
||||
@@ -33,6 +32,7 @@ backend = "com"
|
||||
|
||||
|
||||
NEW_VALUE = ""
|
||||
YEAR_TOKEN = "[YEAR]"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -49,15 +49,61 @@ 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,
|
||||
source_dir: Path,
|
||||
dest_dir: Path,
|
||||
pattern: str,
|
||||
) -> tuple[str, Path]:
|
||||
last_workbook = find_last_workbook(files)
|
||||
path_of_the_year = Path(str(source_dir).replace("2025", str(datetime.now().year)))
|
||||
return last_workbook, path_of_the_year / pattern.replace(
|
||||
return last_workbook, dest_dir / pattern.replace(
|
||||
"[SERIAL]", batch_serial.split("/")[0]
|
||||
)
|
||||
|
||||
@@ -159,13 +205,20 @@ def create_datasheet(
|
||||
return CreateResult(ok=False, message=message)
|
||||
|
||||
row = matches[0]
|
||||
path_ = Path(row["dir"])
|
||||
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 path_.glob(search_pattern) if x.is_file()]
|
||||
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 {path_}"
|
||||
message = f"No workbooks matching {search_pattern!r} in {read_dir}"
|
||||
log.error("Create failed: %s (%s)", message, ctx)
|
||||
return CreateResult(
|
||||
ok=False,
|
||||
@@ -174,7 +227,7 @@ def create_datasheet(
|
||||
)
|
||||
|
||||
ref_workbook_name, new_workbook_name = get_workbook_names(
|
||||
files, batch_serial, path_, pattern
|
||||
files, batch_serial, write_dir, pattern
|
||||
)
|
||||
template = Path(ref_workbook_name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user