# Timenet.writer

## timenet.writer

Serialization of a :class:`~timenet.dataset.TimeFDataset` to the TimeF on-disk format.

### ProgressStage

Bases: `StrEnum`

The writer's incremental stages. The writer emits only stages that carry information.

#### COMMIT `class-attribute` `instance-attribute`

```
COMMIT = 'commit'
```

#### SHARD\_FINALIZED `class-attribute` `instance-attribute`

```
SHARD_FINALIZED = 'shard_finalized'
```

#### TIME\_SERIES `class-attribute` `instance-attribute`

```
TIME_SERIES = 'time_series'
```

### TimeFWriter

Context manager that serializes a dataset into the TimeF format and commits it atomically.

#### payload\_sources `property`

```
payload_sources: Mapping[SourceKey, PayloadSource]
```

The releases that store the payloads this release reuses, pinned to their files.

:meth:`write` fills it, and the manifest records it as `payload_sources`.

#### abort

```
abort() -> None
```

Delete the staging directory. Safe to call more than once.

#### close

```
close() -> None
```

Write the manifest and atomically publish the staging directory.

An existing version directory is renamed aside to a sibling `<version>.old-<uuid>` first.
The staging directory then takes its place, and the old directory is deleted. If that
rename fails, the old directory moves back. A hard kill between the two renames leaves the
old release in `<version>.old-<uuid>`, which no sweep deletes, so it can be renamed back.
The manifest pins the payload source files that :meth:`write` checked. Before the rename,
the written files are compared with the pins of each release in `pinned_by`.

Raises:

| Type | Description |
| --- | --- |
| `RuntimeError` | If :meth:`write` has not run successfully. |
| `TimeFFormatError` | If a release in `pinned_by` pins a file that this release did not write as pinned. Nothing is published. |
| `OSError` | If publishing fails and the old release cannot be moved back either. The error names the `<version>.old-<uuid>` directory that holds it. |

#### write

```
write() -> None
```

Serialize every artifact except the manifest into the staging directory.

### WriteProgressEvent `dataclass`

A single progress event passed to the writer's `progress_cb`.

#### completed `instance-attribute`

```
completed: int
```

Number of items finished in this stage so far.

#### message `class-attribute` `instance-attribute`

```
message: str | None = None
```

Optional human-readable detail for the event.

#### stage `instance-attribute`

```
stage: ProgressStage
```

The writer stage this event reports.

#### total `instance-attribute`

```
total: int | None
```

Total number of items to process, or `None` if unknown.
