Jerod Santo changelog.com/posts

nginxparser: A Python module that loads and dumps Nginx configs

Even tiny, single-use open source tools are worthy of our attention. If you have the need to programmatically configure your Nginx servers, look no further than nginxparser by Fatih Erikli.

Weighing in at less than 100 lines of code, nginxparser provides two features.

loading:

from nginxparser import load
load(open("/etc/nginx/sites-enabled/foo.conf"))

    [['server'], [
        ['listen', '80'],
        ['server_name', 'foo.com'],
        ['root', '/home/ubuntu/sites/foo/']]]]

and dumping:

from 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/;
    }'    

I was impressed with how simple it is to define a parser using Pyparsing, which nginxparser does to great effect.

It’s definitely worth checking out!


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00