ansible-tools/siteconf.py
2022-09-11 01:13:33 +02:00

34 lines
960 B
Python
Executable File

import os
import re
#from pprint import pprint
configuration = f"{os.path.dirname(__file__)}/../site.conf"
site_conf = {}
def read():
with open(configuration, "r") as cf:
site = None
for line in cf:
line = line.rstrip();
if r := re.match('#', line):
continue
if r := re.match('\s*$', line):
continue
if r := re.match('\[([A-Za-z0-9_-]+)\]', line):
site = r[1]
site_conf[site] = {}
elif r := re.match('([A-Za-z0-9_-]+)\s+(.+)', line):
if site == None:
print(f"configuration line before 1st block: {line}")
exit(1)
site_conf[site][r[1]] = r[2]
else:
print(f"invalid line in site_conf: {line}")
exit(1)
cf.close()
return site_conf
# vim: set tabstop=4 shiftwidth=4 expandtab smarttab: