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: the documents agents published (a annals.store.DocStore).

  • 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()), 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 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 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

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

RESERVED_SOURCE_NAMES

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

HOSTS_PREFIX

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

Functions

as_source(name, spec)

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

check_source_name(name)

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

file_stamp(st)

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

iso_utc(ts)

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(spec)

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

split_key(key)

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

top_level_name(rel)

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

with_thumb(meta)

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

Classes

FolderSource(root, *, name[, title, ...])

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

HostMirrors(root, *, name[, title])

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

MirrorSource(root, *, name, machine, ...)

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

PublishedSource(store, *[, title, ...])

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

Source(*args, **kwargs)

What the catalog needs from a source of items.

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>)[source]#

Bases: 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()).

file_path(rel)[source]#

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

text(rel)[source]#

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

Return type:

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)[source]#

Bases: 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, 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]#

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

Return type:

dict[str, Source]

class annals.sources.MirrorSource(root, *, name, machine, base_title, **kw)[source]#

Bases: 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()[source]#

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

Return type:

dict

pushed_at()[source]#

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

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

Bases: 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)[source]#

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

Return type:

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)[source]#

Bases: Protocol

What the catalog needs from a source of items.

item(item_id)[source]#

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

Return type:

dict

rows(*, trash=False)[source]#

Listing rows, newest first.

Return type:

list[dict]

text(item_id)[source]#

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

Return type:

str

class annals.sources.SourceGroup[source]#

Bases: object

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

annals.sources.as_source(name, spec)[source]#

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

Return type:

Source | HostMirrors

annals.sources.check_source_name(name)[source]#

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

Return type:

str

annals.sources.file_stamp(st)[source]#

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[int, int, int, int]

annals.sources.list_meta(meta)[source]#

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

Return type:

dict

annals.sources.parse_sources_spec(spec)[source]#

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

Return type:

dict[str, str]

annals.sources.split_key(key)[source]#

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

Return type:

tuple[str, str]

annals.sources.top_level_name(rel)[source]#

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

Return type:

str

annals.sources.with_thumb(meta)[source]#

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