Source code for jcmsuite.results.internal.models.bundle

# SPDX-License-Identifier: MIT
# Copyright (c) 2001-{{ COPYRIGHT_END_YEAR }} JCMwave GmbH

"""Ordered project-bundle solve models."""

from __future__ import annotations

from collections.abc import Iterator
from dataclasses import dataclass, field
from pathlib import Path

from .batch import BatchSolveEntry, BatchSolveFailure, BatchSolveResult
from .core import SolveExecutionLog, SolveResult


[docs] @dataclass(slots=True, frozen=True) class BundledSolveEntry: """Store one project result in an ordered project bundle. Parameters ---------- project_file : Path Path to the project file. result : SolveResult Result for the project. """ project_file: Path | None result: SolveResult _project_identity: str | None = field(default=None, repr=False, compare=False)
[docs] @dataclass(slots=True, frozen=True) class BundledBatchSolveEntry: """Store one batch entry for a complete ordered project bundle. Parameters ---------- index : int Position in the batched project keys. keys : dict[str, object] project keys used for this batch entry. result : BundledSolveResult | None Result for a successful ordered project bundle. failure : BatchSolveFailure | None Failure information when the solve did not succeed. project_files : tuple[Path, ...], optional project files in ordered project bundle order. The files remain available if the solve fails before it produces a project result. log : SolveExecutionLog | None, optional Execution log for a failed ordered project bundle. """ index: int keys: dict[str, object] result: BundledSolveResult | None failure: BatchSolveFailure | None project_files: tuple[Path | None, ...] = () log: SolveExecutionLog | None = field(default=None, repr=False)
[docs] @dataclass(slots=True, frozen=True) class BundledSolveResult: """Provide results for one ordered project bundle. Parameters ---------- entries : tuple[BundledSolveEntry, ...] project results in ordered project bundle order. Notes ----- `result[index]` returns one project result. Iteration yields project results in ordered project bundle order. """ entries: tuple[BundledSolveEntry, ...]
[docs] def __len__(self) -> int: """Return the number of project results in the bundle. Returns ------- int Number of projects in the ordered project bundle. """ return len(self.entries)
[docs] def __iter__(self) -> Iterator[SolveResult]: """Iterate over project results in ordered project bundle order. Returns ------- Iterator[SolveResult] Iterator over the project results. """ entry: BundledSolveEntry for entry in self.entries: yield entry.result
[docs] def __getitem__(self, index: int) -> SolveResult: """Return one project result by its position. Parameters ---------- index : int Position in the ordered project bundle. Negative positions count from the end of the bundle. Returns ------- SolveResult Result for the selected project. Raises ------ TypeError If `index` is not an integer. IndexError If `index` is outside the ordered project bundle. """ if not isinstance(index, int): raise TypeError("BundledSolveResult indexing requires an integer index.") return self.entries[index].result
@property def project_files(self) -> tuple[Path | None, ...]: """Return the project file paths in ordered project bundle order. Returns ------- tuple[Path, ...] Absolute project file paths. """ return tuple( ( None if entry.project_file is None else entry.project_file.expanduser().resolve() ) for entry in self.entries )
[docs] def project(self, selector: int) -> SolveResult: """Return one project result by its position. Parameters ---------- selector : int Position of the project in the ordered project bundle. Returns ------- SolveResult Result for the selected project. """ if not isinstance(selector, int): raise TypeError( "BundledSolveResult project selection requires an integer index." ) return self[selector]
[docs] @dataclass(slots=True, frozen=True) class BundledBatchSolveResult: """Provide project-first access to batched ordered project bundles. Parameters ---------- entries : tuple[BundledBatchSolveEntry, ...] Results in batched project keys order. Notes ----- List-style access is project-first. `result[index]` returns the `BatchSolveResult` for one project across all batched project keys. Iteration yields these project batch results in ordered project bundle order. """ entries: tuple[BundledBatchSolveEntry, ...] def _project_axis_files(self) -> tuple[Path | None, ...]: """Return the ordered project axis for this bundle batch. The bundle-batch public API is project-oriented even though the stored entries are batch-oriented. Successful bundle results already carry the public project axis directly; failed entries preserve the same axis via stored `project_files` metadata. """ entry: BundledBatchSolveEntry for entry in self.entries: if entry.result is not None: project_files: tuple[Path | None, ...] = entry.result.project_files if project_files: return project_files if entry.project_files: return tuple( ( None if project_file is None else project_file.expanduser().resolve() ) for project_file in entry.project_files ) return () def _require_project_axis_files(self) -> tuple[Path | None, ...]: """Return ordered project files for the public bundle axis.""" project_files: tuple[Path | None, ...] = self._project_axis_files() if not project_files: raise TypeError( "BundledBatchSolveResult cannot expose a project axis because no " "bundle project metadata is available; inspect `.entries` for " "collected failures." ) return project_files def _resolve_project_index(self, selector: int) -> int: """Resolve one project selector against the public bundle axis.""" project_files: tuple[Path | None, ...] = self._require_project_axis_files() if isinstance(selector, bool) or not isinstance(selector, int): raise TypeError( "BundledBatchSolveResult project selection requires an integer index." ) resolved_index: int = selector if resolved_index < 0: resolved_index += len(project_files) if resolved_index < 0 or resolved_index >= len(project_files): raise IndexError("BundledBatchSolveResult project index out of range.") return resolved_index
[docs] def __len__(self) -> int: """Return the number of projects in the ordered project bundle. Returns ------- int Number of available project positions. """ return len(self._project_axis_files())
[docs] def __iter__(self) -> Iterator[BatchSolveResult]: """Iterate over project batch results in bundle order. Returns ------- Iterator[BatchSolveResult] Iterator with one batch result for each project. """ project_index: int for project_index in range(len(self)): yield self[project_index]
[docs] def __getitem__(self, index: int) -> BatchSolveResult: """Return the batch result for one project position. Parameters ---------- index : int Position in the ordered project bundle. Negative positions count from the end of the bundle. Returns ------- BatchSolveResult Results for the selected project in batched project keys order. Raises ------ TypeError If `index` is not an integer or is a Boolean value. IndexError If `index` is outside the ordered project bundle. """ return self.project(index)
@property def project_files(self) -> tuple[Path | None, ...]: """Return the project files in ordered project bundle order. Returns ------- tuple[Path | None, ...] Absolute project file paths, or an empty tuple if no project information is available. """ return self._project_axis_files()
[docs] def project(self, selector: int) -> BatchSolveResult: """Return the batch results for one project in the bundle. Parameters ---------- selector : int Position of the project in the ordered project bundle. Returns ------- BatchSolveResult Results for the selected project in batched project keys order. The result includes failures that were collected during the solve. """ project_index: int = self._resolve_project_index(selector) selected_entries: list[BatchSolveEntry] = [] bundle_entry: BundledBatchSolveEntry for bundle_entry in self.entries: if bundle_entry.result is None: selected_entries.append( BatchSolveEntry( index=bundle_entry.index, keys=bundle_entry.keys, result=None, failure=bundle_entry.failure, _log=bundle_entry.log, ) ) continue selected_result: SolveResult = bundle_entry.result[project_index] selected_entries.append( BatchSolveEntry( index=bundle_entry.index, keys=bundle_entry.keys, result=selected_result, failure=None, _log=bundle_entry.log, ) ) return BatchSolveResult(entries=tuple(selected_entries))