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

resolveMarkupInclusions takes TextIOBase as input but can't take it all the way home #1329

Closed
opotowsky opened this issue Jun 26, 2023 · 3 comments · Fixed by #1333
Closed
Assignees
Labels
bug Something is wrong: Highest Priority

Comments

@opotowsky
Copy link
Member

opotowsky commented Jun 26, 2023

If I have code like so:

yaml = YAML()
loadedYaml = yaml.load(pathlib.Path(readPath))
#...skipping some important but unrelated stuff that is the reason for this approach...
yamlStream = TextIOBase(loadedYaml)
yamlStream = resolveMarkupInclusions(src=yamlStream, root=pathlib.Path(readPath).parent)

I get:

    yamlStream = resolveMarkupInclusions(
  File "<path>framework\armi\utils\textProcessors.py", line 187, in resolveMarkupInclusions
    return _resolveMarkupInclusions(src, root)[0]
  File "<path>\framework\armi\utils\textProcessors.py", line 247, in _resolveMarkupInclusions
    _processIncludes(src, out, includes, root)
  File "<path>\framework\armi\utils\textProcessors.py", line 96, in _processIncludes
    for i, line in enumerate(src.readlines()):
io.UnsupportedOperation: readline

resolveMarkupInclusions should be able to take either a filepath to a yaml, or a yaml TextIOBase "stream". But as you can see above, this line can't handle src being a TextIOBase object:

for i, line in enumerate(src.readlines()):

There is a nonzero chance the error is mine, but I've looked at the problem long enough that I'm comfortable making this issue.

@opotowsky
Copy link
Member Author

opotowsky commented Jun 26, 2023

If it makes any difference, I tried to edit the code snippet above to this (this was actually my first attempt):

yaml = YAML()
loadedYaml = yaml.load(pathlib.Path(readPath))
#...skipping some important but unrelated stuff that is the reason for this approach...
yamlStream = TextIOBase()
yaml.dump(loadedYaml, yamlStream)
yamlStream = resolveMarkupInclusions(src=yamlStream, root=pathlib.Path(readPath).parent)

And got:

    yaml.dump(preBuildData, TextIOWrapper(preYamlStream))
  File "<path>venv\lib\site-packages\ruamel\yaml\main.py", line 574, in dump
    return self.dump_all([data], stream, transform=transform)
# skip a bunch of the stack trace
  File "<path>venv\lib\site-packages\ruamel\yaml\emitter.py", line 1740, in write_comment
    self.stream.write(value)
io.UnsupportedOperation: not writable

This gets into troubleshooting my code territory and not something wrong with ARMI, but I think what I did in the issue description SHOULD work.

@john-science john-science added the bug Something is wrong: Highest Priority label Jun 27, 2023
@john-science
Copy link
Member

Arrielle and I have worked on this offline and found a good path forward. It appears to be a combination of two problems: one because an ARMI method has no type hints/docs, and one in ruaml.yaml not round-tripping comments in YAML files well.

Both are solvable, PR incoming.

@opotowsky
Copy link
Member Author

opotowsky commented Jun 28, 2023

For the round tripping of comments issue, I found a resolution in my script.

I was trying to do:

yaml.load(filepath)

which on Windows added a \r to the end of every comment. The workaround was stealing this code:

with open(src, "r") as rootFile:
src = io.StringIO(rootFile.read())

then I did:

yaml.load(src)

No more windows line endings ruining my day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is wrong: Highest Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants