From 05e66155a2587b51460de4f9d4e04f9a50744762 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 2 Nov 2018 07:34:34 -0500 Subject: [PATCH] Support pathlib in openmc.data --- openmc/data/ace.py | 7 ++++--- openmc/data/endf.py | 2 +- openmc/data/library.py | 12 +++++++++--- openmc/data/multipole.py | 4 ++-- openmc/data/neutron.py | 4 ++-- openmc/data/njoy.py | 8 ++++---- openmc/data/photon.py | 2 +- openmc/data/thermal.py | 4 ++-- 8 files changed, 25 insertions(+), 18 deletions(-) diff --git a/openmc/data/ace.py b/openmc/data/ace.py index 41a7057acb..f44f8931c6 100644 --- a/openmc/data/ace.py +++ b/openmc/data/ace.py @@ -106,7 +106,7 @@ def ascii_to_binary(ascii_file, binary_file): """ # Open ASCII file - ascii = open(ascii_file, 'r') + ascii = open(str(ascii_file), 'r') # Set default record length record_length = 4096 @@ -116,7 +116,7 @@ def ascii_to_binary(ascii_file, binary_file): ascii.close() # Open binary file - binary = open(binary_file, 'wb') + binary = open(str(binary_file), 'wb') idx = 0 @@ -228,8 +228,9 @@ class Library(EqualityMixin): self.tables = [] # Determine whether file is ASCII or binary + filename = str(filename) try: - fh = open(str(filename), 'rb') + fh = open(filename, 'rb') # Grab 10 lines of the library sb = b''.join([fh.readline() for i in range(10)]) diff --git a/openmc/data/endf.py b/openmc/data/endf.py index 4c3cb6d470..4340aeca02 100644 --- a/openmc/data/endf.py +++ b/openmc/data/endf.py @@ -348,7 +348,7 @@ def get_evaluations(filename): """ evaluations = [] - with open(filename, 'r') as fh: + with open(str(filename), 'r') as fh: while True: pos = fh.tell() line = fh.readline() diff --git a/openmc/data/library.py b/openmc/data/library.py index 1a797aa53e..41bcd58a7b 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -52,6 +52,10 @@ class DataLibrary(EqualityMixin): Path to the file to be registered. """ + # Support pathlib + # TODO: Remove when support is Python 3.6+ only + filename = str(filename) + with h5py.File(filename, 'r') as h5file: filetype = h5file.attrs['filetype'].decode().lstrip('data_') materials = list(h5file) @@ -76,7 +80,7 @@ class DataLibrary(EqualityMixin): if common_dir == '': common_dir = '.' - if os.path.relpath(common_dir, os.path.dirname(path)) != '.': + if os.path.relpath(common_dir, os.path.dirname(str(path))) != '.': dir_element = ET.SubElement(root, "directory") dir_element.text = os.path.realpath(common_dir) @@ -91,7 +95,7 @@ class DataLibrary(EqualityMixin): # Write XML file tree = ET.ElementTree(root) - tree.write(path, xml_declaration=True, encoding='utf-8', + tree.write(str(path), xml_declaration=True, encoding='utf-8', method='xml') @classmethod @@ -123,7 +127,9 @@ class DataLibrary(EqualityMixin): raise ValueError("Either path or OPENMC_CROSS_SECTIONS " "environmental variable must be set") - check_type('path', path, str) + # Convert to string to support pathlib + # TODO: Remove when support is Python 3.6+ only + path = str(path) tree = ET.parse(path) root = tree.getroot() diff --git a/openmc/data/multipole.py b/openmc/data/multipole.py index 5d34c9ff6e..9022061a09 100644 --- a/openmc/data/multipole.py +++ b/openmc/data/multipole.py @@ -333,7 +333,7 @@ class WindowedMultipole(EqualityMixin): if isinstance(group_or_filename, h5py.Group): group = group_or_filename else: - h5file = h5py.File(group_or_filename, 'r') + h5file = h5py.File(str(group_or_filename), 'r') # Make sure version matches if 'version' in h5file.attrs: @@ -515,7 +515,7 @@ class WindowedMultipole(EqualityMixin): """ # Open file and write version. - with h5py.File(path, mode, libver=libver) as f: + with h5py.File(str(path), mode, libver=libver) as f: f.attrs['filetype'] = np.string_('data_wmp') f.attrs['version'] = np.array(WMP_VERSION) diff --git a/openmc/data/neutron.py b/openmc/data/neutron.py index fb00bccb9d..1856a13d4b 100644 --- a/openmc/data/neutron.py +++ b/openmc/data/neutron.py @@ -446,7 +446,7 @@ class IncidentNeutron(EqualityMixin): 'originated from an ENDF file.') # Open file and write version - f = h5py.File(path, mode, libver=libver) + f = h5py.File(str(path), mode, libver=libver) f.attrs['filetype'] = np.string_('data_neutron') f.attrs['version'] = np.array(HDF5_VERSION) @@ -520,7 +520,7 @@ class IncidentNeutron(EqualityMixin): if isinstance(group_or_filename, h5py.Group): group = group_or_filename else: - h5file = h5py.File(group_or_filename, 'r') + h5file = h5py.File(str(group_or_filename), 'r') # Make sure version matches if 'version' in h5file.attrs: diff --git a/openmc/data/njoy.py b/openmc/data/njoy.py index b2894b3d1c..0c82f32f11 100644 --- a/openmc/data/njoy.py +++ b/openmc/data/njoy.py @@ -146,14 +146,14 @@ def run(commands, tapein, tapeout, input_filename=None, stdout=False, """ if input_filename is not None: - with open(input_filename, 'w') as f: + with open(str(input_filename), 'w') as f: f.write(commands) with tempfile.TemporaryDirectory() as tmpdir: # Copy evaluations to appropriates 'tapes' for tape_num, filename in tapein.items(): tmpfilename = os.path.join(tmpdir, 'tape{}'.format(tape_num)) - shutil.copy(filename, tmpfilename) + shutil.copy(str(filename), tmpfilename) # Start up NJOY process njoy = Popen([njoy_exec], cwd=tmpdir, stdin=PIPE, stdout=PIPE, @@ -182,7 +182,7 @@ def run(commands, tapein, tapeout, input_filename=None, stdout=False, for tape_num, filename in tapeout.items(): tmpfilename = os.path.join(tmpdir, 'tape{}'.format(tape_num)) if os.path.isfile(tmpfilename): - shutil.move(tmpfilename, filename) + shutil.move(tmpfilename, str(filename)) def make_pendf(filename, pendf='pendf', error=0.001, stdout=False): @@ -422,7 +422,7 @@ def make_ace_thermal(filename, filename_thermal, temperatures=None, commands = "" nendf, nthermal_endf, npendf = 20, 21, 22 - tapein = {nendf: filename, nthermal_endf:filename_thermal} + tapein = {nendf: filename, nthermal_endf: filename_thermal} tapeout = {} # reconr diff --git a/openmc/data/photon.py b/openmc/data/photon.py index e0f71d5ce5..08f8f085d7 100644 --- a/openmc/data/photon.py +++ b/openmc/data/photon.py @@ -668,7 +668,7 @@ class IncidentPhoton(EqualityMixin): """ # Open file and write version - f = h5py.File(path, mode, libver=libver) + f = h5py.File(str(path), mode, libver=libver) f.attrs['filetype'] = np.string_('data_photon') if 'version' not in f.attrs: f.attrs['version'] = np.array(HDF5_VERSION) diff --git a/openmc/data/thermal.py b/openmc/data/thermal.py index 23f02c90d9..b1e4e015fa 100644 --- a/openmc/data/thermal.py +++ b/openmc/data/thermal.py @@ -278,7 +278,7 @@ class ThermalScattering(EqualityMixin): """ # Open file and write version - f = h5py.File(path, mode, libver=libver) + f = h5py.File(str(path), mode, libver=libver) f.attrs['filetype'] = np.string_('data_thermal') f.attrs['version'] = np.array(HDF5_VERSION) @@ -387,7 +387,7 @@ class ThermalScattering(EqualityMixin): if isinstance(group_or_filename, h5py.Group): group = group_or_filename else: - h5file = h5py.File(group_or_filename, 'r') + h5file = h5py.File(str(group_or_filename), 'r') # Make sure version matches if 'version' in h5file.attrs: