Skip to content

discry.history

Write the snapshot and append new rows to the history log.

load_json

load_json(path, default)

Read a JSON file, or return default when it is missing or empty.

Parameters:

Name Type Description Default
path Path

File to read.

required
default Any

Value used when the file is absent or blank.

required

Returns:

Name Type Description
Any Any

Parsed JSON, or default.

dump_json

dump_json(path, payload, settings=None)

Write JSON, creating parent directories.

Parameters:

Name Type Description Default
path Path

Destination file.

required
payload Any

Value the JSON writer can encode.

required
settings Settings | None

Indent, key order, and trailing newline. Defaults to Settings().

None

history_key

history_key(row, key_fields=None)

Return the identity used to decide whether a history row is new.

Parameters:

Name Type Description Default
row Mapping[str, Any]

History row or an equivalent mapping.

required
key_fields tuple[str, ...] | None

Fields that form the key. None uses the default channel, build number, version hash, and capabilities. An empty version hash falls back to the build id.

None

Returns:

Type Description
tuple[Any, ...]

tuple[Any, ...]: One entry per key field.

merge_history

merge_history(
    history, surfaces, scraped_at=None, settings=None
)

Build the latest snapshot and append unseen surfaces to history.

Parameters:

Name Type Description Default
history list[Mapping[str, Any]]

Rows already stored.

required
surfaces list[ClientSurface]

Surfaces from this run.

required
scraped_at datetime | None

Timestamp stored on new rows. None uses the settings clock.

None
settings Settings | None

Field lists, formats, and the clock. Defaults to Settings().

None

Returns:

Type Description
dict[str, Any]

tuple[dict[str, Any], list[Mapping[str, Any]]]: Latest document, then the

list[Mapping[str, Any]]

combined history.

write_data_files

write_data_files(
    data_dir, surfaces, scraped_at=None, settings=None
)

Write the snapshot and the history log.

latest_formats chooses the snapshot files. An empty list uses output_formats. History uses output_formats.

The JSON history file is the append log. It is updated even when json is not one of the output formats, so later runs can still see old rows.

Parameters:

Name Type Description Default
data_dir Path

Directory used when a format's directory is unset.

required
surfaces list[ClientSurface]

Surfaces from this run.

required
scraped_at datetime | None

Timestamp stored on new rows.

None
settings Settings | None

Formats, paths, and fields. Defaults to Settings().

None

Returns:

Type Description
list[Path]

list[Path]: Files that were written.

Raises:

Type Description
TypeError

The JSON history file exists and is not an array.