Skip to content

Commit

Permalink
config_dir= parameter for Datasette(), refs #731
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 27, 2020
1 parent 62162dc commit bce27d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 46 deletions.
45 changes: 17 additions & 28 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,16 @@ def __init__(
memory=False,
config=None,
version_note=None,
config_dir=None,
):
assert config_dir is None or isinstance(
config_dir, Path
), "config_dir= should be a pathlib.Path"
# TODO: Use 'inspect-data.json' to decide on immutables
immutables = immutables or []
self.files = tuple(files) + tuple(immutables)
if config_dir:
self.files += tuple([str(p) for p in config_dir.glob("*.db")])
self.immutables = set(immutables)
if not self.files:
self.files = [MEMORY]
Expand All @@ -187,12 +194,22 @@ def __init__(
self.add_database(db.name, db)
self.cache_headers = cache_headers
self.cors = cors
if (config_dir / "metadata.json").exists() and not metadata:
metadata = json.load((config_dir / "metadata.json").open())
self._metadata = metadata or {}
self.sqlite_functions = []
self.sqlite_extensions = sqlite_extensions or []
if (config_dir / "templates").is_dir() and not template_dir:
template_dir = str((config_dir / "templates").resolve())
self.template_dir = template_dir
if (config_dir / "plugins").is_dir() and not plugins_dir:
template_dir = str((config_dir / "plugins").resolve())
self.plugins_dir = plugins_dir
if (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 / "config.json").exists() and not config:
config = json.load((config_dir / "config.json").open())
self._config = dict(DEFAULT_CONFIG, **(config or {}))
self.renderers = {} # File extension -> renderer function
self.version_note = version_note
Expand Down Expand Up @@ -247,34 +264,6 @@ def __init__(

self.register_renderers()

@classmethod
def from_path(cls, path):
path = Path(path)
files = [str(p.resolve()) for p in path.glob("*.db")]
inspect_data = None
if (path / "inspect.json").exists():
inspect_data = json.load((path / "inspect.json").open())
metadata = None
if (path / "metadata.json").exists():
metadata = json.load((path / "metadata.json").open())
template_dir = None
if (path / "templates").exists():
template_dir = str((path / "templates"))
plugins_dir = None
if (path / "plugins").exists():
plugins = str((path / "plugins"))
static_mounts = None
if (path / "static").exists():
static_mounts = [("static", str(path / "plugins"))]
return cls(
files,
inspect_data=inspect_data,
metadata=metadata,
template_dir=template_dir,
plugins_dir=plugins_dir,
static_mounts=static_mounts,
)

def add_database(self, name, db):
self.databases[name] = db

Expand Down
40 changes: 22 additions & 18 deletions datasette/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from click_default_group import DefaultGroup
import json
import os
import pathlib
import shutil
from subprocess import call
import sys
Expand Down Expand Up @@ -353,25 +354,28 @@ def serve(
"Serve! files={} (immutables={}) on port {}".format(files, immutable, port)
)

# if files is a single directory, do something special with it
kwargs = dict(
immutables=immutable,
cache_headers=not debug and not reload,
cors=cors,
inspect_data=inspect_data,
metadata=metadata_data,
sqlite_extensions=sqlite_extensions,
template_dir=template_dir,
plugins_dir=plugins_dir,
static_mounts=static,
config=dict(config),
memory=memory,
version_note=version_note,
)

# if files is a single directory, use that as config_dir=
if 1 == len(files) and os.path.isdir(files[0]):
ds = Datasette.from_path(files[0])
else:
ds = Datasette(
files,
immutables=immutable,
cache_headers=not debug and not reload,
cors=cors,
inspect_data=inspect_data,
metadata=metadata_data,
sqlite_extensions=sqlite_extensions,
template_dir=template_dir,
plugins_dir=plugins_dir,
static_mounts=static,
config=dict(config),
memory=memory,
version_note=version_note,
)
kwargs["config_dir"] = pathlib.Path(files[0])
files = []

ds = Datasette(files, **kwargs)

if return_instance:
# Private utility mechanism for writing unit tests
return ds
Expand Down

0 comments on commit bce27d7

Please sign in to comment.