From 1d91ab71d4359741b03bbd4347f4360eb8ca817d Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 30 Apr 2020 11:47:21 -0700 Subject: [PATCH] Directory configuration mode supports metadata.yaml, closes #747 --- datasette/app.py | 13 +++++++++++-- tests/test_config_dir.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 3c7644a6..320cc621 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -34,6 +34,7 @@ from .utils import ( escape_sqlite, format_bytes, module_from_path, + parse_metadata, sqlite3, to_css_class, ) @@ -203,8 +204,16 @@ class Datasette: self.add_database(db.name, db) self.cache_headers = cache_headers self.cors = cors - if config_dir and (config_dir / "metadata.json").exists() and not metadata: - metadata = json.load((config_dir / "metadata.json").open()) + metadata_files = [] + if config_dir: + metadata_files = [ + config_dir / filename + for filename in ("metadata.json", "metadata.yaml", "metadata.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()) self._metadata = metadata or {} self.sqlite_functions = [] self.sqlite_extensions = sqlite_extensions or [] diff --git a/tests/test_config_dir.py b/tests/test_config_dir.py index e495b9d3..f262cc59 100644 --- a/tests/test_config_dir.py +++ b/tests/test_config_dir.py @@ -130,3 +130,15 @@ def test_databases(config_dir_client): assert databases[0]["is_mutable"] assert "immutable" == databases[1]["name"] assert not databases[1]["is_mutable"] + + +@pytest.mark.parametrize("filename", ("metadata.yml", "metadata.yaml")) +def test_metadata_yaml(tmp_path_factory, filename): + config_dir = tmp_path_factory.mktemp("yaml-config-dir") + (config_dir / filename).write_text("title: Title from metadata", "utf-8") + ds = Datasette([], config_dir=config_dir) + client = TestClient(ds.app()) + client.ds = ds + response = client.get("/-/metadata.json") + assert 200 == response.status + assert {"title": "Title from metadata"} == json.loads(response.text)