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

Set repository attribute correctly #335

Merged
Merged
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
1 change: 1 addition & 0 deletions nomenclature/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Code(BaseModel):
description: str | None = None
file: Union[str, Path] | None = None
extra_attributes: Dict[str, Any] = {}
repository: str | None = None

def __eq__(self, other) -> bool:
return self.model_dump(exclude="file") == other.model_dump(exclude="file")
Expand Down
26 changes: 18 additions & 8 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def from_directory(
cls,
name: str,
path: Path,
config: NomenclatureConfig = None,
config: NomenclatureConfig | None = None,
file_glob_pattern: str = "**/*",
):
"""Initialize a CodeList from a directory with codelist files
Expand All @@ -200,9 +200,12 @@ def from_directory(
for repo in getattr(
config.definitions, name.lower(), CodeListConfig()
).repositories:
repo_path = config.repositories[repo].local_path / "definitions" / name
code_list = (
cls._parse_codelist_dir(repo_path, file_glob_pattern) + code_list
code_list.extend(
cls._parse_codelist_dir(
config.repositories[repo].local_path / "definitions" / name,
file_glob_pattern,
repo,
)
)
errors = ErrorCollector()
mapping: Dict[str, Code] = {}
Expand All @@ -217,7 +220,12 @@ def from_directory(
return cls(name=name, mapping=mapping)

@classmethod
def _parse_codelist_dir(cls, path: Path, file_glob_pattern: str = "**/*"):
def _parse_codelist_dir(
cls,
path: Path,
file_glob_pattern: str = "**/*",
repository: str | None = None,
):
code_list: List[Code] = []
for yaml_file in (
f
Expand All @@ -229,6 +237,8 @@ def _parse_codelist_dir(cls, path: Path, file_glob_pattern: str = "**/*"):
for code_dict in _code_list:
code = cls.code_basis.from_dict(code_dict)
code.file = yaml_file.relative_to(path.parent).as_posix()
if repository:
code.repository = repository
code_list.append(code)

code_list = cls._parse_and_replace_tags(code_list, path, file_glob_pattern)
Expand Down Expand Up @@ -569,7 +579,7 @@ def from_directory(
cls,
name: str,
path: Path,
config: NomenclatureConfig = None,
config: NomenclatureConfig | None = None,
file_glob_pattern: str = "**/*",
):
"""Initialize a RegionCodeList from a directory with codelist files
Expand Down Expand Up @@ -617,7 +627,7 @@ def from_directory(
code_list,
repo_path,
file_glob_pattern,
repository=config.definitions.region.repositories,
repository=repo,
)
code_list = cls._parse_and_replace_tags(
code_list, repo_path, file_glob_pattern
Expand Down Expand Up @@ -656,7 +666,7 @@ def _parse_region_code_dir(
code_list: List[Code],
path: Path,
file_glob_pattern: str = "**/*",
repository: Path = None,
repository: str | None = None,
) -> List[RegionCode]:
""""""

Expand Down
16 changes: 16 additions & 0 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,22 @@ def test_multiple_external_repos():
clean_up_external_repos(nomenclature_config.repositories)


def test_multiple_external_repos():
nomenclature_config = NomenclatureConfig.from_file(
TEST_DATA_DIR / "nomenclature_configs" / "multiple_repos_per_dimension.yaml"
)
try:
variable_code_list = VariableCodeList.from_directory(
"variable",
TEST_DATA_DIR / "nomenclature_configs" / "variable",
nomenclature_config,
)
assert variable_code_list["Final Energy"].repository == "common-definitions"
assert variable_code_list["Employment"].repository == "legacy-definitions"
finally:
clean_up_external_repos(nomenclature_config.repositories)


@pytest.mark.parametrize("CodeList", [VariableCodeList, CodeList])
def test_variable_codelist_with_duplicates_raises(CodeList):
error_string = "2 errors:\n.*Some Variable\n.*Some other Variable"
Expand Down
Loading