diff --git a/src/physrisk/vulnerability_models/configuration/config_builders.py b/src/physrisk/vulnerability_models/configuration/config_builders.py index 2fa71763..1e8d2e94 100644 --- a/src/physrisk/vulnerability_models/configuration/config_builders.py +++ b/src/physrisk/vulnerability_models/configuration/config_builders.py @@ -18,10 +18,11 @@ ) -def build_all_config(): +def build_all_config(source_dir: Optional[Path] = None): """Build all of the configuration.""" config_items: list[VulnerabilityConfigItem] = [] - config_builders = [ConfigBuilderJRCFlood()] + source_dir = vulnerability_onboarding_dir() if source_dir is None else source_dir + config_builders = [ConfigBuilderJRCFlood(source_dir=source_dir)] for builder in config_builders: config_items += builder.build_config() config_items_to_csv( @@ -29,6 +30,18 @@ def build_all_config(): ) +def vulnerability_onboarding_dir(): + """Path of the vulnerability on-boarding directory.""" + path = ( + Path(__file__).parents[4] + / "docs" + / "user_guide" + / "vulnerability" + / "vulnerability_functions" + ) + return path + + def vulnerability_config_dir(): """Path of the vulnerability configuration directory.""" path = Path(__file__).parents[2] / "data" / "static" / "vulnerability" @@ -40,9 +53,7 @@ def build_config(self) -> List[VulnerabilityConfigItem]: ... class ConfigBuilderBase: - def __init__( - self, source_dir: Optional[str] = None, fs: Optional[AbstractFileSystem] = None - ): + def __init__(self, source_dir: Path, fs: Optional[AbstractFileSystem] = None): """_summary_ Args: @@ -54,22 +65,7 @@ def __init__( Defaults to None, in which case a LocalFileSystem is assumed. """ self.fs = fs if fs is not None else LocalFileSystem() - self.source_dir = ( - Path(source_dir) - if source_dir is not None - else self.vulnerability_onboarding_dir() - ) - - def vulnerability_onboarding_dir(self): - """Path of the vulnerability on-boarding directory.""" - path = ( - Path(__file__).parents[4] - / "docs" - / "user_guide" - / "vulnerability" - / "vulnerability_functions" - ) - return path + self.source_dir = source_dir class ConfigBuilderJRCFlood(ConfigBuilderBase, ConfigBuilder): @@ -98,7 +94,7 @@ def build_config(self): "Industrial buildings": "Buildings/Industrial", } - config_items: list[VulnerabilityConfigItem] = [] + config_items: List[VulnerabilityConfigItem] = [] for mapping in type_mappings: type_df = df[df["Type"] == mapping] flood_depth = type_df["Flood depth [m]"].to_numpy() diff --git a/tests/vulnerability_models/config_generate_test.py b/tests/vulnerability_models/config_generate_test.py index 7f041d3d..333cbdc6 100644 --- a/tests/vulnerability_models/config_generate_test.py +++ b/tests/vulnerability_models/config_generate_test.py @@ -1,5 +1,16 @@ +from pathlib import Path from physrisk.vulnerability_models.configuration.config_builders import build_all_config def test_generate_all_config(): - build_all_config() + # if this is run against the library, e.g. via tox, docs data will not be available + source_dir = ( + Path(__file__).parents[2] + / "docs" + / "user_guide" + / "vulnerability" + / "vulnerability_functions" + / "vulnerability_functions" + ) + if source_dir.exists(): + build_all_config(source_dir)