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

Fix context loader #218

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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