nginxparser: Try parsing nginx config file with Python

nginxparser

Other parsers

-Ruby guy -PHP guy

install

$ pip install -e git+https://github.com/fatiherikli/nginxparser#egg=nginxparser
Obtaining nginxparser from git+https://github.com/fatiherikli/nginxparser#egg=nginxparser
  Cloning https://github.com/fatiherikli/nginxparser to /home/vagrant/.pyenv/versions/wordpress/src/nginxparser
Collecting pyparsing>=1.5.5 (from nginxparser)
  Downloading pyparsing-2.0.3-py2.py3-none-any.whl
Installing collected packages: pyparsing, nginxparser

  Running setup.py develop for nginxparser
    Creating /home/vagrant/.pyenv/versions/wordpress/lib/python2.7/site-packages/Nginxparser.egg-link (link to .)
    Adding Nginxparser 0.2 to easy-install.pth file
    Installed /home/vagrant/.pyenv/versions/wordpress/src/nginxparser
Successfully installed nginxparser pyparsing-2.0.3

Try

>>> from nginxparser import load
>>> conf = load(open('nginx.conf'))
>>> type(conf)
<type 'list'>

It seems that modifire of location is not supported

-I tried pull request -It was pointed out earlier ... (> <)

nginx.py

--Enable / disable nginx.conf with nginx in Deiban package -I tried using a crappy argparse wrapper called pycommand


#!/usr/bin/env python                                                               
from nginxparser import load                                                        
import os                                                                           
from pycommand.command import Command, SubCommand                                   
                                                                                    
                                                                                    
def server_name(file):                                                              
    for i in load(open(file)):                                                      
        for j in (i[0] == ['server']) and i[1] or []:                               
            if j[0] == 'server_name':                                               
                return j[1]                                                         
    return None                                                                     
                                                                                    
                                                                                    
ARGS = [                                                                            
    (('file', ), dict(nargs='?', default='nginx.conf')),                            
]                                                                                   
                                                                                    
                                                                                    
class NginxCommand(Command):                                                        
                                                                                    
    class Enable(SubCommand):                                                       
        args = ARGS                                                                 
                                                                                    
        def run(self, param, **options):                                            
            cname = server_name(param.file)                                         
            print "sudo ln -s {0} /etc/nginx/site-enable/{1}".format(               
                os.path.abspath(param.file), cname)   
            print "mkdir run"                                                       
            print "mkdir logs"                                               
                                                                                    
    class Disable(SubCommand):                                                      
        args = ARGS                                                                 
                                                                                    
        def run(self, param, **options):                                            
            cname = server_name(param.file)                                         
            print "sudo unlink /etc/nginx/site-enable/{0}".format(cname)            
                                                                                    
if __name__ == '__main__':                                                          
    NginxCommand().run()                                    

Recommended Posts

nginxparser: Try parsing nginx config file with Python
Nginx config file
Try scraping with Python.
Try Python output with Haxe 3.2
Draw netCDF file with python
Try running Python with Try Jupyter
Try face recognition with Python
Download csv file with python
Try to decipher the garbled attachment file name with Python
Try scraping with Python + Beautiful Soup
Extract the xz file with python
[Python] Write to csv file with Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
CSS parsing with cssutils in Python
Try to operate Facebook with Python
Try singular value decomposition with Python
Output to csv file with Python
Create an Excel file with Python3
Try face recognition with python + OpenCV
Try frequency control simulation with Python
[Automation with python! ] Part 2: File operation
The idea of feeding the config file with a python file instead of yaml
Try adding a wall to your IFC file with IfcOpenShell python
Try to reproduce color film with Python
Creating a simple PowerPoint file with Python
Try logging in to qiita with Python
Exclusive control with lock file in Python
Try mathematical formulas using Σ with python
Read CSV file with python (Download & parse CSV file)
Try working with binary data in Python
Check the existence of the file with python
Quickly create an excel file with Python #python
Try python
Try using Python with Google Cloud Functions
Let's read the RINEX file with Python ①
Create Python + uWSGI + Nginx environment with Docker
Create Excel file with Python + similarity matrix
Try HTML scraping with a Python library
Record with Python → Save file (sounddevice + wave)
Try calling Python from Ruby with thrift
Try rewriting the file with the less command
Try drawing a map with python + cartopy 0.18.0
[Continued] Try PLC register access with Python
Try assigning or switching with Python: lambda
[For beginners] Try web scraping with Python
I made a configuration file with Python
[Automation] Read mail (msg file) with Python
Split mol2 file with python (-> 2016.04.17 Also supports sdf file)
How to read a CSV file with Python 2/3
Try it with Word Cloud Japanese Python JupyterLab.
Try running Google Chrome with Python and Selenium
Try to solve the man-machine chart with Python
Try to draw a life curve with python
[Python] How to read excel file with pandas
Convert svg file to png / ico with Python
Read table data in PDF file with Python
Hello World with nginx + uwsgi + python on EC2
Try to make a "cryptanalysis" cipher with Python
Develop Windows apps with Python 3 + Tkinter (exe file)
Try to automatically generate Python documents with Sphinx