https://github.com/pyhedgehog/nginxparser
https://github.com/pyhedgehog/nginxparser
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/pyhedgehog/nginxparser
- Owner: pyhedgehog
- License: mit
- Created: 2022-12-08T20:04:38.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T20:04:49.000Z (about 3 years ago)
- Last Synced: 2025-01-14T08:38:27.737Z (11 months ago)
- Language: Python
- Homepage:
- Size: 43.9 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Nginx Configuration Parser
==========================
An nginx configuration parser that uses Pyparsing.
You can parse a nginx configuration file with `load` or `loads` method:
``` {.python}
>>> from nginxparser.nginxparser import load
>>> load(open("/etc/nginx/sites-enabled/foo.conf"))
[['server'], [
['listen', '80'],
['server_name', 'foo.com'],
['root', '/home/ubuntu/sites/foo/']]]]
```
Same as other serialization modules also you can export configuration
with
``` {.python}
>>> from nginxparser.nginxparser import load
>>> load(open("/etc/nginx/sites-enabled/foo.conf"))
[['server'], [
['listen', '80'],
['server_name', 'foo.com'],
['root', '/home/ubuntu/sites/foo/']]]]
```
Same as other serialization modules also you can export configuration
with `dump` and `dumps` methods.
``` {.python}
>>> from nginxparser.nginxparser import dumps
>>> dumps([['server'], [
['listen', '80'],
['server_name', 'foo.com'],
['root', '/home/ubuntu/sites/foo/']]])
'server {
listen 80;
server_name foo.com;
root /home/ubuntu/sites/foo/;
}'
```
Installation
------------
The Nginx parser is now available via pip:
git clone https://github.com/pyhedgehog/nginxparser.git;cd nginxparser;pip install .
Troubleshooting
---------------
Exception like this may occur:
ParseException: Expected {Group:({W:(ABCD...) [{Suppress:() !W:({};)}] Suppress:(";")}) | Forward: ...} (at char 0), (line:1, col:1)
It may be caused by importing Cmd2 package which modifies pyparsing
globals. In particular, the following code causes the trouble:
``` {.python}
pyparsing.ParserElement.setDefaultWhitespaceChars(' \t')
```
In this setting the pyparser parser stops parsing after a new line.
From this reason, importing pyparsing modifies set white space chars
back to
``` {.python}
pyparsing.ParserElement.setDefaultWhitespaceChars(" \n\t\r")
```
Credits
-------
Based on the and CertBot
Nginx parser.