Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/add cli option #147

Merged
merged 15 commits into from
Aug 18, 2022
18 changes: 15 additions & 3 deletions nomenclature/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import click
import ast
from pathlib import Path

from nomenclature.testing import assert_valid_yaml, assert_valid_structure

cli = click.Group()


class PythonLiteralOption(click.Option):

def type_cast_value(self, ctx, value):
try:
return ast.literal_eval(value)
except Exception:
raise click.BadParameter(value)


@cli.command("validate-yaml")
@click.argument("path", type=click.Path(exists=True, path_type=Path))
def cli_valid_yaml(path: Path):
Expand All @@ -15,7 +25,9 @@ def cli_valid_yaml(path: Path):

@cli.command("validate-project")
@click.argument("path", type=click.Path(exists=True, path_type=Path))
def cli_valid_project(path: Path):
"""Assert that `path` is a valid project nomenclature"""
@click.option('--dimensions', help='Optional list of dimensions',
cls=PythonLiteralOption, default="['region', 'variable']")
def cli_valid_project(path: Path, dimensions):
"""Assert that `path` and `dimensions`(optional) are valid project nomenclatures"""
assert_valid_yaml(path)
assert_valid_structure(path)
assert_valid_structure(path, dimensions)
5 changes: 3 additions & 2 deletions nomenclature/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ def assert_valid_yaml(path: Path):
)


def assert_valid_structure(path: Path):
def assert_valid_structure(path: Path, dimensions=["region", "variable"]):
"""Assert that `path` can be initialized as a :class:`DataStructureDefinition`
with the custom `dimensions` if given

Folder structure of `path`:

- A `definitions` folder is required and must be a valid
:class:`DataStructureDefinition`
- If a `mappings` folder exists, it must be a valid :class:`RegionProcessor`
"""
definition = nomenclature.DataStructureDefinition(path / "definitions")
definition = nomenclature.DataStructureDefinition(path / "definitions", dimensions)
if (path / "mappings").is_dir():
nomenclature.RegionProcessor.from_directory(
path / "mappings"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- common:
- World
- country:
- Austria:
iso2: AT
luciecastella marked this conversation as resolved.
Show resolved Hide resolved
eu_member: true
eu_accession: 1995
large_cities: [Vienna, Graz, Linz]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Primary Energy:
definition: Total primary energy consumption
unit: EJ/yr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
model: model_a
native_regions:
- World
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
model: model_b
common_regions:
- World:
- region_a
- region_b
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- first_scenario
- second_scenario:
description : this is a nice description

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- common:
- World
- country:
- Austria:
iso2: AT
luciecastella marked this conversation as resolved.
Show resolved Hide resolved
eu_member: true
eu_accession: 1995
large_cities: [Vienna, Graz, Linz]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Primary Energy:
definition: Total primary energy consumption
unit: EJ/yr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
model: model_a
native_regions:
- World
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
model: model_b
common_regions:
- World:
- region_a
- region_b
17 changes: 17 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@ def print_helper(_path):

with pytest.raises(NotADirectoryError, match=print_helper(path)):
assert_valid_structure(TEST_DATA_DIR / "invalid_yaml")

def test_cli_custom_dimensions_quotes():
"""Check that CLI expected simple quotes (') inside double quotes (")"""
result_valid = runner.invoke(
cli, ["validate-project",
str(TEST_DATA_DIR / "non-default_dimensions_passing"),
"--dimensions \"['variable', 'region', 'foo']\""
luciecastella marked this conversation as resolved.
Show resolved Hide resolved
]
)
result_invalid = runner.invoke(
cli, ["validate-project",
str(TEST_DATA_DIR / "non-default_dimensions_passing"),
'--dimensions \'["variable", "region", "foo"]\' '
]
)
assert result_valid.exit_code == 0
assert result_invalid.exit_code == 2