Skip to content

Commit

Permalink
Merge pull request #218 from ocefpaf/fix_context_loader
Browse files Browse the repository at this point in the history
Fix context loader
  • Loading branch information
ocefpaf authored Nov 5, 2024
2 parents db0bbf7 + 3e6ff26 commit 6cfd320
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repos:
- id: add-trailing-comma

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
rev: v0.7.2
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
Expand All @@ -62,7 +62,7 @@ repos:
- id: nb-strip-paths

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.4
rev: v2.5.0
hooks:
- id: pyproject-fmt

Expand Down
41 changes: 22 additions & 19 deletions ctd/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,32 @@ def _normalize_names(name: str) -> str:
def _open_compressed(fname: Path) -> str:
"""Open compressed gzip, gz, zip or bz2 files."""
extension = fname.suffix.casefold()
if extension in [".gzip", ".gz"]:
cfile = gzip.open(str(fname))
elif extension == ".bz2":
cfile = bz2.BZ2File(str(fname))
elif extension == ".zip":
loaders = {
".gzip": gzip.open,
".gz": gzip.open,
".bz2": bz2.BZ2File,
".zip": zipfile.ZipFile,
}
loader = loaders.get(extension)
if loader is None:
valid = ", ".join(loaders.keys())
msg = (
"Unrecognized file extension. "
f"Expected {valid}, got {extension}."
)
raise ValueError(msg)

if extension == ".zip":
# NOTE: Zip format may contain more than one file in the archive
# (similar to tar), here we assume that there is just one file per
# zipfile! Also, we ask for the name because it can be different from
# the zipfile file!!
zfile = zipfile.ZipFile(str(fname))
name = zfile.namelist()[0]
cfile = zfile.open(name)
else:
msg = (
"Unrecognized file extension. "
f"Expected .gzip, .bz2, or .zip, got {extension}"
)
raise ValueError(
msg,
)
contents = cfile.read()
cfile.close()
return contents
with loader(str(fname)) as zfile:
name = zfile.namelist()[0]
with zfile.open(name) as cfile:
return cfile.read()
with loader(str(fname)) as cfile:
return cfile.read()


def _read_file(fname: str | Path | StringIO) -> StringIO:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ classifiers = [
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
dynamic = [
"dependencies",
Expand Down

0 comments on commit 6cfd320

Please sign in to comment.