Skip to content

Commit ef18efc

Browse files
authored
Fix to support cdflib v1.0 (#94)
* Fix to support cdflib v1.0 * Replace generic exception with FileExistsError
1 parent fcf1701 commit ef18efc

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

docs/release_notes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Release notes
44
Change log
55
----------
66

7+
Changes from 0.11.1 to 0.11.2
8+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
10+
- Fix support for cdflib version 1.0
11+
712
Changes from 0.11.0 to 0.11.1
813
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
914

src/viresclient/_data_handling.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(self, file, filetype="cdf"):
100100
globalatts.get("MAGNETIC_MODELS", [])
101101
)
102102
self.data_filters = self._ensure_list(globalatts.get("DATA_FILTERS", []))
103-
self.variables = self._cdf.cdf_info()["zVariables"]
103+
self.variables = self._get_attr_or_key(self._cdf.cdf_info(), "zVariables")
104104
self._varatts = {var: self._cdf.varattsget(var) for var in self.variables}
105105
self._varinfo = {var: self._cdf.varinq(var) for var in self.variables}
106106
else:
@@ -110,7 +110,10 @@ def __enter__(self):
110110
return self
111111

112112
def __exit__(self, *args):
113-
self._cdf.close()
113+
try:
114+
self._cdf.close()
115+
except AttributeError:
116+
pass
114117

115118
@staticmethod
116119
def _open_cdf(file):
@@ -130,6 +133,15 @@ def _ensure_list(attribute):
130133
else:
131134
return attribute
132135

136+
@staticmethod
137+
def _get_attr_or_key(obj, attr):
138+
# Used to work around cdflib<1 & >=1 support
139+
# cdflib>=1 introduces dataclasses in place of some dicts
140+
if isinstance(obj, dict):
141+
return obj.get(attr, None)
142+
else:
143+
return getattr(obj, attr, None)
144+
133145
def get_variable(self, var):
134146
try:
135147
data = self._cdf.varget(var)
@@ -149,10 +161,10 @@ def get_variable_description(self, var):
149161
return desc if desc else catdesc
150162

151163
def get_variable_numdims(self, var):
152-
return self._varinfo[var].get("Num_Dims")
164+
return self._get_attr_or_key(self._varinfo[var], "Num_Dims")
153165

154166
def get_variable_dimsizes(self, var):
155-
return self._varinfo[var].get("Dim_Sizes")
167+
return self._get_attr_or_key(self._varinfo[var], "Dim_Sizes")
156168

157169
@staticmethod
158170
def _cdftime_to_datetime(t):
@@ -467,7 +479,9 @@ def _check_outfile(path, path_extension, overwrite=False):
467479
if path.split(".")[-1].lower() != path_extension:
468480
raise TypeError(f"Filename extension should be {path_extension}")
469481
if os.path.isfile(path) and not overwrite:
470-
raise Exception("File not written as it already exists and overwrite=False")
482+
raise FileExistsError(
483+
"File not written as it already exists and overwrite=False"
484+
)
471485

472486
def to_file(self, path, overwrite=False):
473487
"""Saves the data to the specified file.

tests/test_ReturnedData.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ def test_ReturnedDataFile_saving(tmpfile):
8585
# Check that not overwriting and overwriting work right
8686
testfile = str(tmpfile(f"testfile.{filetype}"))
8787
retdata.to_file(testfile)
88-
# with pytest.raises(FileExistsError): # not in py27
89-
with pytest.raises(Exception):
88+
with pytest.raises(FileExistsError):
9089
retdata.to_file(testfile, overwrite=False)
9190
retdata.to_file(testfile, overwrite=True)
9291

@@ -115,14 +114,13 @@ def test_ReturnedData_saving(tmpfile):
115114
# Check that not overwriting and overwriting work right
116115
testfile = str(tmpfile(f"testfile.{filetype}"))
117116
retdata.to_file(testfile)
118-
# with pytest.raises(FileExistsError): # not in py27
119-
with pytest.raises(Exception):
117+
with pytest.raises(FileExistsError):
120118
retdata.to_file(testfile, overwrite=False)
121119
retdata.to_file(testfile, overwrite=True)
122120
# repeat for .to_files()
123121
testfile = str(tmpfile(f"testfile2.{filetype}"))
124122
retdata.to_files([testfile])
125-
with pytest.raises(Exception):
123+
with pytest.raises(FileExistsError):
126124
retdata.to_files([testfile], overwrite=False)
127125
retdata.to_files([testfile], overwrite=True)
128126

0 commit comments

Comments
 (0)