Support Path objects for DataLibrary.register_filename

HDF5 File object supports reading from file-like objects,
which includes pathlib.Path.
Retain the filename of the path in the libraries list
This commit is contained in:
Andrew Johnson 2019-06-11 11:51:34 -04:00
parent f700861646
commit 3c223248a8
No known key found for this signature in database
GPG key ID: 253418E91B7F6FEB

View file

@ -1,5 +1,6 @@
import os
import xml.etree.ElementTree as ET
import pathlib
import h5py
@ -48,19 +49,20 @@ class DataLibrary(EqualityMixin):
Parameters
----------
filename : str
filename : str or Path
Path to the file to be registered.
"""
# Support pathlib
# TODO: Remove when support is Python 3.6+ only
filename = str(filename)
if not isinstance(filename, pathlib.Path):
path = pathlib.Path(filename)
else:
path = filename
with h5py.File(filename, 'r') as h5file:
with h5py.File(path, 'r') as h5file:
filetype = h5file.attrs['filetype'].decode()[5:]
materials = list(h5file)
library = {'path': filename, 'type': filetype, 'materials': materials}
library = {'path': path.name, 'type': filetype, 'materials': materials}
self.libraries.append(library)
def export_to_xml(self, path='cross_sections.xml'):