ruamel.yaml.cmd rt breaks lists, if containing long string, or hash

178 Views Asked by At

I just notices that the command line tool, called like this: "ruamel.yaml.cmd rt --save $YAML_FILE", will break lists that either contain long strings, or hashes:

Example list containing a hash:

Source:

telegraf::inputs:
  cpu:
    - percpu: true
      totalcpu: true
      report_active: true

output:

telegraf::inputs:
  cpu:
     - percpu: true
    totalcpu: true
    report_active: true

example list containing long string:

source:

rsyslog::config::snippets:
  00_forward:
    ensure: 'present'
    lines:
      - 'if $syslogfacility != 1 then {'
      - 'action(Name="collector-syslog" Type="omfwd" Target="%{hiera("rsyslog_server")}" Port="514" Action.ResumeInterval="5" Protocol="tcp")'
      - '}'

output:

rsyslog::config::snippets:
  00_forward:
    ensure: present
    lines:
       - if $syslogfacility != 1 then {
       - action(Name="collector-syslog" Type="omfwd" Target="%{hiera("rsyslog_server")}"
      Port="514" Action.ResumeInterval="5" Protocol="tcp")
       - '}'

I already created a bug report for this, but it was deleted with a comment pointing to https://yaml.readthedocs.io/en/latest/example.html?highlight=indent#output-of-dump-as-a-string.

But I am not sure how this code snipped should help me with the command line tool.

Or is the tool deprecated, and I have to roll my own?

1

There are 1 best solutions below

7
On

The automatic detection of the indent seems incorrect for your input, as that input is inconsistent (your mappings are indented 2 positions and your sequences 4 positions with an offset for the block sequence indicator of 2). ruamel.yaml.cmd as on PyPI doesn't support different indentation levels for sequences and mappings (ruamel.yaml didn't when that was written, it does now).

Apart from that you cannot set the line width for the output in ruamel.yaml.cmd for older versions ( before 2020-12-01), and those versions are using the default 80 characters for the wrapping.

I recommend you upgrade to 0.5.6 and use the command line options:

yaml rt  --indent 2 --width 1024 --save <yourfile>

The appropriate repository for ruamel.yaml.cmd is https://sourceforge.net/p/ruamel-yaml-cmd/code/ci/default/tree/ . A bug report on ruamel.yaml which can only be used from a Python program, should include the minimal source code of the program that reproduces the error, and if not provided, issues will be removed as announced on its create issue page.