Skip to content

Commit

Permalink
Fix bug running with tox.
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Moorhouse <[email protected]>
  • Loading branch information
joemoorhouse committed Oct 20, 2024
1 parent efe8427 commit 71c77f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
40 changes: 18 additions & 22 deletions src/physrisk/vulnerability_models/configuration/config_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,30 @@
)


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(
config_items, vulnerability_config_dir() / "candidate_vulnerability_config.csv"
)


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"
Expand All @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 12 additions & 1 deletion tests/vulnerability_models/config_generate_test.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 71c77f3

Please sign in to comment.