无法转储为"纯粹" YAML

时间:2018-03-28 15:46:36

标签: yaml ruamel.yaml

ruamel.yaml == 0.15.37
Python 3.6.2 :: Continuum Analytics,Inc。

当前代码:

from ruamel.yaml import YAML
import sys


yaml = YAML()
kube_context = yaml.load('''
    apiVersion: v1
    clusters: []
    contexts: []
    current-context: ''
    kind: Config
    preferences: {}
    users: []
''')
kube_context['users'].append({'name': '{username}/{cluster}'.format(username='test', cluster='test'), 'user': {'token': 'test'}})
kube_context['clusters'].append({'name': 'test', 'cluster': {'server': 'URL:443'}})
kube_context['contexts'].append({'name': 'test', 'context': {'user': 'test', 'cluster': 'test'}})

yaml.dump(kube_context, sys.stdout)

我的yaml.dump()正在生成包含list和dict对象的输出,而不是完全展开。

当前输出:

apiVersion: v1
clusters: [{name: test, cluster: {server: URL:443}}]
contexts: [{name: test, context: {user: test, cluster: test}}]
current-context: ''
kind: Config
preferences: {}
users: [{name: test/test, user: {token: test}}]

为了让yaml.dump()输出完全展开,我需要做些什么?

预期产出:

apiVersion: v1
clusters: 
  - name: test
    cluster: 
      server: URL:443
contexts: 
  - name: test
    context:
      user: test
      cluster: test
current-context: ''
kind: Config
preferences: {}
users: 
  - name: test/test
    user: 
      token: test

2 个答案:

答案 0 :(得分:1)

输出是“纯粹的”YAML。您希望节点以块样式(基于缩进)呈现,而不是当前的流样式([] {} - 基于)。以下是如何做到这一点:

typ

(注意Athon对以下safe的评论;您需要将其设置为unsafepublic class Job { @Column private Long jobId; @OneToMany(fetch = FetchType.EAGER) @JoinColumn(name="creator", referencedColumnName="uid") private User creator; } public class User { @Column private Long uid; @Column private String name; } public Predicate toPredicate(Root<Job> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> Predicates=new ArrayList<Predicate>(); Predicates.add(cb.equal(root.get("creator").get("uid"), 123456)); query.where(Predicates.toArray(new Predicate[Predicates.size()])); return null; } ,以便RoundTripLoader不设置空序列的样式)

答案 1 :(得分:1)

ruamel.yaml,使用默认YAML()YAML(typ='rt')时,将保留flow - 或block样式的序列和映射。无法创建块样式空序列或空映射,因此[]{}在加载时会被标记为流样式。

流样式只能包含流样式(而块样式可以包含块样式或流样式)(YAML 1.2 spec 8.2.3):

  

YAML允许将流节点嵌入到块集合中(但反之亦然)。

因此,您在(流式)列表/序列中插入的dict / mapping数据也将表示为flow-style。

如果您希望所有内容都是块样式(您称之为“扩展”模式),则可以通过调用.set_block_style()属性上的.fa方法(仅在集合,因此try / except):

from ruamel.yaml import YAML
import sys


yaml = YAML()

kube_context = yaml.load('''
    apiVersion: v1
    clusters: []
    contexts: []
    current-context: ''
    kind: Config
    preferences: {}
    users: []
''')
kube_context['users'].append({'name': '{username}/{cluster}'.format(username='test', cluster='test'), 'user': {'token': 'test'}})
kube_context['clusters'].append({'name': 'test', 'cluster': {'server': 'URL:443'}})
kube_context['contexts'].append({'name': 'test', 'context': {'user': 'test', 'cluster': 'test'}})

for k in kube_context:
    try:
        kube_context[k].fa.set_block_style()
    except AttributeError:
        pass

yaml.dump(kube_context, sys.stdout)

这给出了:

apiVersion: v1
clusters:
- name: test
  cluster:
    server: URL:443
contexts:
- name: test
  context:
    user: test
    cluster: test
current-context: ''
kind: Config
preferences: {}
users:
- name: test/test
  user:
    token: test

请注意,没有必要将yaml.default_flow_style = False设置为默认的往返模式;虽然已经为key preferences的值设置了block-style,但它表示为flow style,因为没有其他方法可以表示空映射。