简体   繁体   English

Python:体面的配置文件格式

[英]Python: Decent config file format

I'd like to use a configuration file format which supports key value pairs and nestable, repeatable structures, and which is as light on syntax as possible. 我想使用一种配置文件格式,它支持键值对和可嵌套的,可重复的结构,并且尽可能地使用语法。 I'm imagining something along the lines of: 我想象的是:

cachedir = /var/cache
mail_to = me@example.org

job {
   name = my-media
   frequency = 1 day
   source {
      from = /home/michael/Images

   source { }
   source { }       
}

job { }

I'd be happy with something using significant-whitespace as well. 我对使用重要空白的东西感到满意。

JSON requires too many explicit syntax rules (quoting, commas, etc.). JSON需要太多明确的语法规则(引用,逗号等)。 YAML is actually pretty good, but would require the jobs to be defined as a YAML list, which I find slightly awkward to use. YAML实际上非常好,但是需要将作业定义为YAML列表,我发现使用起来有些尴尬。

I think YAML is great for this purpose, actually: 我认为YAML非常适合这个目的,实际上:

jobs:
 - name: my-media
   ...

 - name: something else
   ...

Or, as a dict instead of list: 或者,作为dict而不是list:

jobs:
  my-media:
    frequency: 1 day
    ...
  something-else:
    frequency: 2 day
    ...

Another thing to consider, which you might not have, is using Python source for the configuration. 您可能没有考虑的另一件事是使用Python源进行配置。 You can nest Python dicts and lists in a very readable manner and it provides multiple unexpected benefits. 您可以以非常易读的方式嵌套Python dicts和列表,并提供多种意想不到的好处。 Django uses Python source for its settings files, for example. 例如,Django将Python源用于其设置文件。

As Python's built-in configparser module does not seem to support nested sections, I'd first try ConfigObj . 由于Python的内置configparser模块似乎不支持嵌套部分,我首先尝试使用ConfigObj (See an introductory tutorial here ). (请参阅此处的介绍性教程)。 According to its homepage, this is the set of features worth mentioning: 根据其主页,这是一组值得一提的功能:

  • Nested sections (subsections), to any level 嵌套部分(子部分),任何级别
  • List values 列出值
  • Multiple line values 多行值
  • String interpolation (substitution) 字符串插值(替换)
  • Integrated with a powerful validation system 与强大的验证系统集成
    • including automatic type checking/conversion 包括自动类型检查/转换
    • repeated sections 重复的部分
    • and allowing default values 并允许默认值
  • When writing out config files, ConfigObj preserves all comments and the order of members and sections 在写出配置文件时,ConfigObj会保留所有注释以及成员和部分的顺序
  • Many useful methods and options for working with configuration files (like the 'reload' method) 使用配置文件的许多有用方法和选项(如'reload'方法)
  • Full Unicode support 完整的Unicode支持

ConfigObj is used by Bazaar, Trac, IPython, matplotlib and many other large Python projects, so it seems pretty mature and stable to me (although I never used it myself). ConfigObj被Bazaar,Trac,IPython,matplotlib和许多其他大型Python项目使用,所以它看起来非常成熟和稳定(虽然我自己从未使用它)。

I think you should check libconfig library http://www.hyperrealm.com/libconfig/ . 我想你应该检查libconfig库http://www.hyperrealm.com/libconfig/ There should be somewhere python bindings for it. 应该有一些python绑定。

Another solution is to use json format which is already provided by python itself. 另一个解决方案是使用已经由python本身提供的json格式。 Search documentation for JSON module. 搜索JSON模块的文档。

Why re-invent the wheel? 为什么重新发明轮子? You can make use of: 你可以利用:

http://docs.python.org/library/configparser.html http://docs.python.org/library/configparser.html

You can also consider Jsonnet if your needs exceed these other options. 如果您的需求超出其他选项,您也可以考虑使用Jsonnet Jsonnet is an extension of JSON that at first glance adds comments, relaxes comma rules, and removes the need for so much quoting. Jsonnet是JSON的扩展,乍一看增加了注释,放宽了逗号规则,并且不再需要这么多引用。 But if you look deeper you see it really provides a full functional programming language and has support for template extension via mixins, file imports, etc. There is a Python binding for it, but its actual implementation is C++. 但是如果你看得更深,你会发现它确实提供了一个完整的函数式编程语言,并且通过mixins,文件导入等支持模板扩展。它有一个Python绑定,但它的实际实现是C ++。

You can use the config system of red-dove. 你可以使用red-dove的配置系统。

http://www.red-dove.com/config-doc/ http://www.red-dove.com/config-doc/

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

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