Skip to content

Commit

Permalink
Replace os.path with pathlib in filetype check
Browse files Browse the repository at this point in the history
  • Loading branch information
emolter committed Feb 25, 2025
1 parent 26c9f0f commit 7e4df44
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/stdatamodels/filetype.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path


def check(init):
Expand All @@ -18,18 +18,20 @@ def check(init):
supported = ("asdf", "fits", "json")

if isinstance(init, str):
path, ext = os.path.splitext(init) # noqa: PTH122
ext = ext.strip(".")
path = Path(init)

if not ext:
if len(path.suffixes) == 0:
raise ValueError(f"Input file path does not have an extension: {init}")

ext = path.suffixes[-1].strip(".")
if ext not in supported: # Could be the file is zipped; try splitting again
path, ext = os.path.splitext(path) # noqa: PTH122
ext = ext.strip(".")

err_msg = f"Unrecognized file type for: {init}"
try:
ext = path.suffixes[-2].strip(".")
except IndexError:
raise ValueError(err_msg) from None
if ext not in supported:
raise ValueError(f"Unrecognized file type for: {init}")
raise ValueError(err_msg) from None

Check warning on line 34 in src/stdatamodels/filetype.py

View check run for this annotation

Codecov / codecov/patch

src/stdatamodels/filetype.py#L34

Added line #L34 was not covered by tests

if ext == "json": # Assume json input is an association
return "asn"
Expand Down
1 change: 1 addition & 0 deletions tests/test_filetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
("test_file.json.gz", "asn"),
("test_file.asdf", "asdf"),
("test_file.asdf.gz", "asdf"),
("test_file.asdf.fits", "fits"),
("stpipe.MyPipeline.fits", "fits"),
("stpipe.MyPipeline.fits.gz", "fits"),
],
Expand Down

0 comments on commit 7e4df44

Please sign in to comment.