config.json is now settings.json, closes #1104

pull/1112/head
Simon Willison 2020-11-24 12:37:29 -08:00
rodzic 2a3d5b720b
commit 33eadb8782
5 zmienionych plików z 30 dodań i 9 usunięć

Wyświetl plik

@ -45,6 +45,7 @@ from .database import Database, QueryInterrupted
from .utils import (
PrefixedUrlString,
StartupError,
async_call_with_supported_arguments,
await_me_maybe,
call_with_supported_arguments,
@ -265,8 +266,10 @@ class Datasette:
if config_dir and (config_dir / "static").is_dir() and not static_mounts:
static_mounts = [("static", str((config_dir / "static").resolve()))]
self.static_mounts = static_mounts or []
if config_dir and (config_dir / "config.json").exists() and not config:
config = json.load((config_dir / "config.json").open())
if config_dir and (config_dir / "config.json").exists():
raise StartupError("config.json should be renamed to settings.json")
if config_dir and (config_dir / "settings.json").exists() and not config:
config = json.load((config_dir / "settings.json").open())
self._config = dict(DEFAULT_CONFIG, **(config or {}))
self.renderers = {} # File extension -> (renderer, can_render) functions
self.version_note = version_note

Wyświetl plik

@ -14,6 +14,7 @@ from runpy import run_module
import webbrowser
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm
from .utils import (
StartupError,
check_connection,
parse_metadata,
ConnectionProblem,
@ -488,6 +489,8 @@ def serve(
ds = Datasette(files, **kwargs)
except SpatialiteNotFound:
raise click.ClickException("Could not find SpatiaLite extension")
except StartupError as e:
raise click.ClickException(e.args[0])
if return_instance:
# Private utility mechanism for writing unit tests

Wyświetl plik

@ -1027,3 +1027,7 @@ class PrefixedUrlString(str):
return method.__get__(self)
else:
return super().__getattribute__(name)
class StartupError(Exception):
pass

Wyświetl plik

@ -50,15 +50,15 @@ The files that can be included in this directory are as follows. All are optiona
* ``*.db`` - SQLite database files that will be served by Datasette
* ``metadata.json`` - :ref:`metadata` for those databases - ``metadata.yaml`` or ``metadata.yml`` can be used as well
* ``inspect-data.json`` - the result of running ``datasette inspect`` - any database files listed here will be treated as immutable, so they should not be changed while Datasette is running
* ``config.json`` - settings that would normally be passed using ``--config`` - here they should be stored as a JSON object of key/value pairs
* ``settings.json`` - settings that would normally be passed using ``--setting`` - here they should be stored as a JSON object of key/value pairs
* ``templates/`` - a directory containing :ref:`customization_custom_templates`
* ``plugins/`` - a directory containing plugins, see :ref:`writing_plugins_one_off`
* ``static/`` - a directory containing static files - these will be served from ``/static/filename.txt``, see :ref:`customization_static_files`
Configuration options
---------------------
Settings
--------
The followig options can be set using ``--config name:value``, or by storing them in the ``config.json`` file for use with :ref:`config_dir`.
The following options can be set using ``--setting name value``, or by storing them in the ``settings.json`` file for use with :ref:`config_dir`.
default_page_size
~~~~~~~~~~~~~~~~~

Wyświetl plik

@ -3,7 +3,9 @@ import pytest
import sqlite3
from datasette.app import Datasette
from datasette.cli import cli
from .fixtures import TestClient as _TestClient
from click.testing import CliRunner
PLUGIN = """
from datasette import hookimpl
@ -15,7 +17,7 @@ def extra_template_vars():
}
"""
METADATA = {"title": "This is from metadata"}
CONFIG = {
SETTINGS = {
"default_cache_ttl": 60,
}
CSS = """
@ -44,7 +46,7 @@ def config_dir_client(tmp_path_factory):
(static_dir / "hello.css").write_text(CSS, "utf-8")
(config_dir / "metadata.json").write_text(json.dumps(METADATA), "utf-8")
(config_dir / "config.json").write_text(json.dumps(CONFIG), "utf-8")
(config_dir / "settings.json").write_text(json.dumps(SETTINGS), "utf-8")
for dbname in ("demo.db", "immutable.db"):
db = sqlite3.connect(str(config_dir / dbname))
@ -85,12 +87,21 @@ def test_metadata(config_dir_client):
assert METADATA == response.json
def test_config(config_dir_client):
def test_settings(config_dir_client):
response = config_dir_client.get("/-/settings.json")
assert 200 == response.status
assert 60 == response.json["default_cache_ttl"]
def test_error_on_config_json(tmp_path_factory):
config_dir = tmp_path_factory.mktemp("config-dir")
(config_dir / "config.json").write_text(json.dumps(SETTINGS), "utf-8")
runner = CliRunner(mix_stderr=False)
result = runner.invoke(cli, [str(config_dir), "--get", "/-/settings.json"])
assert result.exit_code == 1
assert "config.json should be renamed to settings.json" in result.stderr
def test_plugins(config_dir_client):
response = config_dir_client.get("/-/plugins.json")
assert 200 == response.status