# annals.sources

Item sources: everything the page lists, wherever it lives, as rows of one shape.

A *source* is a named, read-mostly collection of items. Two kinds exist:

* [`PublishedSource`](#annals.sources.PublishedSource): the documents agents published (a [`annals.store.DocStore`](annals.store.html.md#annals.store.DocStore)).
* [`FolderSource`](#annals.sources.FolderSource): a folder of files owned by someone else, read in place and never
  written (the crowsnest ledgers, and any other personal folder later). Its items are the
  folder’s files, keyed by their path relative to the folder.

Every source yields the same row shape, which the page lists, searches and filters:

`key`      `<source>:<id>`, unique across sources (the API’s item id)
`source`   the source’s name; `id` the item’s id within it
`title`, `kind` (see [`annals.store.kind_of()`](annals.store.html.md#annals.store.kind_of)), `session` (who wrote it)
`time`     ISO 8601, UTC: publication time, or a file’s last modification
`size`, `tags` (the item’s own; the catalog overlays the owner’s), `excerpt`

A *group of sources* is a source set that is not fixed: [`HostMirrors`](#annals.sources.HostMirrors) turns each
`<root>/<machine>/` folder into a source of its own, found again on every listing, so a
machine that starts pushing appears without a restart.

Sources are plain objects with a few methods (the [`Source`](#annals.sources.Source) protocol), so a new kind
of folder is a new class or a new keyword argument, never a change to the API.

### Module Attributes

| [`SOURCE_NAME_RE`](#annals.sources.SOURCE_NAME_RE)        | a source name is a url segment and part of every item key                            |
|------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| [`RESERVED_SOURCE_NAMES`](#annals.sources.RESERVED_SOURCE_NAMES) | names the page uses for its own routes (`/all`, `/trash`) or the published documents |
| [`HOSTS_PREFIX`](#annals.sources.HOSTS_PREFIX)          | `ledger-hosts=hosts:/srv/ledger-hosts`                                               |

### Functions

| [`as_source`](#annals.sources.as_source)(name, spec)    | A source from a source, or from a folder path (read in place as a [`FolderSource`](#annals.sources.FolderSource)); `hosts:<path>` is a [`HostMirrors`](#annals.sources.HostMirrors) (one source per machine folder in it).   |
|---------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`check_source_name`](#annals.sources.check_source_name)(name)  | `name` if it can name an extra source; `ValueError` saying why not otherwise.                                                                                                                                                                               |
| [`file_stamp`](#annals.sources.file_stamp)(st)           | What says a file changed: its inode, mtime and ctime (nanoseconds) and size.                                                                                                                                                                                |
| `iso_utc`(ts)                                                             |                                                                                                                                                                                                                                                             |
| [`list_meta`](#annals.sources.list_meta)(meta)          | A listing row: the meta without its per-file tables (a folder can hold thousands).                                                                                                                                                                          |
| `mk_key`(source, item_id)                                                 |                                                                                                                                                                                                                                                             |
| [`parse_sources_spec`](#annals.sources.parse_sources_spec)(spec) | `"ledgers=~/x, notes=/y"` -> `{"ledgers": "~/x", "notes": "/y"}` (the env form).                                                                                                                                                                            |
| [`split_key`](#annals.sources.split_key)(key)           | `"ledgers:a/b.md"` -> `("ledgers", "a/b.md")`; `KeyError` when malformed.                                                                                                                                                                                   |
| [`top_level_name`](#annals.sources.top_level_name)(rel)      | Who wrote a file in a folder of per-session files: `a.md` -> `a`, `b/c.md` -> `b`.                                                                                                                                                                          |
| [`with_thumb`](#annals.sources.with_thumb)(meta)         | Add `thumb` (the first image, main file first) and fix pre-media kinds.                                                                                                                                                                                     |

### Classes

| [`FolderSource`](#annals.sources.FolderSource)(root, \*, name[, title, ...])   | A folder of files, read in place and never written, as a `Mapping` of rows.          |
|-----------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| [`HostMirrors`](#annals.sources.HostMirrors)(root, \*, name[, title])         | A folder of per-machine mirrors (`ledger-hosts/<machine>/`) as one source each.      |
| [`MirrorSource`](#annals.sources.MirrorSource)(root, \*, name, machine, ...)   | One machine's mirror of a folder (`<root>/<machine>/`), with who and when.           |
| [`PublishedSource`](#annals.sources.PublishedSource)(store, \*[, title, ...])     | The documents agents published, as rows (a view over a `DocStore`).                  |
| [`Source`](#annals.sources.Source)(\*args, \*\*kwargs)                   | What the catalog needs from a source of items.                                       |
| [`SourceGroup`](#annals.sources.SourceGroup)()                                | A set of sources that is looked at again each time: `sources()` -> `{name: Source}`. |

### *class* annals.sources.FolderSource(root, \*, name, title=None, exclude=('.\*', '_\_pycache_\_', '\*.pyc', '\*.pyo'), session_of=<function top_level_name>)

Bases: [`Mapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)

A folder of files, read in place and never written, as a `Mapping` of rows.

Keys are paths relative to `root` (`"annals.md"`, `"cw-liaise/design.md"`), values
listing rows. The folder is walked on every listing (a stat per file, cheap for
hundreds), and a file is re-read only when its size or mtime changed. Hidden files and
caches are skipped (`exclude`, the same patterns a directory publish skips), and so
is anything that resolves outside `root`.

`session_of(rel)` names who wrote a file; the default suits a folder of per-session
files and sub-folders ([`top_level_name()`](#annals.sources.top_level_name)).

#### file_path(rel)

The file on disk; `KeyError` if it is absent, hidden, or escapes the root.

Only the canonical spelling of a path is accepted (`a/b.md`, not `./a/b.md`,
`a//b.md` or `a/b.md/`), so one file has one key.

* **Return type:**
  [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)

#### text(rel)

The file’s text (cached until it changes); `""` for binaries and huge files.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### annals.sources.HOSTS_PREFIX *= 'hosts:'*

`ledger-hosts=hosts:/srv/ledger-hosts`

* **Type:**
  the spelling of a group of host mirrors in `ANNALS_SOURCES`

### *class* annals.sources.HostMirrors(root, , name, title=None)

Bases: [`SourceGroup`](#annals.sources.SourceGroup)

A folder of per-machine mirrors (`ledger-hosts/<machine>/`) as one source each.

`name` prefixes the source names (`<name>-<machine>`, the machine slugged); each
machine’s source is a [`MirrorSource`](#annals.sources.MirrorSource), kept between listings so its caches last.
A hidden folder, a plain file, or a symlink out of `root` is not a machine.

#### sources()

`{source name: MirrorSource}` for each machine folder there now, by machine.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Source`](#annals.sources.Source)]

### *class* annals.sources.MirrorSource(root, , name, machine, base_title, \*\*kw)

Bases: [`FolderSource`](#annals.sources.FolderSource)

One machine’s mirror of a folder (`<root>/<machine>/`), with who and when.

The folder name is the machine. `_mirror.json` (`{"machine", "pushed_at", "files"}`,
written by whoever pushes) says when it was last pushed; each row carries `machine` and
`pushed_at` (read at every listing), so a ledger is never read as current on a machine
that stopped pushing. Items are keyed inside this source, so two machines holding the
same session name stay two items.

#### info()

What the page shows about the mirror: `{machine, pushed_at}`.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### pushed_at()

The mirror’s last push as `_mirror.json` says it (`""` when it does not, when
the file is not a small regular file, or when the time is not valid or is in the future).

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### *class* annals.sources.PublishedSource(store, , title='Published', max_cached_texts=512)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

The documents agents published, as rows (a view over a `DocStore`).

Always named `published`: the page links its items to `/d/<id>`.

#### forget(item_id)

Drop what is cached for a document that is gone for good (purged).

* **Return type:**
  [`None`](https://docs.python.org/3/builtins/constants.html#None)

### annals.sources.RESERVED_SOURCE_NAMES *= frozenset({'all', 'published', 'trash'})*

names the page uses for its own routes (`/all`, `/trash`) or the published documents

### annals.sources.SOURCE_NAME_RE *= re.compile('^[a-z0-9][a-z0-9_-]{0,63}$')*

a source name is a url segment and part of every item key

### *class* annals.sources.Source(\*args, \*\*kwargs)

Bases: [`Protocol`](https://docs.python.org/3/library/typing.html#typing.Protocol)

What the catalog needs from a source of items.

#### item(item_id)

One row plus what the page shows (`text` for text kinds). `KeyError` if absent.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### rows(, trash=False)

Listing rows, newest first.

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

#### text(item_id)

The item’s full text for search (`""` when it has none).

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### *class* annals.sources.SourceGroup

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

A set of sources that is looked at again each time: `sources()` -> `{name: Source}`.

### annals.sources.as_source(name, spec)

A source from a source, or from a folder path (read in place as a [`FolderSource`](#annals.sources.FolderSource));
`hosts:<path>` is a [`HostMirrors`](#annals.sources.HostMirrors) (one source per machine folder in it).

* **Return type:**
  [`Source`](#annals.sources.Source) | [`HostMirrors`](#annals.sources.HostMirrors)

### annals.sources.check_source_name(name)

`name` if it can name an extra source; `ValueError` saying why not otherwise.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### annals.sources.file_stamp(st)

What says a file changed: its inode, mtime and ctime (nanoseconds) and size.

The mtime alone is not enough: a rewrite of the same size within one timestamp tick,
or a sync that keeps mtimes, would look unchanged.

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`int`](https://docs.python.org/3/builtins/functions.html#int), [`int`](https://docs.python.org/3/builtins/functions.html#int), [`int`](https://docs.python.org/3/builtins/functions.html#int), [`int`](https://docs.python.org/3/builtins/functions.html#int)]

### annals.sources.list_meta(meta)

A listing row: the meta without its per-file tables (a folder can hold thousands).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.sources.parse_sources_spec(spec)

`"ledgers=~/x, notes=/y"` -> `{"ledgers": "~/x", "notes": "/y"}` (the env form).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### annals.sources.split_key(key)

`"ledgers:a/b.md"` -> `("ledgers", "a/b.md")`; `KeyError` when malformed.

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### annals.sources.top_level_name(rel)

Who wrote a file in a folder of per-session files: `a.md` -> `a`, `b/c.md` -> `b`.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### annals.sources.with_thumb(meta)

Add `thumb` (the first image, main file first) and fix pre-media kinds.

Documents published before media kinds existed say `file` for an image or a video;
their kind is recomputed from the main file so they show inline too.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)
