-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
if the folder doesn't exist #835
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for writing this up, @AminTorabi-NOAA! I left a few change requests that cover a few possible edge cases. Once these have been added, we should be good to go! Thanks again!
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's still require if this exists, that it is a pathlib.Path
object.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few minor improvements:
- We only need to validate after
pydantic
has coerced the input into apathlib.Path
(droppre=True
andalways=True
) - Handle if we got an unexpanded path (e.g.
~/output/dir
) - Raise if we got a file
- Create dir if it doesn't exist, and all intermediate directories
@validator('stream_output_directory')
def validate_stream_output_directory(cls, 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
@@ -1,6 +1,6 @@ | |||
from pathlib import Path | |||
from pydantic import BaseModel, Field, validator | |||
|
|||
import os |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be dropped.
I addressed all. Thanks for the comments @aaraney |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thanks @AminTorabi-NOAA! 🎉
In the current implementation, if the user specifies a directory for
stream_output_directory
(e.g.,output/
) that doesn't exist, it results in an error. With the new PR, the code automatically creates the specified directory if it doesn't exist, allowing the output to be saved in that folder without any issues.Additions
Removals
Changes
Testing
Screenshots
Notes
Todos
Checklist
Testing checklist
Target Environment support
Accessibility
Other