From 21682674e687e02ab509904491c3debf1a007ea1 Mon Sep 17 00:00:00 2001 From: AminTorabi-NOAA Date: Fri, 30 Aug 2024 13:08:00 -0400 Subject: [PATCH 1/2] if the folder doesn't exist --- src/troute-config/troute/config/output_parameters.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/troute-config/troute/config/output_parameters.py b/src/troute-config/troute/config/output_parameters.py index f56c89f6a..e9dc00cf1 100644 --- a/src/troute-config/troute/config/output_parameters.py +++ b/src/troute-config/troute/config/output_parameters.py @@ -1,6 +1,6 @@ from pathlib import Path from pydantic import BaseModel, Field, validator - +import os from typing import Optional, List from typing_extensions import Annotated, Literal from .types import FilePath, DirectoryPath @@ -93,11 +93,19 @@ class ParityCheckCompareFileSet(BaseModel): class StreamOutput(BaseModel): # NOTE: required if writing StreamOutput files - stream_output_directory: Optional[DirectoryPath] = None + stream_output_directory: Optional[str] = None mask_output: Optional[FilePath] = None stream_output_time: int = 1 stream_output_type:streamOutput_allowedTypes = ".nc" stream_output_internal_frequency: Annotated[int, Field(strict=True, ge=5)] = 5 + + @validator('stream_output_directory', pre=True, always=True) + def validate_stream_output_directory(cls, value): + if value: + if not os.path.exists(value): + os.makedirs(value) + return value + @validator('stream_output_internal_frequency') def validate_stream_output_internal_frequency(cls, value, values): if value is not None: From 639a160a3a397e4f3893477103fdb6de4e9fc917 Mon Sep 17 00:00:00 2001 From: AminTorabi-NOAA Date: Thu, 5 Sep 2024 16:36:36 -0400 Subject: [PATCH 2/2] handling edge cases --- .../troute/config/output_parameters.py | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/troute-config/troute/config/output_parameters.py b/src/troute-config/troute/config/output_parameters.py index e9dc00cf1..78128401e 100644 --- a/src/troute-config/troute/config/output_parameters.py +++ b/src/troute-config/troute/config/output_parameters.py @@ -1,6 +1,6 @@ from pathlib import Path from pydantic import BaseModel, Field, validator -import os + from typing import Optional, List from typing_extensions import Annotated, Literal from .types import FilePath, DirectoryPath @@ -93,17 +93,25 @@ class ParityCheckCompareFileSet(BaseModel): class StreamOutput(BaseModel): # NOTE: required if writing StreamOutput files - stream_output_directory: Optional[str] = None + stream_output_directory: Optional[Path] = None mask_output: Optional[FilePath] = None stream_output_time: int = 1 stream_output_type:streamOutput_allowedTypes = ".nc" stream_output_internal_frequency: Annotated[int, Field(strict=True, ge=5)] = 5 - @validator('stream_output_directory', pre=True, always=True) + @validator('stream_output_directory') def validate_stream_output_directory(cls, value): - if value: - if not os.path.exists(value): - os.makedirs(value) + if value is None: + return None + + # expand ~/output/dir -> /home/user/output/dir + value = value.expanduser() + + if value.exists() and not value.is_dir(): + raise ValueError(f"'stream_output_directory'={value!s} is a file, expected directory.") + + # make directory (and intermediates) if they don't exist + value.mkdir(parents=True, exist_ok=True) return value @validator('stream_output_internal_frequency')