diff --git a/digital_land/cli.py b/digital_land/cli.py index 3afa304d..96cdc1b7 100644 --- a/digital_land/cli.py +++ b/digital_land/cli.py @@ -673,6 +673,12 @@ def add_redirections_cmd(csv_path, pipeline_dir): type=click.Path(exists=True), help="Directory of flattened files.", ) +@click.option( + "--dataset-environment-path", + type=click.Path(), + default=None, + help="config pipeline/dataset.csv giving each dataset's environment.", +) @click.option( "--dataset-dir", type=click.Path(exists=True), @@ -711,6 +717,7 @@ def organisation_create_cmd( cache_dir, output_path, environment, + dataset_environment_path, ): return organisation_create( specification_dir=specification_dir, @@ -720,6 +727,7 @@ def organisation_create_cmd( cache_dir=cache_dir, path=output_path, environment=environment, + dataset_environment_path=dataset_environment_path, ) diff --git a/digital_land/package/organisation.py b/digital_land/package/organisation.py index f9ce9eb2..15c9f8d0 100644 --- a/digital_land/package/organisation.py +++ b/digital_land/package/organisation.py @@ -100,6 +100,7 @@ def __init__(self, **kwargs): self.download_url = kwargs.pop("download_url", None) self.cache_dir = kwargs.pop("cache_dir", None) self.environment = kwargs.pop("environment", None) + self.dataset_environment_path = kwargs.pop("dataset_environment_path", None) if self.download_url and self.download_url[-1] != "/": self.download_url += "/" super().__init__("organisation", tables={"organisation": None}, **kwargs) @@ -125,7 +126,15 @@ def source_filenames(self): os.path.join(self.specification.specification_dir, "dataset.csv"), newline="", ) as f: - datasets = organisation_datasets(csv.DictReader(f), self.environment) + rows = list(csv.DictReader(f)) + if self.dataset_environment_path: + with open(self.dataset_environment_path, newline="") as f: + environments = { + r["dataset"]: r["environment"] for r in csv.DictReader(f) + } + for row in rows: + row["environment"] = environments.get(row["dataset"], "") + datasets = organisation_datasets(rows, self.environment) logger.info(f"combining organisation datasets: {datasets}") return [f"{dataset}.csv" for dataset in datasets] diff --git a/tests/integration/package/test_organisation.py b/tests/integration/package/test_organisation.py index e54e3ae8..3721fa13 100644 --- a/tests/integration/package/test_organisation.py +++ b/tests/integration/package/test_organisation.py @@ -2,7 +2,7 @@ import json import csv import os -import shutil + import urllib.request from digital_land.package.organisation import OrganisationPackage @@ -271,35 +271,20 @@ def test_organisation_package_from_dataset( def test_organisation_package_skips_datasets_not_in_environment( - tmp_path, - specification_dir, - dataset_dir, + specification_dir, dataset_dir, tmp_path ): - """A dataset switched off for an environment in the specification must drop - out of organisation.csv there, without changing any code""" - staging_specification_dir = tmp_path / "specification" - shutil.copytree(specification_dir, staging_specification_dir) - dataset_csv = staging_specification_dir / "dataset.csv" - with open(dataset_csv, newline="") as f: - reader = csv.DictReader(f) - fieldnames = reader.fieldnames - rows = list(reader) - for row in rows: - if row["dataset"] == "government-organisation": - row["environment"] = "staging" - with open(dataset_csv, "w", newline="") as f: - writer = csv.DictWriter(f, fieldnames) - writer.writeheader() - writer.writerows(rows) + config_path = tmp_path / "dataset.csv" + config_path.write_text("dataset,environment\ngovernment-organisation,staging\n") for environment, expected_rows in (("production", 0), ("staging", 1)): package = OrganisationPackage( - specification_dir=str(staging_specification_dir), + specification_dir=specification_dir, dataset_dir=dataset_dir, path=os.path.join(tmp_path, f"{environment}-organisation.csv"), environment=environment, + dataset_environment_path=str(config_path), ) package.create() - with open(package.path, "r", encoding="UTF8", newline="") as f: + with open(package.path) as f: assert len(list(csv.DictReader(f))) == expected_rows