WIP implementation of configure directory, refs #731

This commit is contained in:
Simon Willison 2020-04-26 13:37:02 -07:00
commit 62162dcdea
2 changed files with 48 additions and 15 deletions

View file

@ -247,6 +247,34 @@ class Datasette:
self.register_renderers() 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): def add_database(self, name, db):
self.databases[name] = db self.databases[name] = db

View file

@ -352,6 +352,11 @@ def serve(
click.echo( click.echo(
"Serve! files={} (immutables={}) on port {}".format(files, immutable, port) "Serve! files={} (immutables={}) on port {}".format(files, immutable, port)
) )
# if files is a single directory, do something special with it
if 1 == len(files) and os.path.isdir(files[0]):
ds = Datasette.from_path(files[0])
else:
ds = Datasette( ds = Datasette(
files, files,
immutables=immutable, immutables=immutable,