Architecture#
How the code is laid out, what each core file owns, and what happens on one write. Read concepts first for the vocabulary.
Layout#
src/metarelay/
├── __init__.py re-exports metarelay.core: the whole public API
├── cli.py the metarelay command (ls, mkdir, write, read)
├── core/ backend-neutral: the public API, and it imports no backend
├── backends/
│ ├── local.py file://, the reference backend
│ └── labarchives.py labarchives://, built on labapi
└── testing/
├── memory.py an in-memory backend for tests, the smallest backend to read
├── conformance.py the suite every backend must pass (BackendConformance)
├── labarchives_fake.py a fake LabArchives API server, standard library only
├── labarchives_smoke.py a live check DSST runs against real LabArchives
└── labarchives_cassettes.py records traffic, and compares the fake with it
Backends find each other through entry points, not imports. Each registers
under the metarelay.backends group in pyproject.toml, keyed by URI
scheme, and connect() imports only the one a URI names. So a missing extra
(no labapi) only matters to someone who uses that backend, and a
third-party package can add a backend without changing MetaRelay.
What each file in core/ owns#
File |
Owns |
|---|---|
|
The |
|
The |
|
Parsing and normalizing destination URIs. Refuses a URI with a username or password in it, so credentials never land in a receipt or an error |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Readable previews. A renderer turns a record into headings and tables; formatters turn those into HTML and Markdown. Backends store the result and never build markup themselves |
|
The error classes. Every backend translates its native errors into these, with no URL, token, or signature in the message |
Rules that keep the core neutral:
Conflict rules live only in
decide_write. A backend works out the checksums of what’s at the destination, then asks.Previews come only from
render_preview.A backend raises only
metarelayerrors.
One write, from connect() to the receipt#
This traces the
save_metadata example against a
folder:
session = connect("file:///Users/me/metadata")
receipt = session.write_record(where, record, prior_receipt=prior)
connect(uri)(core/backend.py) callsDestination.parse(uri)(core/destination.py), which refuses credentials in the URI and normalizes it: lowercase scheme and host, no trailing/.load_backend("file")reads themetarelay.backendsentry points, findsfile = "metarelay.backends.local:LocalBackend", imports it, and checks it has theBackendmethods and a validauth_flow.LocalBackend.auth_flowisNONE, soconnect()callsLocalBackend.open(destination, config). AREDIRECTbackend such as LabArchives would raiseConfigurationErrorhere, pointing tobegin_login().open()(backends/local.py) checks that the folder exists, resolves symlinks, and returns aLocalSessionwhose destination is the folder’s one canonical URI. Receipts are keyed by that URI, so every spelling of the same folder finds the same receipts.Before the write,
Record(...)(core/record.py) validated the envelope and passed the payload todefault_registry.validate_payload(core/registry.py), which stored it in normal form.session.write_record(where, record, prior_receipt=prior)walks to the container’s folder. A missing folder isNotFoundError, since onlyensure_containercreates one.It reads what is there now: the record’s
.jsonand its.mdpreview. It computesrecord_checksumof the stored record andpreview_checksumof the stored preview (core/checksum.py).decide_write(core/conflict.py) compares those with the new record’s checksum and withprior:forcemode: write;nothing there: write (unless its entry was edited, below);
createmode with a record there:ConflictError;content other than the new record’s or the one
priordescribes:ConflictError;a preview edited since
prior:ConflictError;otherwise, skip if the content is already the same, or write.
Where several records share one preview (an eLabFTW entry), the backend passes
entry_editedinstead: a hand edit anywhere in the entry is aConflictErrorfor every write into it, new records too, untilforce.On
ConflictErrornothing is written.On write,
render_preview(record).markdown(core/preview.py) builds the table. The.jsonand.mdare written atomically, then the folder’s.metarelay/index, preview checksum, and history line are updated.write_recordreads the record back and returns itsReceipt(core/receipt.py): destination, container, record ID, schema,checksum, the backend’sconflict_token(here the SHA-256 of the.jsonfile), andpreview_checksum.The caller keeps it with
SyncIndex.put(receipt), keyed by destination, path, and record ID, and passes it asprior_receiptnext time.
LabArchives follows the same steps from step 6, with notebook pages for
folders and entries for files. Its login replaces steps 3 and 4:
begin_login() returns a link, and complete_login() turns the redirect’s
query parameters into a LabArchivesSession.