Support pathlib in openmc.data

This commit is contained in:
Paul Romano 2018-11-02 07:34:34 -05:00
parent 0bd3448193
commit 05e66155a2
8 changed files with 25 additions and 18 deletions

View file

@ -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)])

View file

@ -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()

View file

@ -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()

View file

@ -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)

View file

@ -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:

View file

@ -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

View file

@ -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)

View file

@ -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: