Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions digital_land/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -711,6 +717,7 @@ def organisation_create_cmd(
cache_dir,
output_path,
environment,
dataset_environment_path,
):
return organisation_create(
specification_dir=specification_dir,
Expand All @@ -720,6 +727,7 @@ def organisation_create_cmd(
cache_dir=cache_dir,
path=output_path,
environment=environment,
dataset_environment_path=dataset_environment_path,
)


Expand Down
11 changes: 10 additions & 1 deletion digital_land/package/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]

Expand Down
29 changes: 7 additions & 22 deletions tests/integration/package/test_organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import csv
import os
import shutil

import urllib.request

from digital_land.package.organisation import OrganisationPackage
Expand Down Expand Up @@ -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