Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a stream readable. #553

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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