Skip to content

pyflightstream.workspace.excel_sync (stable)

Generated from the docstrings at build time: every one of the 14 names in the __all__ of pyflightstream.workspace.excel_sync has an entry below. The names a guide or an example uses come first; the others follow under Advanced.

Name-based optional Excel synchronization; no solver or live Excel dependency.

Advanced

pyflightstream.workspace.excel_sync.ApplyResult dataclass

ApplyResult(
    changes: list[Change],
    mappings: list[DictionaryEntry],
    headers: list[str],
    baseline: dict[str, str],
    written: list[str] = list(),
    backups: dict[str, str] = dict(),
    error: str = "",
)

Only cell deltas go back to VBA; the open workbook is never replaced.

pyflightstream.workspace.excel_sync.Cell dataclass

Cell(value: str = '', formula: bool = False)

One literal or formula captured from the current workbook.

pyflightstream.workspace.excel_sync.Change dataclass

Change(
    action: str,
    matrix: str,
    pol: str,
    ascii_name: str,
    excel_header: str,
    before: str,
    after: str,
    row: int = -1,
    column: int = -1,
    detail: str = "",
)

One visible preview decision with its identity and before/after values.

pyflightstream.workspace.excel_sync.DictionaryEntry dataclass

DictionaryEntry(
    ascii_name: str,
    excel_header: str,
    status: str = "MAPPED",
)

An exact ASCII-to-Excel name mapping or an Excel-only column declaration.

pyflightstream.workspace.excel_sync.ExcelSyncError

Bases: PyflightstreamError, ValueError

A preview or apply cannot preserve an unambiguous matrix meaning.

pyflightstream.workspace.excel_sync.SUPPORTED_COLUMNS module-attribute

SUPPORTED_COLUMNS = tuple(
    dict.fromkeys(
        name
        for layout in SUPPORTED_LAYOUTS
        for name in layout
    )
)

pyflightstream.workspace.excel_sync.SyncPreview dataclass

SyncPreview(
    direction: Literal["read", "write"],
    workspace: str,
    workbook_digest: str,
    file_digests: dict[str, str | None],
    changes: list[Change],
    mappings: list[DictionaryEntry],
    headers: list[str],
    baseline: dict[str, str],
    file_outputs: dict[str, str] = dict(),
    token: str = (lambda: uuid.uuid4().hex)(),
    selection: list[str] | None = None,
)

A captured synchronization batch that must be reviewed before applying.

counts property

counts: dict[str, int]

Count actual preview actions, including conflicts and Excel-only columns.

applicable property

applicable: bool

Return whether the batch contains no invalid or conflicting changes.

pyflightstream.workspace.excel_sync.WorkbookSnapshot dataclass

WorkbookSnapshot(
    headers: list[str],
    rows: list[list[Cell]],
    dictionary: list[DictionaryEntry],
    baseline: dict[str, str] = dict(),
)

Values captured from the live workbook, including unsaved edits.

Rows follow headers in their current order. Custom cells and formulas stay opaque. Baseline keys are JSON [MATRIX, POL, ASCII_NAME] arrays.

digest

digest() -> str

Hash every captured cell, mapping and baseline value for freshness.

pyflightstream.workspace.excel_sync.apply_preview

apply_preview(
    preview: SyncPreview, snapshot: WorkbookSnapshot
) -> ApplyResult

Apply a reviewed batch after freshness checks; keep originals per file.

A write failure returns completed files and their recovery paths. There is deliberately no claim of a filesystem-wide atomic transaction.

Parameters:

Name Type Description Default
preview SyncPreview

The reviewed preview.

required
snapshot WorkbookSnapshot

The workbook as it is now, checked against the preview.

required

Returns:

Type Description
ApplyResult

The files written and their backups, the headers and mappings returned to the workbook, and the error of a partial write.

Raises:

Type Description
ExcelSyncError

If the preview holds INVALID or CONFLICT rows, or the workbook or a matrix changed after the preview. A matrix that changes during the apply is not raised: the result's error names it, with the files already written and their backups.

pyflightstream.workspace.excel_sync.check_snapshot

check_snapshot(snapshot: WorkbookSnapshot) -> None

Validate the Dictionary and the identity of every Runs row.

Parameters:

Name Type Description Default
snapshot WorkbookSnapshot

The workbook snapshot.

required

Raises:

Type Description
ExcelSyncError

If the Dictionary is invalid, or a Runs row has no MATRIX and POL text or repeats one.

pyflightstream.workspace.excel_sync.dictionary_mapping

dictionary_mapping(
    snapshot: WorkbookSnapshot,
) -> dict[str, str]

Return the validated ASCII-to-Excel mapping, without guessing names or positions.

Parameters:

Name Type Description Default
snapshot WorkbookSnapshot

The workbook snapshot.

required

Returns:

Type Description
dict of str to str

ASCII name to Excel header.

Raises:

Type Description
ExcelSyncError

If the Runs headers are blank, repeated or narrower than a row, or the Dictionary is ambiguous, misses MATRIX or POL, or names a field the matrix reader does not support.

pyflightstream.workspace.excel_sync.preview_sync

preview_sync(
    workspace: str | Path,
    snapshot: WorkbookSnapshot,
    *,
    direction: Literal["read", "write"],
    matrices: list[str] | None = None,
) -> SyncPreview

Capture an immutable review batch. This function writes nothing.

Parameters:

Name Type Description Default
workspace str or Path

The workspace whose matrices are compared.

required
snapshot WorkbookSnapshot

The workbook snapshot.

required
direction str

read (matrices to workbook) or write (workbook to matrices).

required
matrices list of str

The matrix files to compare. None compares every matrix of the workspace when reading, and the matrices the Runs rows name when writing.

None

Returns:

Type Description
SyncPreview

The batch: every change with its action, the mappings, the counts and a token.

Raises:

Type Description
ExcelSyncError

If the direction is unknown, a selected matrix does not exist or is ambiguous, or a new field collides with an unmapped Runs column.

pyflightstream.workspace.excel_sync.require_token

require_token(token: str) -> str

Return token when it is a preview token, else refuse it.

A token names the recovery folder of an apply, so a batch edited to carry a path is refused before anything is created.

Parameters:

Name Type Description Default
token str

The token a preview gave.

required

Returns:

Type Description
str

token unchanged.

Raises:

Type Description
ExcelSyncError

If token is not 32 lowercase hexadecimal characters.

pyflightstream.workspace.excel_sync.three_way_action

three_way_action(
    base: str | None, source: str, target: str
) -> str

Keep target-only changes; refuse different changes on both sides.

Parameters:

Name Type Description Default
base str or None

The value at the last synchronization; None when there was none.

required
source str

The value on the side being read.

required
target str

The value on the side being written.

required

Returns:

Type Description
str

UNCHANGED, UPDATE or CONFLICT.