Skip to content

Commit

Permalink
Merge pull request #553 from idaholab/stream_reader
Browse files Browse the repository at this point in the history
Make a stream readable.
  • Loading branch information
MicahGale authored Oct 7, 2024
2 parents 2b510c0 + b80032e commit ab6c57d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
7 changes: 7 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
MontePy Changelog
=================

#Next Version#
--------------

**Bug Fixes**

* Fixed bug where file streams couldn't actually be read (:pull:`553`).

0.4.1
--------------

Expand Down
8 changes: 7 additions & 1 deletion montepy/input_parser/input_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, path, parent_file=None, overwrite=False):
self._overwrite = overwrite
self._mode = None
self._fh = None
self._is_stream = False

@classmethod
def from_open_stream(cls, fh):
Expand All @@ -43,6 +44,7 @@ def from_open_stream(cls, fh):
name = getattr(fh, "name", fh.__class__.__name__)
inpfile = cls(path=name)
inpfile._fh = fh
inpfile._is_stream = True
return inpfile

@make_prop_pointer("_path")
Expand All @@ -56,7 +58,11 @@ def path(self):

@property
def name(self):
return self.path
return self._path

@property
def is_stream(self):
return self._is_stream

@make_prop_pointer("_parent_file")
def parent_file(self):
Expand Down
6 changes: 5 additions & 1 deletion montepy/input_parser/input_syntax_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def read_input_syntax(input_file, mcnp_version=DEFAULT_VERSION, replace=True):
"""
global reading_queue
reading_queue = deque()
with input_file.open("r", replace=replace) as fh:
if input_file.is_stream:
context = input_file
else:
context = input_file.open("r", replace=replace)
with context as fh:
yield from read_front_matters(fh, mcnp_version)
yield from read_data(fh, mcnp_version)

Expand Down
4 changes: 3 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,10 +1106,12 @@ def test_read_write_cycle(file):
problem = montepy.read_input(file)
SKIPPERS = _SKIP_LINES.get(str(file), {})
fh = io.StringIO()
# make string unclosable to keep open after reading.
fh.close = lambda: None
problem.write_problem(fh)
fh.seek(0)
# test valid syntax
new_problem = montepy.MCNP_Problem("foo")
new_problem = montepy.read_input(fh)
# verify lines are similar
fh.seek(0)
lines = [line.rstrip() for line in fh]
Expand Down

0 comments on commit ab6c57d

Please sign in to comment.