SolarKennedy
SolarKennedy

Reputation: 148

How can I make python ruaml preserve long strings as is?

I'm using ruamel.yaml to do round trip load/dump, but on some files with hand-crafted strings, I would like to preserve them. Sometimes this is nice because the formatting is actually pretty (readability), or copy/paste ability.

Here is my code:

from ruamel.yaml import YAML
import sys
yaml = YAML()

i = """
mydict:
  command: "my_shell_script
    --firstarg
    --second
    --third
    .......
    fourth
  "
"""

data = yaml.load(i)
yaml.dump(data, sys.stdout)

Which outputs

mydict:
  command: 'my_shell_script --firstarg --second --third ....... fourth '

I don't want it to change those lines. I'm familiar with the yaml.width option, but I don't want to set that at all! (high or low).

How can I make ruamel.yaml preserve the formatting on scalars like this? Does it have anything to do with PreservedScalarString? Can I do it such a way that it preserves only certain scalars?

Upvotes: 1

Views: 344

Answers (1)

Anthon
Anthon

Reputation: 76578

You can hook into the double quoted scalar string loading/construction, you would probably have to set yaml.preserve_quotes, and then have that behave like the loader for folded scalars.

But it is much easier to use YAML's folded scalars in the first place, as that looks quite like your input, round-trips out of the box. And only differs in the loaded value in that there is no trailing space (which I hope is not significant):

import sys
import ruamel.yaml

yaml_str = """
mydict:
  command: "my_shell_script
    --firstarg
    --second
    --third
    .......
    fourth
    "
altdict:
  command: >-
    my_shell_script
    --firstarg
    --second
    --third
    .......
    fourth
"""

yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)
for k in data:
    print(repr(data[k]['command']))
yaml.dump(data, sys.stdout)

which gives:

'my_shell_script --firstarg --second --third ....... fourth '
'my_shell_script --firstarg --second --third ....... fourth'
mydict:
  command: 'my_shell_script --firstarg --second --third ....... fourth '
altdict:
  command: >-
    my_shell_script
    --firstarg
    --second
    --third
    .......
    fourth

As you can see the folded scalar dumps as the input.

If you leave the - from >-, you'll get a (single) newline at the end of your loaded data.

Upvotes: 1

Related Questions