# annals.originals

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

One file ([`annals.combine`](annals.combine.html.md#module-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()`](#annals.originals.plan)), and a pick over the cap is refused with the cap in the
message ([`TooBig`](#annals.originals.TooBig)), never started and abandoned half way.

```pycon
>>> 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`](#annals.originals.folder_name)(title, fallback)                   | A title as a folder name: no separators or control characters, no leading dots, bounded.   |
|-------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
| [`plan`](#annals.originals.plan)(docs, \*, path_of[, max_bytes, max_docs]) | The files of `docs` (metas with `id`, `title`, `files`) and their total size.              |
| [`size_text`](#annals.originals.size_text)(n)                                   | Bytes as the page says them.                                                               |
| [`stream`](#annals.originals.stream)(the_plan, \*[, chunk])                  | The zip of `the_plan`, in pieces, a file at a time (nothing is held whole).                |

### Classes

| [`Entry`](#annals.originals.Entry)(path, arcname, size, mtime)   | One file: where it is, its name in the zip, its size and modification time.            |
|--------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|
| [`Plan`](#annals.originals.Plan)(entries, bytes, docs)          | What a zip will hold: the entries, their total size, and the documents they came from. |

### Exceptions

| [`TooBig`](#annals.originals.TooBig)   | The pick is over a cap; the message names the cap.   |
|-----------------------------------------------------------|------------------------------------------------------|

### *class* annals.originals.Entry(path, arcname, size, mtime)

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

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

### *class* annals.originals.Plan(entries, bytes, docs)

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

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

### *exception* annals.originals.TooBig

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

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

### annals.originals.folder_name(title, fallback)

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

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

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

### annals.originals.plan(docs, , path_of, max_bytes=None, max_docs=None)

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`](#annals.originals.TooBig).

* **Return type:**
  [`Plan`](#annals.originals.Plan)

### annals.originals.size_text(n)

Bytes as the page says them.

```pycon
>>> size_text(512 * 2**20), size_text(1536)
('512 MB', '1.5 KB')
```

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

### annals.originals.stream(the_plan, , chunk=262144)

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

* **Return type:**
  [`Iterator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterator)[[`bytes`](https://docs.python.org/3/builtins/stdtypes.html#bytes)]
