数据结构格式(YAML 或其他)的往返解析保留注释,用于编写配置

发布于 2024-11-28 06:29:29 字数 405 浏览 2 评论 0原文

我一直在多个应用程序中使用 YAML 作为配置文件格式,一切都很顺利,除了一件事:当我的程序需要在 YAML 配置文件中写入/修改配置变量时,它会通过加载和转储整个文件来破坏格式和注释/结构。

(嗯,实际上 YAML 还有另一个问题。大多数用户,其中许多人不是程序员,都会被 YAML 规则的细节所困扰,比如某些地方空格的重要性。但这并不是一个主要的抱怨。

)我更喜欢的是一个 YAML 加载器/转储器,它可以进行往返解析(保留所有空格和注释),或者具有此类解析器的其他一些人类可读的序列化格式。我什至正在考虑使用 Perl 文档和 PPI,因为 PPI 是一个往返安全解析器。或者也许 PPI 可以处理 YAML 或类似的格式?我宁愿不使用 XML,在此之前我会求助于 INI+(JSON|YAML|... 用于键值)。

有什么建议或指示吗?

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.

(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.)

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. I'm even considering using Perl document and PPI, since PPI is a round-trip safe parser. Or perhaps PPI can be bent to deal with YAML or similar formats? I'd rather not use XML, I'd resort to INI+(JSON|YAML|... for key values) before that.

Any advice or pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦罢 2024-12-05 06:29:29

如果您使用块结构 YAML 并且 Python 是可以接受的,那么您
可以使用 Python 包 1 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)

结果:

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

请注意,行尾注释仍然对齐。

code 包含以下内容,而不是普通的 listdict 对象
附有评论的包装版本²。

1 使用 pip install ruamel.yaml 安装。适用于 Python 2.6/2.7/3.3+。
免责声明:我是该包的作者。

² ordereddict 用于映射情况,以保留顺序

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:

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.

¹ Install with pip install ruamel.yaml. Works on 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

只有影子陪我不离不弃 2024-12-05 06:29:29

是的,您和所有认为哇,yaml 听起来很酷的人,简单地说,它不存在,但是

更新:你可能想使用 Config::General,它的 apache 配置格式(xmlish)

不,PPI 不是通用工具,如果你想要 BNF,你想使用 Marpa

在所有 INI/JSON/YAML/XML 中,XML 可能为非程序员提供最好的编辑器支持(听起来很疯狂)

Yeah, you and everyone who thought wow, yaml sounds cool, simply put, it doesn't exist, yet

update: you probably want to use Config::General, its apache config format (xmlish)

No, PPI is not general purpose tool, if you want BNF-ness, you want to use Marpa

Of all INI/JSON/YAML/XML, XML probably has the best editor support for non-programmers (sounds crazy)

以往的大感动 2024-12-05 06:29:29

一种方法是使用“镜头”。请参阅 Augeas 了解一种实现。

One approach to this is using "lenses". See Augeas for one implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文