# annals.prefs

Each reader’s page preferences: a few named sections of JSON, kept on the server.

The page’s settings (the order of a document’s actions, first) follow the reader from the
phone to the tablet to the desktop, and from Safari to the home-screen app, which on an
iPhone keep separate browser storage. So they live here, one JSON file per reader beside the
listening records (`<root>/prefs/<reader>.json`), and the page keeps a copy in the browser
only to draw its first screen.

The page owns what a section means: its schema, defaults and the merge of a saved value with
a newer default are in `app.js` (`SETTINGS`). The server keeps each section as the
reader left it, refusing only what no section could be: a name that is not a short slug, a
value that is not a JSON object, a section over `MAX_SECTION_BYTES`, or more than
`MAX_SECTIONS` sections. That keeps a new setting a page-only change.

```pycon
>>> clean_section("bar", {"order": ["links", "listen"]})
{'order': ['links', 'listen']}
>>> clean_section("Bar!", {})
Traceback (most recent call last):
...
ValueError: a section name is a lower-case slug of at most 32 characters
```

### Functions

| [`clean_section`](#annals.prefs.clean_section)(name, value)        | `value` as kept under `name`, or `ValueError` saying why it cannot be.       |
|------------------------------------------------------------------------------------|------------------------------------------------------------------------------|
| [`for_reader`](#annals.prefs.for_reader)(root)                  | The default seam: each reader's preferences in `<root>/prefs/<reader>.json`. |
| [`put_section`](#annals.prefs.put_section)(records, name, value) | Keep `value` as the reader's `name` section; the sections as they now stand. |

### Classes

| [`PrefsStore`](#annals.prefs.PrefsStore)(path)   | One reader's preferences: `{section: object}` in one JSON file.   |
|---------------------------------------------------------------------|-------------------------------------------------------------------|

### *class* annals.prefs.PrefsStore(path)

Bases: [`JsonFileStore`](annals.jsonstore.html.md#annals.jsonstore.JsonFileStore)

One reader’s preferences: `{section: object}` in one JSON file.

#### valid(value)

Whether a value read from the file is kept (an invalid one is logged and skipped).

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

### annals.prefs.clean_section(name, value)

`value` as kept under `name`, or `ValueError` saying why it cannot be.

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

### annals.prefs.for_reader(root)

The default seam: each reader’s preferences in `<root>/prefs/<reader>.json`.

* **Return type:**
  [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)[[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)], [`Records`](annals.listening.html.md#annals.listening.Records)]

### annals.prefs.put_section(records, name, value)

Keep `value` as the reader’s `name` section; the sections as they now stand.

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