mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Start datasette.json, re-add --config, rm settings.json
The first step in defining the new `datasette.json/yaml` configuration mechanism. Refs #2093, #2143, #493
This commit is contained in:
parent
01e0558825
commit
17ec309e14
7 changed files with 55 additions and 93 deletions
|
|
@ -242,6 +242,7 @@ class Datasette:
|
|||
cache_headers=True,
|
||||
cors=False,
|
||||
inspect_data=None,
|
||||
config=None,
|
||||
metadata=None,
|
||||
sqlite_extensions=None,
|
||||
template_dir=None,
|
||||
|
|
@ -316,6 +317,7 @@ class Datasette:
|
|||
)
|
||||
self.cache_headers = cache_headers
|
||||
self.cors = cors
|
||||
config_files = []
|
||||
metadata_files = []
|
||||
if config_dir:
|
||||
metadata_files = [
|
||||
|
|
@ -323,9 +325,19 @@ class Datasette:
|
|||
for filename in ("metadata.json", "metadata.yaml", "metadata.yml")
|
||||
if (config_dir / filename).exists()
|
||||
]
|
||||
config_files = [
|
||||
config_dir / filename
|
||||
for filename in ("datasette.json", "datasette.yaml", "datasette.yml")
|
||||
if (config_dir / filename).exists()
|
||||
]
|
||||
if config_dir and metadata_files and not metadata:
|
||||
with metadata_files[0].open() as fp:
|
||||
metadata = parse_metadata(fp.read())
|
||||
|
||||
if config_dir and config_files and not config:
|
||||
with config_files[0].open() as fp:
|
||||
config = parse_metadata(fp.read())
|
||||
|
||||
self._metadata_local = metadata or {}
|
||||
self.sqlite_extensions = []
|
||||
for extension in sqlite_extensions or []:
|
||||
|
|
@ -344,17 +356,19 @@ 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():
|
||||
raise StartupError("config.json should be renamed to settings.json")
|
||||
if config_dir and (config_dir / "settings.json").exists() and not settings:
|
||||
settings = json.loads((config_dir / "settings.json").read_text())
|
||||
# Validate those settings
|
||||
for key in settings:
|
||||
if key not in DEFAULT_SETTINGS:
|
||||
raise StartupError(
|
||||
"Invalid setting '{}' in settings.json".format(key)
|
||||
)
|
||||
self._settings = dict(DEFAULT_SETTINGS, **(settings or {}))
|
||||
if config_dir and (config_dir / "datasette.json").exists() and not config:
|
||||
config = json.loads((config_dir / "datasette.json").read_text())
|
||||
|
||||
config = config or {}
|
||||
config_settings = config.get("settings") or {}
|
||||
|
||||
# validate "settings" keys in datasette.json
|
||||
for key in config_settings:
|
||||
if key not in DEFAULT_SETTINGS:
|
||||
raise StartupError("Invalid setting '{}' in datasette.json".format(key))
|
||||
|
||||
# CLI settings should overwrite datasette.json settings
|
||||
self._settings = dict(DEFAULT_SETTINGS, **(config_settings), **(settings or {}))
|
||||
self.renderers = {} # File extension -> (renderer, can_render) functions
|
||||
self.version_note = version_note
|
||||
if self.setting("num_sql_threads") == 0:
|
||||
|
|
|
|||
|
|
@ -50,46 +50,6 @@ except ImportError:
|
|||
pass
|
||||
|
||||
|
||||
class Config(click.ParamType):
|
||||
# This will be removed in Datasette 1.0 in favour of class Setting
|
||||
name = "config"
|
||||
|
||||
def convert(self, config, param, ctx):
|
||||
if ":" not in config:
|
||||
self.fail(f'"{config}" should be name:value', param, ctx)
|
||||
return
|
||||
name, value = config.split(":", 1)
|
||||
if name not in DEFAULT_SETTINGS:
|
||||
msg = (
|
||||
OBSOLETE_SETTINGS.get(name)
|
||||
or f"{name} is not a valid option (--help-settings to see all)"
|
||||
)
|
||||
self.fail(
|
||||
msg,
|
||||
param,
|
||||
ctx,
|
||||
)
|
||||
return
|
||||
# Type checking
|
||||
default = DEFAULT_SETTINGS[name]
|
||||
if isinstance(default, bool):
|
||||
try:
|
||||
return name, value_as_boolean(value)
|
||||
except ValueAsBooleanError:
|
||||
self.fail(f'"{name}" should be on/off/true/false/1/0', param, ctx)
|
||||
return
|
||||
elif isinstance(default, int):
|
||||
if not value.isdigit():
|
||||
self.fail(f'"{name}" should be an integer', param, ctx)
|
||||
return
|
||||
return name, int(value)
|
||||
elif isinstance(default, str):
|
||||
return name, value
|
||||
else:
|
||||
# Should never happen:
|
||||
self.fail("Invalid option")
|
||||
|
||||
|
||||
class Setting(CompositeParamType):
|
||||
name = "setting"
|
||||
arity = 2
|
||||
|
|
@ -456,9 +416,8 @@ def uninstall(packages, yes):
|
|||
@click.option("--memory", is_flag=True, help="Make /_memory database available")
|
||||
@click.option(
|
||||
"--config",
|
||||
type=Config(),
|
||||
help="Deprecated: set config option using configname:value. Use --setting instead.",
|
||||
multiple=True,
|
||||
type=click.File(mode="r"),
|
||||
help="Path to JSON/YAML Datasette configuration file",
|
||||
)
|
||||
@click.option(
|
||||
"--setting",
|
||||
|
|
@ -568,6 +527,8 @@ def serve(
|
|||
reloader = hupper.start_reloader("datasette.cli.serve")
|
||||
if immutable:
|
||||
reloader.watch_files(immutable)
|
||||
if config:
|
||||
reloader.watch_files([config.name])
|
||||
if metadata:
|
||||
reloader.watch_files([metadata.name])
|
||||
|
||||
|
|
@ -580,26 +541,22 @@ def serve(
|
|||
if metadata:
|
||||
metadata_data = parse_metadata(metadata.read())
|
||||
|
||||
combined_settings = {}
|
||||
config_data = None
|
||||
if config:
|
||||
click.echo(
|
||||
"--config name:value will be deprecated in Datasette 1.0, use --setting name value instead",
|
||||
err=True,
|
||||
)
|
||||
combined_settings.update(config)
|
||||
combined_settings.update(settings)
|
||||
config_data = parse_metadata(config.read())
|
||||
|
||||
kwargs = dict(
|
||||
immutables=immutable,
|
||||
cache_headers=not reload,
|
||||
cors=cors,
|
||||
inspect_data=inspect_data,
|
||||
config=config_data,
|
||||
metadata=metadata_data,
|
||||
sqlite_extensions=sqlite_extensions,
|
||||
template_dir=template_dir,
|
||||
plugins_dir=plugins_dir,
|
||||
static_mounts=static,
|
||||
settings=combined_settings,
|
||||
settings=dict(settings),
|
||||
memory=memory,
|
||||
secret=secret,
|
||||
version_note=version_note,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue