Ruamel yaml坏转储

时间:2018-04-05 09:47:36

标签: ruamel.yaml

我有一个Python脚本,试图从“main.yml”文件生成README。

我可以转储YML流的每一行,但是在每行中,ruamel.yaml似乎添加了一行“...”字符串。

我的Python:

#!/usr/bin/python

import os, sys
import argparse
import ruamel.yaml

ROLES_PATH="../"
DESC_SCRIPT = 'This program will try to generate MD documentation from variables files of an Ansible role'

parser = argparse.ArgumentParser(description = DESC_SCRIPT)
parser.add_argument("-r", "--role", help="specify the name of existing role")
if len(sys.argv)<2:
   parser.print_help(sys.stderr)
   sys.exit(1)

args = parser.parse_args()

if args.role:
   README=(ROLES_PATH+args.role+"/README.md")
   if os.path.isfile(README):
      print(sys.argv[0]+": README.md already exists for %s" % args.role)
      print(sys.argv[0]+": Exiting... ")
      sys.exit(2)
   if os.path.isdir(ROLES_PATH+args.role):
      print(sys.argv[0]+": Role found")
      print(sys.argv[0]+": Selected role is %s" % args.role)

      readme_file = open(README, 'w')
      readme_file.write("Role Name\n=========\n%s\n\nRequirements\n------------\n- Debian\n\nRole variables (default)\n--------------\n\n" % args.role)

      yml = ruamel.yaml
      yml.YAML.explicit_start = True
      yml.YAML.default_flow_style = None
      yml.YAML.encoding = "utf-8"
      yml.YAML.allow_unicode = True
      yml.YAML.errors = "strict"

      readme_file = open(README, 'a+')
      with open(ROLES_PATH+args.role+"/defaults/main.yml", "r") as stream:
         code = yml.load(stream, Loader=yml.RoundTripLoader)
         print(code)
         for line in code:
            print(type(line))
            print("Line : %s" % line)
            yml.dump(line, readme_file, Dumper=yml.RoundTripDumper)

我的main.yml:

---
apache_datadir: '/var/www'
apache_security_configuration: '/etc/apache2/conf-available/security.conf'
apache_default_vhost: '/etc/apache2/sites-available/default.conf'
apache_mpm: event
apache_mod_list:
  - headers
  - ssl
  - rewrite

我的README生成:

Role Name
=========
ansible-apache

Requirements
------------
- Debian

Role variables (default)
--------------

apache_datadir
...
apache_security_configuration
...
apache_default_vhost
...
apache_mpm
... 
apache_mod_list
...

我不明白为什么在每一行产生“......”。 我试图做一些类似“if line is'...'”的内容,但它不起作用。

1 个答案:

答案 0 :(得分:1)

每当您转储单个普通,折叠或文字标量而没有缩进时(即当它们是唯一要转储的节点时),ruamel.yaml会添加这些文档结束标记。这是来自PyYAML的继承行为。

在内部,这是因为在open_ended实例上设置了属性Emitter,虽然您可以重写方法而不是这样做,但是将字符串写入{{更容易“ 1}}而不是将字符串转储为YAML:

readme_file

如果您事先知道所有if isinstance(line, str): readme_file.write(line + '\n') else: yml.dump(line, readme_file, Dumper=yml.RoundTripDumper) 值都是字符串,那么您根本不需要使用line

您似乎也将旧样式加载/转储与新的dump API结合使用。这样:

ruamel.yaml

yml = ruamel.yaml yml.YAML.explicit_start = True yml.YAML.default_flow_style = None yml.YAML.encoding = "utf-8" yml.YAML.allow_unicode = True yml.YAML.errors = "strict" 没有影响。你可能想做的是:

yml.dump

然后转储 yml = ruamel.yaml.YAML() yml.explicit_start = True yml.default_flow_style = None yml.encoding = "utf-8" # default when using YAML() or YAML(typ="rt") yml.allow_unicode = True # always default in the new API yml.errors = "strict" (即没有yml.dump(data, file_pointer)