简体   繁体   English

用于编写配置的数据结构格式(YAML或诸如此类)保留注释的往返解析

[英]Round-trip parsing of data structure format (YAML or whatnot) preserving comments, for writing configuration

I have been using YAML as configuration file format in several applications, and all went well except one thing: when my program needs to write/modify a config variable in a YAML config file, it destroys formatting and comments by loading and dumping the entire file/structure. 我在几个应用程序中一直使用YAML作为配置文件格式,除了一件事情之外一切顺利:当我的程序需要在YAML配置文件中编写/修改配置变量时,它会通过加载和转储整个文件来破坏格式和注释/结构体。

(Well, there is another problem with YAML actually. Most users, many of them are not programmers, will be tripped over the details of YAML rules, like the significance of whitespace in some places. But this is not a major gripe.) (嗯,实际上YAML存在另一个问题。大多数用户,其中很多都不是程序员,会被YAML规则的细节所绊倒,比如某些地方的空白的重要性。但这不是一个主要的抱怨。)

What I would prefer is a YAML loader/dumper which can do round-trip parsing (preserving all whitespaces & comments), or some other human-readable serialization format which has such parser. 我更喜欢的是一个YAML加载器/转储器,它可以进行往返解析(保留所有空格和注释),或者其他具有这种解析器的人类可读序列化格式。 I'm even considering using Perl document and PPI, since PPI is a round-trip safe parser. 我甚至考虑使用Perl文档和PPI,因为PPI是一个往返安全解析器。 Or perhaps PPI can be bent to deal with YAML or similar formats? 或者PPI可能会倾向于处理YAML或类似的格式? I'd rather not use XML, I'd resort to INI+(JSON|YAML|... for key values) before that. 我宁愿不使用XML,在此之前我会使用INI +(JSON | YAML | ...作为键值)。

Any advice or pointers? 有什么建议或指示?

If you are using block structured YAML and Python is acceptable, you can use the Python package¹ ruamel.yaml which is a derivative of PyYAML and supports round trip preservation of comments : 如果你使用块结构化的YAML并且Python是可以接受的,你可以使用Python包¹ruamel.yaml这是PyYAML的衍生物并支持往返保存注释

import sys
import ruamel.yaml

inp = """\
# example
name:
  # details
  family: Smith   # very common
  given: Alice    # one of the siblings
"""

yaml = ruamel.yaml.YAML()

code = yaml.load(inp)
code['name']['given'] = 'Bob'

yaml.dump(code, sys.stdout)

with result: 结果:

# example
name:
  # details
  family: Smith   # very common
  given: Bob      # one of the siblings

Note that the end-of-line comments are still aligned. 请注意,行尾注释仍然是对齐的。

Instead of normal list and dict objects the code consists of wrapped versions² on which the comments attached. 相反,正常的listdict对象的code由versions²包装上附着的意见。

¹ Install with pip install ruamel.yaml . ¹ 使用pip install ruamel.yaml Works on Python 2.6/2.7/3.3+. 适用于Python 2.6 / 2.7 / 3.3 +。 Disclaimer: I am the author of that package. 免责声明:我是该套餐的作者。
² ordereddict is used in case of a mapping, to preserve ordering ordereddict用于映射,以保持排序

Yeah, you and everyone who thought wow, yaml sounds cool , simply put, it doesn't exist , yet 是的,你和所有想到哇的人,yaml听起来很酷 ,简单地说, 它还不存在

update: you probably want to use Config::General, its apache config format (xmlish) 更新:你可能想使用Config :: General,它的apache配置格式(xmlish)

No, PPI is not general purpose tool, if you want BNF-ness, you want to use Marpa 不,PPI不是通用工具,如果你想要BNF-ness,你想使用Marpa

Of all INI/JSON/YAML/XML, XML probably has the best editor support for non-programmers (sounds crazy) 在所有INI / JSON / YAML / XML中,XML可能对非程序员提供最好的编辑器支持(听起来很疯狂)

One approach to this is using "lenses". 一种方法是使用“镜头”。 See Augeas for one implementation. 请参阅Augeas了解一个实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM