ruamel.yaml dump: how to stop map scalar values from being moved to a new indented line?

27 Views Asked by At

Hard to describe succinctly, so I'll demonstrate.

from sys import stdout
from ruamel.yaml import YAML
yml = YAML()
doc = """
kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8
"""
y = yml.load(doc)
yml.dump(y, stdout)

Which prints:

kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: 
      quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: 
      quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: 
      quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: 
      quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: 
      quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: 
      quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8

Why do the name: xxx dicts in the mirror.additionalImages list get split over two lines when dumping? Is it based on the length of the key's value or something? How can I stop this? (On a file with hundreds of those, it really gets ridiculous.) I've played with various settings of the YAML() instances, to no avail. (E.g., indent() and compact() and default_flow_style.)

1

There are 1 best solutions below

2
Anthon On BEST ANSWER

The default width for output in a YAML instance is 80 characters, that is what causing the values (which are too long), to wrap to the next line.

If you set yaml.width to some larger value, this won't happen:

from sys import stdout
from ruamel.yaml import YAML

yaml = YAML()
yaml.width = 1024     # added line
doc = """
kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8
"""
y = yaml.load(doc)
yaml.dump(y, stdout)

which gives:

kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8