annals.originals#

Originals: a pick’s files, exactly as published, as one streamed .zip.

One file (annals.combine) cannot hold audio or video, so a pick of originals has its own form: one folder per document, named after its title, holding the document’s files with their published names. The zip is streamed (members are stored, since audio, video, PDFs and images are already compressed), so the server never holds it whole; the size is known before it starts (plan()), and a pick over the cap is refused with the cap in the message (TooBig), never started and abandoned half way.

>>> import io, zipfile
>>> from pathlib import Path
>>> import tempfile
>>> d = Path(tempfile.mkdtemp()); (d / "a.txt").write_text("hi")
2
>>> docs = [{"id": "x", "title": "My / doc", "files": ["a.txt"]}]
>>> p = plan(docs, path_of=lambda i, rel: d / rel)
>>> [e.arcname for e in p.entries], p.bytes
(['My doc/a.txt'], 2)
>>> zipfile.ZipFile(io.BytesIO(b"".join(stream(p)))).read("My doc/a.txt")
b'hi'

Functions

folder_name(title, fallback)

A title as a folder name: no separators or control characters, no leading dots, bounded.

plan(docs, *, path_of[, max_bytes, max_docs])

The files of docs (metas with id, title, files) and their total size.

size_text(n)

Bytes as the page says them.

stream(the_plan, *[, chunk])

The zip of the_plan, in pieces, a file at a time (nothing is held whole).

Classes

Entry(path, arcname, size, mtime)

One file: where it is, its name in the zip, its size and modification time.

Plan(entries, bytes, docs)

What a zip will hold: the entries, their total size, and the documents they came from.

Exceptions

TooBig

The pick is over a cap; the message names the cap.

class annals.originals.Entry(path, arcname, size, mtime)[source]#

Bases: object

One file: where it is, its name in the zip, its size and modification time.

class annals.originals.Plan(entries, bytes, docs)[source]#

Bases: object

What a zip will hold: the entries, their total size, and the documents they came from.

exception annals.originals.TooBig[source]#

Bases: ValueError

The pick is over a cap; the message names the cap.

annals.originals.folder_name(title, fallback)[source]#

A title as a folder name: no separators or control characters, no leading dots, bounded.

>>> folder_name("Q3: plans / draft", "id1")
'Q3 plans draft'
>>> folder_name("  ..  ", "id1")
'id1'
>>> folder_name("con", "id1")
'con_'
Return type:

str

annals.originals.plan(docs, *, path_of, max_bytes=None, max_docs=None)[source]#

The files of docs (metas with id, title, files) and their total size.

path_of(doc_id, rel) finds a file on disk (and refuses one that is not the document’s). Two documents with one title get title, title (2)… A file that is gone from disk is skipped. Over max_docs documents or max_bytes bytes: TooBig.

Return type:

Plan

annals.originals.size_text(n)[source]#

Bytes as the page says them.

>>> size_text(512 * 2**20), size_text(1536)
('512 MB', '1.5 KB')
Return type:

str

annals.originals.stream(the_plan, *, chunk=262144)[source]#

The zip of the_plan, in pieces, a file at a time (nothing is held whole).

Return type:

Iterator[bytes]