Skip to content

Commit

Permalink
Fix for file names containing dots
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Heitzmann Gabrielli <lucas@flexcompute.com>
  • Loading branch information
lucas-flexcompute committed Nov 15, 2023
1 parent 77f8076 commit 2828687
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Proper equality checking between `Tidy3dBaseModel` instances, which takes `DataArray` values and coords into account and handles `np.ndarray` types.
- Correctly set the contour length scale when exporting 2D (or 1D) structures with custom medium to GDSII.
- Improved error handling if file can not be downloaded from server.
- Fix for detection of file extensions for file names with dots.

## [2.5.0rc2] - 2023-10-30

Expand Down
21 changes: 14 additions & 7 deletions tidy3d/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,20 @@ def ndarray_encoder(val):

def _get_valid_extension(fname: str) -> str:
"""Return the file extension from fname, validated to accepted ones."""
extension = "".join(pathlib.Path(fname).suffixes).lower()
valid_extensions = [".json", ".yaml", ".hdf5", ".hdf5.gz", ".h5"]
if extension not in valid_extensions:
raise FileError(
f"{fname}::File extension must be in {valid_extensions}, but '{extension}' given"
)
return extension
valid_extensions = [".json", ".yaml", ".hdf5", ".h5", ".hdf5.gz"]
extensions = [s.lower() for s in pathlib.Path(fname).suffixes[-2:]]
if len(extensions) == 0:
raise FileError(f"File '{fname}' missing extension.")
single_extension = extensions[-1]
if single_extension in valid_extensions:
return single_extension
double_extension = "".join(extensions)
if double_extension in valid_extensions:
return double_extension
raise FileError(
f"File extension must be one of {', '.join(valid_extensions)}; file '{fname}' does not "
"match any of those."
)


class Tidy3dBaseModel(pydantic.BaseModel):
Expand Down

0 comments on commit 2828687

Please sign in to comment.