ruamel.yaml dump: 如何防止映射标量值移动到新缩进行?

1 投票
1 回答
24 浏览
提问于 2025-04-12 18:40

很难简洁地描述,所以我来演示一下。

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)

这段代码的输出是:

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

为什么在打印的时候,mirror.additionalImages 列表里的 name: xxx 字典会分成两行呢?这是因为键的值长度吗?我怎么才能让它不这样?(在一个有几百个这样的字典的文件里,这样真的很麻烦。)我尝试过调整 YAML() 实例的各种设置,但都没有效果。(比如 indent()compact()default_flow_style。)

1 个回答

1

在YAML文件中,默认的输出宽度是80个字符。这就是为什么一些值(如果太长的话)会换行到下一行的原因。

如果你把yaml.width设置成一个更大的值,这种情况就不会发生了:

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)

这样就会得到:

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

撰写回答