From 3c223248a80043b12e700b09f1e32ebf54f6ebd3 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 11 Jun 2019 11:51:34 -0400 Subject: [PATCH] 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 --- openmc/data/library.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/openmc/data/library.py b/openmc/data/library.py index 905dbbe4a7..e090b2062f 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -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'):