From 62162dcdea0082645f91c3216c6120f4a4cb296f Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 26 Apr 2020 13:37:02 -0700 Subject: [PATCH] WIP implementation of configure directory, refs #731 --- datasette/app.py | 28 ++++++++++++++++++++++++++++ datasette/cli.py | 35 ++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 221e862c..471dcbc3 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -247,6 +247,34 @@ class Datasette: 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 diff --git a/datasette/cli.py b/datasette/cli.py index 6e8f0d9b..2240ede3 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -352,21 +352,26 @@ def serve( click.echo( "Serve! files={} (immutables={}) on port {}".format(files, immutable, port) ) - 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, - ) + + # 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( + 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, + ) if return_instance: # Private utility mechanism for writing unit tests return ds