Skip to content

Commit 6cfd320

Browse files
authored
Merge pull request #218 from ocefpaf/fix_context_loader
Fix context loader
2 parents db0bbf7 + 3e6ff26 commit 6cfd320

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repos:
3939
- id: add-trailing-comma
4040

4141
- repo: https://github.com/astral-sh/ruff-pre-commit
42-
rev: v0.6.9
42+
rev: v0.7.2
4343
hooks:
4444
- id: ruff
4545
args: ["--fix", "--show-fixes"]
@@ -62,7 +62,7 @@ repos:
6262
- id: nb-strip-paths
6363

6464
- repo: https://github.com/tox-dev/pyproject-fmt
65-
rev: 2.2.4
65+
rev: v2.5.0
6666
hooks:
6767
- id: pyproject-fmt
6868

ctd/read.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,32 @@ def _normalize_names(name: str) -> str:
3636
def _open_compressed(fname: Path) -> str:
3737
"""Open compressed gzip, gz, zip or bz2 files."""
3838
extension = fname.suffix.casefold()
39-
if extension in [".gzip", ".gz"]:
40-
cfile = gzip.open(str(fname))
41-
elif extension == ".bz2":
42-
cfile = bz2.BZ2File(str(fname))
43-
elif extension == ".zip":
39+
loaders = {
40+
".gzip": gzip.open,
41+
".gz": gzip.open,
42+
".bz2": bz2.BZ2File,
43+
".zip": zipfile.ZipFile,
44+
}
45+
loader = loaders.get(extension)
46+
if loader is None:
47+
valid = ", ".join(loaders.keys())
48+
msg = (
49+
"Unrecognized file extension. "
50+
f"Expected {valid}, got {extension}."
51+
)
52+
raise ValueError(msg)
53+
54+
if extension == ".zip":
4455
# NOTE: Zip format may contain more than one file in the archive
4556
# (similar to tar), here we assume that there is just one file per
4657
# zipfile! Also, we ask for the name because it can be different from
4758
# the zipfile file!!
48-
zfile = zipfile.ZipFile(str(fname))
49-
name = zfile.namelist()[0]
50-
cfile = zfile.open(name)
51-
else:
52-
msg = (
53-
"Unrecognized file extension. "
54-
f"Expected .gzip, .bz2, or .zip, got {extension}"
55-
)
56-
raise ValueError(
57-
msg,
58-
)
59-
contents = cfile.read()
60-
cfile.close()
61-
return contents
59+
with loader(str(fname)) as zfile:
60+
name = zfile.namelist()[0]
61+
with zfile.open(name) as cfile:
62+
return cfile.read()
63+
with loader(str(fname)) as cfile:
64+
return cfile.read()
6265

6366

6467
def _read_file(fname: str | Path | StringIO) -> StringIO:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ classifiers = [
1818
"Programming Language :: Python :: 3 :: Only",
1919
"Programming Language :: Python :: 3.11",
2020
"Programming Language :: Python :: 3.12",
21+
"Programming Language :: Python :: 3.13",
2122
]
2223
dynamic = [
2324
"dependencies",

0 commit comments

Comments
 (0)