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

Fix _processIncludes to take StringIO argument #1333

Merged
merged 5 commits into from
Jul 18, 2023
Merged
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
4 changes: 2 additions & 2 deletions armi/utils/tests/resources/lower/includeA.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
full_name: Jennifer Person
# some comment in includeA
children:
!include includeB.yaml
children:
!include includeB.yaml
12 changes: 6 additions & 6 deletions armi/utils/tests/resources/root.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Behold, the Person family
bobby: &bobby
full_name: Robert Person
full_name: Robert Person

billy:
full_name: William Person
# comment
children:
- *bobby
- !include lower/includeA.yaml
full_name: William Person
# comment
children:
- *bobby
- !include lower/includeA.yaml
21 changes: 19 additions & 2 deletions armi/utils/tests/test_textProcessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for functions in textProcessors.py."""
from io import StringIO, TextIOWrapper, TextIOBase
import os
import pathlib
import unittest

import ruamel
import unittest

from armi.utils import textProcessors

Expand Down Expand Up @@ -60,6 +60,23 @@ def test_resolveIncludes(self):
self.assertTrue(commentFound)
self.assertTrue(anchorFound)

def test_resolveIncludes_StringIO(self):
"""Tests that resolveMarkupInclusions handles StringIO input"""
yaml = ruamel.yaml.YAML()
with open(os.path.join(RES_DIR, "root.yaml")) as f:
loadedYaml = yaml.load(f)
stringIO = StringIO()
yaml.dump(loadedYaml, stringIO)
resolved = textProcessors.resolveMarkupInclusions(
src=stringIO, root=pathlib.Path(RES_DIR)
)
with open(os.path.join(RES_DIR, "root.yaml")) as f:
expected = textProcessors.resolveMarkupInclusions(
f, root=pathlib.Path(RES_DIR)
)
# strip it because one method gives an extra newline we don't care about
self.assertEqual(resolved.getvalue().strip(), expected.getvalue().strip())

def test_findIncludes(self):
includes = textProcessors.findYamlInclusions(
pathlib.Path(RES_DIR) / "root.yaml"
Expand Down
14 changes: 10 additions & 4 deletions armi/utils/textProcessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def __str__(self):


def _processIncludes(
src,
src: Union[TextIO, pathlib.Path],
out,
includes: List[Tuple[pathlib.Path, FileMark]],
root,
root: pathlib.Path,
indentation=0,
currentFile="<stream>",
):
Expand All @@ -88,7 +88,13 @@ def _beginningOfContent(line: str) -> int:
return 0

indentSpace = " " * indentation
for i, line in enumerate(src.readlines()):
if hasattr(src, "getvalue"):
# assume stringIO
lines = [l + "\n" for l in src.getvalue().split("\n")]
else:
# assume file stream or TextIOBase, and it has a readlines attr
lines = src.readlines()
for i, line in enumerate(lines):
leadingSpace = indentSpace if i > 0 else ""
m = _INCLUDE_RE.match(line)
if m:
Expand Down Expand Up @@ -134,7 +140,7 @@ def resolveMarkupInclusions(

Parameters
----------
src : TextIOBase or Path
src : StringIO or TextIOBase/Path
If a Path is provided, read text from there. If is stream is provided, consume
text from the stream. If a stream is provided, ``root`` must also be provided.
root : Optional Path
Expand Down
1 change: 1 addition & 0 deletions doc/release/0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ What's new in ARMI

Bug fixes
---------
#. Fix _processIncludes to handle StringIO input (`PR#1333 <https://github.com/terrapower/armi/pull/1333>`_)
#. TBD

ARMI v0.2.8
Expand Down
Loading