Add configparser

parent 8a8c9153
[DEFAULT]
nginx_dir = /etc/nginx
maps_dir = $(nginx_dir)s/maps
config_dir = $(nginx_dir)s/location_includes
import configparser
import argparse as ap
class Config:
def __init__(self):
self.config = configparser.ConfigParser()
self.filename = 'config.ini'
def read_config(self, filename='config.ini'):
self.filename = filename
self.config.read(self.filename)
def get_default_dir(self, dir_key):
default_dir = self.config['DEFAULT'][dir_key]
return default_dir
def rewrite_default_dir(self, dir_key, new_val):
self.config['DEFAULT'][dir_key] = new_val
f = open(self.filename, 'w')
self.config.write(f)
f.close()
def show_config(self):
f = open(self.filename, 'r')
print(f.read())
f.close()
def main():
parser = ap.ArgumentParser(description='Redirector configure utility')
parser.add_argument('--show', help='Show current config file content.', action='store_true')
parser.add_argument('--edit', action='store', nargs=2, help='Edit one of params. \n Usage: redirector-config --edit NGINX_DIR /etc/nginx2')
args = parser.parse_args()
config = Config()
config.read_config()
if args.show:
config.show_config()
if args.edit:
config.rewrite_default_dir(args.edit[0], args.edit[1])
if __name__ == "__main__":
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment