Best practice to parse multiple config files

元气小坏坏 提交于 2019-12-02 06:17:56

问题


What would be the best practice - if there is any - to parse multiple config files?

I want to parse the mysql server configuration and also write the configuration again.

The configuration allows to issue multiple lines like:

!includedir /etc/mysql.d/

So the interesting thing is, that some configuration may be located in the main file but other may be located in a sub file.

I think pyparsing only works on ONE single file or one content string.

So I probably first need to read all files and maybe restructures the contents like adding headers for the different files...

====main file====
[mysql]
....

!includedir /etc/mysql.d/

====/etc/mysql.d/my.cnf====
[client]
.....

I would only have one pyparsing call. Then I could parse everything into one big data object, group the file sections and have the file names as keys. This way I could also write the data back to the disk...

The other possibility would be to parse the main file and programmatically parse all other files that were found in the main file. Thus I would have several pyparsing calls.

What do you think?


回答1:


In your pyparsing code, attach a parse action to the expression that matches the include statements, have it parse the contents of the referenced files or directory of files, then merge those results into the current parse output. The parse action would make the successive calls to parseString, your code would only make a single call.

See this new example added to the pyparsing examples directory: https://github.com/pyparsing/pyparsing/blob/master/examples/include_preprocessor.py



来源:https://stackoverflow.com/questions/44021524/best-practice-to-parse-multiple-config-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!