Skip to content

Commit 567d3dd

Browse files
committed
instead of failing, files woith no parser are now returned as strings in
VaspDir.
1 parent aa37e99 commit 567d3dd

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/pymatgen/io/vasp/outputs.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5672,15 +5672,20 @@ def __iter__(self):
56725672
def __getitem__(self, item):
56735673
if item in self._parsed_files:
56745674
return self._parsed_files[item]
5675+
fpath = self.path / item
5676+
5677+
if not (self.path / item).exists():
5678+
raise ValueError(f"{item} not found in {self.path}. List of files are {self.files}.")
5679+
56755680
for k, cls_ in VaspDir.FILE_MAPPINGS.items():
56765681
if k in item:
56775682
try:
5678-
self._parsed_files[item] = cls_.from_file(self.path / item)
5683+
self._parsed_files[item] = cls_.from_file(fpath)
56795684
except AttributeError:
5680-
self._parsed_files[item] = cls_(self.path / item)
5685+
self._parsed_files[item] = cls_(fpath)
56815686

56825687
return self._parsed_files[item]
5683-
if (self.path / item).exists():
5684-
raise RuntimeError(f"Unable to parse {item}. Supported files are {list(VaspDir.FILE_MAPPINGS.keys())}.")
56855688

5686-
raise ValueError(f"{item} not found in {self.path}. List of files are {self.files}.")
5689+
warnings.warn(f"No parser defined for {item}. Full text of file is returned as a string.", UserWarning)
5690+
with zopen(fpath, "rt") as f:
5691+
return f.read()

tests/io/vasp/test_outputs.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,12 +2110,14 @@ def test_getitem(self):
21102110
d = VaspDir(f"{TEST_FILES_DIR}/io/vasp/fixtures/relaxation")
21112111
assert len(d) == 5
21122112
assert d["OUTCAR"].run_stats["cores"] == 8
2113-
d = VaspDir(f"{TEST_FILES_DIR}/io/vasp/outputs")
2114-
with pytest.raises(RuntimeError, match=r"Unable to parse IBZKPT.*"):
2115-
d["IBZKPT.lobster"]
2113+
21162114
d = VaspDir(f"{TEST_FILES_DIR}/io/vasp/fixtures/scan_relaxation")
21172115
assert len(d) == 2
21182116
assert d["vasprun.xml.gz"].incar["METAGGA"] == "R2scan"
21192117

21202118
with pytest.raises(ValueError, match="hello not found"):
21212119
d["hello"]
2120+
2121+
d = VaspDir(f"{TEST_FILES_DIR}/io/vasp/outputs")
2122+
with pytest.warns(UserWarning, match=r"No parser defined for IBZKPT.*"):
2123+
assert isinstance(d["IBZKPT.lobster"], str)

0 commit comments

Comments
 (0)