Corpus discovery¶
gutenberg_kg.corpus
¶
corpus.py — Library API for GutenbergKG corpus-level operations.
Provides path-parametrised functions for building corpus status reports and snapshot dicts. All functions accept explicit path arguments so they can be called from any context — CLI, tests, or kgrag adapters — without depending on package-level path constants.
Author: Eric G. Suchanek, PhD License: Elastic 2.0
GutenbergSnapshotManager(snapshots_dir, *, registry_path, repo_root, corpus_root)
¶
Bases: SnapshotManager
Corpus-aware snapshot manager backed by kg_utils.snapshots.SnapshotManager.
Snapshots are stored as <key>.json alongside a manifest.json index
in snapshots_dir. Use :meth:capture to build a snapshot from the live
corpus, then :meth:save_snapshot to persist it.
Since kgmodule-utils 0.19.0 the key is a UTC timestamp, not the git tree
hash: the hash was read before git add staged the snapshot, so it named
a tree that was never committed. A timestamp is the right key for a corpus
anyway — the corpus changes when books are ingested, not when the repo is
tagged. Older snapshots on disk keep their hash keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshots_dir
|
Path | str
|
Directory where snapshot JSON files are stored. |
required |
registry_path
|
Path
|
KGRAG registry SQLite path. |
required |
repo_root
|
Path
|
Repository root (for git metadata). |
required |
corpus_root
|
Path
|
Corpus data root (contains |
required |
Initialize the manager and store the paths used to capture snapshots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshots_dir
|
Path | str
|
Directory where snapshot JSON files are stored. |
required |
registry_path
|
Path
|
KGRAG registry SQLite path. |
required |
repo_root
|
Path
|
Repository root (for git metadata). |
required |
corpus_root
|
Path
|
Corpus data root (contains |
required |
Source code in src/gutenberg_kg/corpus.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
capture(version=None, branch=None, graph_stats_dict=None, tree_hash='', hotspots=None, issues=None, key='', subject='', **extra_metrics)
¶
Build a corpus snapshot from the live registry.
key and subject are named rather than left to extra_metrics:
the base takes **extra_metrics too, so an unnamed one would be
recorded as a metric instead of reaching the fields it belongs in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str | None
|
Version string; auto-detected from package if None. |
None
|
branch
|
str | None
|
Git branch name; auto-detected if None. |
None
|
graph_stats_dict
|
dict[str, Any] | None
|
Ignored; corpus metrics are computed from the registry. |
None
|
tree_hash
|
str
|
Git tree hash; recorded as provenance, no longer the key. |
''
|
hotspots
|
list[dict[str, Any]] | None
|
Unused; accepted for LSP compatibility. |
None
|
issues
|
list[str] | None
|
Unused; accepted for LSP compatibility. |
None
|
key
|
str
|
Snapshot identifier; a UTC timestamp when omitted, which is what a corpus wants — pass a release tag only when snapshotting the package rather than the books. |
''
|
subject
|
str
|
What was measured; defaults to :data: |
''
|
extra_metrics
|
Any
|
Passed through to the base |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
New |
Source code in src/gutenberg_kg/corpus.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
save_snapshot(snapshot, *, force=False)
¶
Persist snapshot, guarding against degenerate (zero-book) state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot
|
Any
|
Snapshot to persist. |
required |
force
|
bool
|
If True, always create a new history entry. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the snapshot has 0 books. |
Source code in src/gutenberg_kg/corpus.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
collect_genre_stats(registry_path)
¶
Aggregate per-genre stats from the KGRAG registry.
Returns a list of dicts with keys: corpus, label, books,
nodes, edges. Only SQLite files that exist on disk are counted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry_path
|
Path
|
Path to the KGRAG registry SQLite file. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
Per-genre stat dicts in |
Raises:
| Type | Description |
|---|---|
OSError
|
If the registry cannot be opened. |
Source code in src/gutenberg_kg/corpus.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
corpus_status(registry_path, repo_root, corpus_root)
¶
Return a live corpus-wide status dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry_path
|
Path
|
KGRAG registry SQLite path. |
required |
repo_root
|
Path
|
Root of the gutenberg_kg repository (for git info). |
required |
corpus_root
|
Path
|
Corpus data directory (contains |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with |
Source code in src/gutenberg_kg/corpus.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
snapshot_list(snapshots_dir)
¶
Load and return all snapshots, oldest first.
Reads what :class:GutenbergSnapshotManager writes: a manifest.json
listing one <key>.json per snapshot. Each record carries key,
branch, timestamp, version and a metrics dict.
This used to glob snapshot-*.json — the filename the retired
:func:snapshot_save produced — which silently returned nothing once
gutenkg snapshot save moved to the manager, taking gutenkg
viz-timeline with it. The manifest is authoritative and already ordered;
the glob is a fallback for a directory whose manifest is missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshots_dir
|
Path
|
Directory containing |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
Snapshot dicts oldest first; empty if the directory is absent or holds nothing readable. |
Source code in src/gutenberg_kg/corpus.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |