Skip to content
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

Merged
merged 2 commits into from
Sep 5, 2024

Conversation

AminTorabi-NOAA
Copy link
Contributor

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

  • PR has an informative and human-readable title
  • Changes are limited to a single goal (no scope creep)
  • Code can be automatically merged (no conflicts)
  • Code follows project standards (link if applicable)
  • Passes all existing automated tests
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future todos are captured in comments
  • Visually tested in supported browsers and devices (see checklist below 👇)
  • Project documentation has been updated (including the "Unreleased" section of the CHANGELOG)
  • Reviewers requested with the Reviewers tool ➡️

Testing checklist

Target Environment support

  • Windows
  • Linux
  • Browser

Accessibility

  • Keyboard friendly
  • Screen reader friendly

Other

  • Is useable without CSS
  • Is useable without JS
  • Flexible from small to large screens
  • No linting errors or warnings
  • JavaScript tests are passing

Copy link
Member

@aaraney aaraney left a 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
Copy link
Member

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.

Suggested change
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)
Copy link
Member

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 a pathlib.Path (drop pre=True and always=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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be dropped.

@AminTorabi-NOAA
Copy link
Contributor Author

I addressed all. Thanks for the comments @aaraney

Copy link
Member

@aaraney aaraney left a 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! 🎉

@aaraney aaraney merged commit 5d5bca1 into NOAA-OWP:master Sep 5, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants