annals.target#

The transport seam: a document tree lands in a directory, here or on another machine.

A Target is the handful of filesystem verbs the store needs (put a directory, move, remove, read and write a text file, list sub-directories, read every meta.json under a directory). Two implementations:

  • LocalTarget: a directory on this machine, plain pathlib/shutil.

  • SshTarget: host:path on another machine, through the system ssh and rsync in batch mode (no prompts, so an agent never hangs). The host is whatever the user’s ~/.ssh/config resolves, so an alias such as tw works.

parse_target() picks one from a string. An HTTP target (a bearer-token ingest endpoint) is the declared replacement for the ssh one and is not built yet.

Module Attributes

RECORD_SEP

Separator between concatenated meta.json files when reading many at once over ssh.

META_GLOB

each document's meta.json

LOCK_NOTE

the note a lock directory is created with (see Target.make_lock())

SSH_SHARE_S

the several calls of one command (and the next command soon after) reuse it instead of paying a handshake each (~3 s to a far host)

Functions

check_glob(pattern)

A glob safe to hand a remote shell unquoted: names, * and / only, no hidden part.

check_rel(rel)

Refuse anything that could escape the root; the store only ever passes safe paths.

parse_target(spec)

host:/path becomes an SshTarget; anything else a LocalTarget.

ssh_opts(*[, share_dir])

The ssh options: batch mode, and one connection shared by consecutive calls.

Classes

LocalTarget(root)

A directory on this machine.

SshTarget(host, root)

host:path on another machine, via the system ssh and rsync in batch mode.

Target(*args, **kwargs)

What the store needs from wherever the documents live.

annals.target.LOCK_NOTE = 'lock.json'#

the note a lock directory is created with (see Target.make_lock())

class annals.target.LocalTarget(root)[source]#

Bases: object

A directory on this machine.

local_path(rel)[source]#

rel under the root, resolved; None if it escapes the root (a symlink).

Return type:

Path | None

annals.target.META_GLOB = '*/meta.json'#

each document’s meta.json

Type:

what Target.read_metas() reads by default

annals.target.RECORD_SEP = '\x1e'#

Separator between concatenated meta.json files when reading many at once over ssh.

annals.target.SSH_SHARE_S = 60#

the several calls of one command (and the next command soon after) reuse it instead of paying a handshake each (~3 s to a far host)

Type:

how long the shared ssh connection outlives its last use

class annals.target.SshTarget(host, root)[source]#

Bases: object

host:path on another machine, via the system ssh and rsync in batch mode.

class annals.target.Target(*args, **kwargs)[source]#

Bases: Protocol

What the store needs from wherever the documents live.

file_sizes(rel, files)[source]#

The byte size of each of files under rel that is a regular file.

Return type:

dict[str, int]

local_path(rel)[source]#

The file on this machine’s disk, when the documents are local (to stream it).

Return type:

Path | None

make_lock(rel, note)[source]#

Create directory rel holding lock.json (note) as one step; fail if it exists. An atomic test-and-set: a lock that is never seen without its note.

Return type:

None

move(rel_src, rel_dst)[source]#

Rename; fails when the source is gone or the destination already exists.

Return type:

None

read_metas(rel, *, pattern='*/meta.json')[source]#

Every JSON file under rel matching pattern, read in one go (hidden ones skipped).

Return type:

list[dict]

annals.target.check_glob(pattern)[source]#

A glob safe to hand a remote shell unquoted: names, * and / only, no hidden part.

Return type:

str

annals.target.check_rel(rel)[source]#

Refuse anything that could escape the root; the store only ever passes safe paths.

Return type:

str

annals.target.parse_target(spec)[source]#

host:/path becomes an SshTarget; anything else a LocalTarget.

Return type:

Target

annals.target.ssh_opts(*, share_dir=None)[source]#

The ssh options: batch mode, and one connection shared by consecutive calls.

The shared connection’s socket lives in share_dir (default ~/.ssh); without that directory every call opens its own connection, as plain ssh would.

Return type:

tuple[str, ...]