nginx配置分析器
nginxparser_eb的Python项目详细描述
使用pyparsing的nginx配置解析器。
可以使用load或^{tt2}解析nginx配置文件$ 方法:
>>>fromnginxparser_ebimportload>>>load(open("/etc/nginx/sites-enabled/foo.conf"))[['server'],[['listen','80'],['server_name','foo.com'],['root','/home/ubuntu/sites/foo/']]]]
与其他序列化模块一样,还可以导出配置 使用
>>>fromnginxparser_ebimportload>>>load(open("/etc/nginx/sites-enabled/foo.conf"))[['server'],[['listen','80'],['server_name','foo.com'],['root','/home/ubuntu/sites/foo/']]]]
与其他序列化模块一样,还可以导出配置 使用dump和dumps方法。
>>>fromnginxparser_ebimportdumps>>>dumps([['server'],[['listen','80'],['server_name','foo.com'],['root','/home/ubuntu/sites/foo/']]])'server {listen80;server_namefoo.com;root/home/ubuntu/sites/foo/;}'
安装
nginx解析器现在可以通过pip:
pip install nginxparser_eb
故障排除
可能会发生这样的异常:
ParseException: Expected {Group:({W:(ABCD...) [{Suppress:(<SPC><TAB><CR><LF>) !W:({};)}] Suppress:(";")}) | Forward: ...} (at char 0), (line:1, col:1)
这可能是由于导入修改pyparsing globals的cmd2包所致。特别是,以下代码导致 问题在于:
pyparsing.ParserElement.setDefaultWhitespaceChars(' \t')
在这个设置中,pyparser解析器在新行之后停止解析。
因此,导入pyparsing将修改空白字符设置回
pyparsing.ParserElement.setDefaultWhitespaceChars(" \n\t\r")
学分
基于https://github.com/fatiherikli/nginxparser和certbot nginx解析器。