Skip to content

Commit

Permalink
handling edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AminTorabi-NOAA authored and aaraney committed Sep 5, 2024
1 parent 6f8e276 commit 5d5bca1
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/troute-config/troute/config/output_parameters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 5d5bca1

Please sign in to comment.