我有一个 Python 脚本,它试图从“main.yml”文件中生成一个 README。
我可以转储 YML 流的每一行,但在每一行,ruamel.yaml 似乎都添加了一行“...”字符串。
我的蟒蛇:
#!/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
我的自述文件生成:
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 '...'" 的事情,但它不起作用。