Python 配置解析器(重复键支持)
所以我最近开始为我正在开发的一个 Python 项目编写一个配置解析器。我最初避免使用 configparser 和 configobj,因为我想支持这样的配置文件:
key=value
key2=anothervalue
food=burger
food=hotdog
food=cake icecream
简而言之,这个配置文件将经常通过 SSH 的命令行进行编辑。因此,我不想对空格进行制表符或挑剔(如 YAML),但我也希望避免在 vi 中换行具有多个值(很容易是 10 个或更多)的键。这就是为什么我想支持重复键。
在我的理想世界中,当我向 Python 配置对象请求食物时,它会给我一个包含 ['burger', 'hotdog', 'cake', 'icecream'] 的列表。如果没有定义食物值,它会在默认配置文件中查找并给我那个/那些值。
我已经实现了上述内容
但是,当我意识到我想支持保留内联注释等时,我的麻烦就开始了。我处理读取和写入配置文件的方式是将文件解码为内存中的字典,从字典中读取值,或将值写入字典,然后将该字典转储回文件中。这对于保留行顺序和评论等来说并不是很好,而且它让我烦恼不已。
A) ConfigObj 看起来除了支持重复键之外它拥有我需要的一切。相反,它希望我制作一个列表,由于换行,在 vi 中通过 ssh 手动编辑会很痛苦。我可以让 configobj 对 ssh/vi 更加友好吗?
B)我的自制解决方案是否错误?有没有更好的方法来读取/写入/存储我的配置值?是否有任何简单的方法可以通过修改该行并从内存中重写整个配置文件来处理更改配置文件中的键值?
So I recently started writing a config parser for a Python project I'm working on. I initially avoided configparser and configobj, because I wanted to support a config file like so:
key=value
key2=anothervalue
food=burger
food=hotdog
food=cake icecream
In short, this config file is going to be edited via the command line over SSH often. So I don't want to tab or finicky about spacing (like YAML), but I also want avoid keys with multiple values (easily 10 or more) being line wrapped in vi. This is why I would like to support duplicate keys.
An my ideal world, when I ask the Python config object for food, it would give me a list back with ['burger', 'hotdog', 'cake', 'icecream']. If there wasn't a food value defined, it would look in a defaults config file and give me that/those values.
I have already implemented the above
However, my troubles started when I realized I wanted to support preserving inline comments and such. The way I handle reading and writing to the config files, is decoding the file into a dict in memory, read the values from the dict, or write values to the dict, and then dump that dict back out into a file. This isn't really nice for preserving line order and commenting and such and it's bugging the crap out of me.
A) ConfigObj looks like it has everything I need except support duplicate keys. Instead it wants me to make a list is going to be a pain to edit manually in vi over ssh due to line wrapping. Can I make configobj more ssh/vi friendly?
B) Is my homebrew solution wrong? Is there a better way of reading/writing/storing my config values? Is there any easy way to handle changing a key value in a config file by just modifying that line and rewriting the entire config file from memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果可以的话,我当然会尝试利用标准库中的内容。
配置解析器类的签名如下所示:
class ConfigParser.SafeConfigParser([defaults[, dict_type[, allowed_no_value]]])
请注意
dict_type
参数。如果提供,它将用于构造节列表、节内选项以及默认值的字典对象。它默认为collections.OrderedDict
。也许您可以在其中传递一些内容来获得所需的多键行为,然后获得 ConfigParser 的所有优势。您可能必须编写自己的类才能执行此操作,或者您可能会在 PyPi 或 ActiveState 配方中找到为您编写的类。尝试寻找包包或多组课程。我要么走那条路,要么就忍气吞声,列个清单:
Well I would certainly try to leverage what is in the standard library if I could.
The signature for the config parser classes look like this:
class ConfigParser.SafeConfigParser([defaults[, dict_type[, allow_no_value]]])
Notice the
dict_type
argument. When provided, this will be used to construct the dictionary objects for the list of sections, for the options within a section, and for the default values. It defaults tocollections.OrderedDict
. Perhaps you could pass something in there to get your desired multiple-key behavior, and then reap all the advantages ofConfigParser
. You might have to write your own class to do this, or you could possibly find one written for you on PyPi or in the ActiveState recipes. Try looking for a bag or multiset class.I'd either go that route or just suck it up and make a list:
疯狂的想法:将你的字典值设置为包含行号、列号和值本身的三元组列表,并添加特殊键进行注释。
Crazy idea: make your dictionary values as a list of 3-tuples with line number, col number and value itself and add special key for comment.