Skip to content

Commit

Permalink
Import (region) definitions from other repository (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Jul 20, 2023
1 parent 7613986 commit 7cccbd3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
59 changes: 42 additions & 17 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,24 +589,18 @@ def from_directory(
)
for c in PYCOUNTRY_NAME_ADD:
code_list.append(RegionCode(name=c, hierarchy="Country"))
if repo := config.region.repository:
repo_path = path.parents[1] / repo
if not repo_path.exists():
raise FileNotFoundError(f"Repository not found: {repo}")
code_list = cls._parse_region_code_dir(
code_list, repo_path, file_glob_pattern, repository=repo
)
code_list = cls._parse_and_replace_tags(
code_list, repo_path, file_glob_pattern
)

for yaml_file in (
f
for f in path.glob(file_glob_pattern)
if f.suffix in {".yaml", ".yml"} and not f.name.startswith("tag_")
):
with open(yaml_file, "r", encoding="utf-8") as stream:
_code_list = yaml.safe_load(stream)

# a "region" codelist assumes a top-level category to be used as attribute
for top_level_cat in _code_list:
for top_key, _codes in top_level_cat.items():
for item in _codes:
code = RegionCode.from_dict(item)
code.hierarchy = top_key
code.file = yaml_file.relative_to(path.parent).as_posix()
code_list.append(code)

code_list = cls._parse_region_code_dir(code_list, path, file_glob_pattern)
code_list = cls._parse_and_replace_tags(code_list, path, file_glob_pattern)

mapping: Dict[str, RegionCode] = {}
Expand All @@ -628,6 +622,37 @@ def hierarchy(self) -> List[str]:
"""
return sorted(list({v.hierarchy for v in self.mapping.values()}))

@classmethod
def _parse_region_code_dir(
cls,
code_list: List[Code],
path: Path,
file_glob_pattern: str = "**/*",
repository: Path = None,
) -> List[RegionCode]:
""""""

for yaml_file in (
f
for f in path.glob(file_glob_pattern)
if f.suffix in {".yaml", ".yml"} and not f.name.startswith("tag_")
):
with open(yaml_file, "r", encoding="utf-8") as stream:
_code_list = yaml.safe_load(stream)

# a "region" codelist assumes a top-level category to be used as attribute
for top_level_cat in _code_list:
for top_key, _codes in top_level_cat.items():
for item in _codes:
code = RegionCode.from_dict(item)
code.hierarchy = top_key
if repository:
code.repository = repository
code.file = yaml_file.relative_to(path.parent).as_posix()
code_list.append(code)

return code_list


class MetaCodeList(CodeList):
"""A subclass of CodeList specified for MetaCodes
Expand Down
6 changes: 5 additions & 1 deletion nomenclature/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import yaml


class RegionCodeListConfig(BaseModel):
class CodeListConfig(BaseModel):
repository: Optional[Path]


class RegionCodeListConfig(CodeListConfig):
country: Optional[bool]


Expand Down
1 change: 1 addition & 0 deletions tests/data/general-config-definitions/config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
region:
repository: validation_nc/region
country: true
2 changes: 1 addition & 1 deletion tests/data/general-config-definitions/region/regions.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- common:
- World
- Region A
2 changes: 2 additions & 0 deletions tests/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def test_definition_from_general_config():
)

# explicitly defined in `general-config-definitions/region/regions.yaml`
assert "Region A" in obs.region
# imported from `validation_nc` repo
assert "World" in obs.region
# added via general-config definitions
assert "Austria" in obs.region
Expand Down

0 comments on commit 7cccbd3

Please sign in to comment.