kopia lustrzana https://github.com/simonw/datasette
config.json is now settings.json, closes #1104
rodzic
2a3d5b720b
commit
33eadb8782
|
@ -45,6 +45,7 @@ from .database import Database, QueryInterrupted
|
||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
PrefixedUrlString,
|
PrefixedUrlString,
|
||||||
|
StartupError,
|
||||||
async_call_with_supported_arguments,
|
async_call_with_supported_arguments,
|
||||||
await_me_maybe,
|
await_me_maybe,
|
||||||
call_with_supported_arguments,
|
call_with_supported_arguments,
|
||||||
|
@ -265,8 +266,10 @@ class Datasette:
|
||||||
if config_dir and (config_dir / "static").is_dir() and not static_mounts:
|
if config_dir and (config_dir / "static").is_dir() and not static_mounts:
|
||||||
static_mounts = [("static", str((config_dir / "static").resolve()))]
|
static_mounts = [("static", str((config_dir / "static").resolve()))]
|
||||||
self.static_mounts = static_mounts or []
|
self.static_mounts = static_mounts or []
|
||||||
if config_dir and (config_dir / "config.json").exists() and not config:
|
if config_dir and (config_dir / "config.json").exists():
|
||||||
config = json.load((config_dir / "config.json").open())
|
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._config = dict(DEFAULT_CONFIG, **(config or {}))
|
||||||
self.renderers = {} # File extension -> (renderer, can_render) functions
|
self.renderers = {} # File extension -> (renderer, can_render) functions
|
||||||
self.version_note = version_note
|
self.version_note = version_note
|
||||||
|
|
|
@ -14,6 +14,7 @@ from runpy import run_module
|
||||||
import webbrowser
|
import webbrowser
|
||||||
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm
|
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS, pm
|
||||||
from .utils import (
|
from .utils import (
|
||||||
|
StartupError,
|
||||||
check_connection,
|
check_connection,
|
||||||
parse_metadata,
|
parse_metadata,
|
||||||
ConnectionProblem,
|
ConnectionProblem,
|
||||||
|
@ -488,6 +489,8 @@ def serve(
|
||||||
ds = Datasette(files, **kwargs)
|
ds = Datasette(files, **kwargs)
|
||||||
except SpatialiteNotFound:
|
except SpatialiteNotFound:
|
||||||
raise click.ClickException("Could not find SpatiaLite extension")
|
raise click.ClickException("Could not find SpatiaLite extension")
|
||||||
|
except StartupError as e:
|
||||||
|
raise click.ClickException(e.args[0])
|
||||||
|
|
||||||
if return_instance:
|
if return_instance:
|
||||||
# Private utility mechanism for writing unit tests
|
# Private utility mechanism for writing unit tests
|
||||||
|
|
|
@ -1027,3 +1027,7 @@ class PrefixedUrlString(str):
|
||||||
return method.__get__(self)
|
return method.__get__(self)
|
||||||
else:
|
else:
|
||||||
return super().__getattribute__(name)
|
return super().__getattribute__(name)
|
||||||
|
|
||||||
|
|
||||||
|
class StartupError(Exception):
|
||||||
|
pass
|
||||||
|
|
|
@ -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
|
* ``*.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
|
* ``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
|
* ``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`
|
* ``templates/`` - a directory containing :ref:`customization_custom_templates`
|
||||||
* ``plugins/`` - a directory containing plugins, see :ref:`writing_plugins_one_off`
|
* ``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`
|
* ``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
|
default_page_size
|
||||||
~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
|
@ -3,7 +3,9 @@ import pytest
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
from datasette.cli import cli
|
||||||
from .fixtures import TestClient as _TestClient
|
from .fixtures import TestClient as _TestClient
|
||||||
|
from click.testing import CliRunner
|
||||||
|
|
||||||
PLUGIN = """
|
PLUGIN = """
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
@ -15,7 +17,7 @@ def extra_template_vars():
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
METADATA = {"title": "This is from metadata"}
|
METADATA = {"title": "This is from metadata"}
|
||||||
CONFIG = {
|
SETTINGS = {
|
||||||
"default_cache_ttl": 60,
|
"default_cache_ttl": 60,
|
||||||
}
|
}
|
||||||
CSS = """
|
CSS = """
|
||||||
|
@ -44,7 +46,7 @@ def config_dir_client(tmp_path_factory):
|
||||||
(static_dir / "hello.css").write_text(CSS, "utf-8")
|
(static_dir / "hello.css").write_text(CSS, "utf-8")
|
||||||
|
|
||||||
(config_dir / "metadata.json").write_text(json.dumps(METADATA), "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"):
|
for dbname in ("demo.db", "immutable.db"):
|
||||||
db = sqlite3.connect(str(config_dir / dbname))
|
db = sqlite3.connect(str(config_dir / dbname))
|
||||||
|
@ -85,12 +87,21 @@ def test_metadata(config_dir_client):
|
||||||
assert METADATA == response.json
|
assert METADATA == response.json
|
||||||
|
|
||||||
|
|
||||||
def test_config(config_dir_client):
|
def test_settings(config_dir_client):
|
||||||
response = config_dir_client.get("/-/settings.json")
|
response = config_dir_client.get("/-/settings.json")
|
||||||
assert 200 == response.status
|
assert 200 == response.status
|
||||||
assert 60 == response.json["default_cache_ttl"]
|
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):
|
def test_plugins(config_dir_client):
|
||||||
response = config_dir_client.get("/-/plugins.json")
|
response = config_dir_client.get("/-/plugins.json")
|
||||||
assert 200 == response.status
|
assert 200 == response.status
|
||||||
|
|
Ładowanie…
Reference in New Issue