mirror of
https://github.com/zokradonh/kopano-docker
synced 2025-06-26 09:16:38 +00:00
New: KCCONF is now capable of adding new conf lines
This commit is contained in:
parent
b5d30b13d7
commit
cd4d3caf79
138
core/kcconf.py
138
core/kcconf.py
@ -1,61 +1,77 @@
|
|||||||
"""This module provides functions for easy editing of kopano config files \
|
"""This module provides functions for easy editing of kopano config files
|
||||||
via environment variables"""
|
via environment variables"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
def configkopano(configs):
|
def configkopano(configs):
|
||||||
for filename, config in configs.items():
|
""" Changes configuration files according to configs typically returned from parseenvironmentvariables(..)"""
|
||||||
if not os.path.exists(filename):
|
for filename, config in configs.items():
|
||||||
return
|
if not os.path.exists(filename):
|
||||||
with open(filename) as f:
|
return
|
||||||
contents = f.read()
|
# read configuration file
|
||||||
f.close()
|
with open(filename) as f:
|
||||||
|
contents = f.read()
|
||||||
for key, newvalue in config.items():
|
f.close()
|
||||||
if key == "kccomment":
|
|
||||||
for line in newvalue:
|
for key, newvalue in config.items():
|
||||||
contents = re.sub(r"^\s*" + re.escape(line), r"#{}".format(line), contents, 0, re.MULTILINE)
|
if key == "kccomment":
|
||||||
elif key == "kcuncomment":
|
# comment lines
|
||||||
for line in newvalue:
|
for line in newvalue:
|
||||||
contents = re.sub(r"^\s*#\s*" + re.escape(line) , line, contents, 0, re.MULTILINE)
|
contents = re.sub(r"^\s*" + re.escape(line), r"#{}".format(line), contents, 0, re.MULTILINE)
|
||||||
else:
|
elif key == "kcuncomment":
|
||||||
contents = re.sub(r"^\s*#?\s*{}\s*=.*".format(key), r"{} = {}".format(key, newvalue), contents, 0, re.MULTILINE)
|
# uncomment lines
|
||||||
|
for line in newvalue:
|
||||||
with open(filename, "w") as f:
|
contents = re.sub(r"^\s*#\s*" + re.escape(line) , line, contents, 0, re.MULTILINE)
|
||||||
f.write(contents)
|
else:
|
||||||
f.close()
|
# find config line
|
||||||
|
if re.search(r"^\s*#?\s*{}\s*=.*".format(key), contents, re.MULTILINE) == None:
|
||||||
def parseenvironmentvariables(prependingpath):
|
# add configuration as new line
|
||||||
configs = dict()
|
contents += "\n{} = {}".format(key, newvalue)
|
||||||
|
else:
|
||||||
for name, value in os.environ.items():
|
# change existing line
|
||||||
namematch = re.match(r"^KCCONF_([A-Z]+)_([A-Z0-9_]+)$", name)
|
contents = re.sub(r"^\s*#?\s*{}\s*=.*".format(key), r"{} = {}".format(key, newvalue), contents, 0, re.MULTILINE)
|
||||||
if namematch != None:
|
|
||||||
filename = namematch.group(1).lower() + ".cfg"
|
# save new configuration
|
||||||
if prependingpath + filename not in configs:
|
with open(filename, "w") as f:
|
||||||
configs[prependingpath + filename] = dict()
|
f.write(contents)
|
||||||
confkey = namematch.group(2).lower()
|
f.close()
|
||||||
configs[prependingpath + filename][confkey] = value
|
|
||||||
commentmatch = re.match(r"^KCCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name)
|
def parseenvironmentvariables(prependingpath):
|
||||||
if commentmatch != None:
|
""" Parse all environment variables starting with KCCONF_, KCCOMMENT_ and KCUNCOMMENT_ and
|
||||||
filename = commentmatch.group(1).lower() + ".cfg"
|
return as multi dimensional dict """
|
||||||
if prependingpath + filename not in configs:
|
configs = dict()
|
||||||
configs[prependingpath + filename] = dict()
|
|
||||||
try:
|
for name, value in os.environ.items():
|
||||||
configs[prependingpath + filename]["kccomment"].append(value)
|
# parse change/add configuration commands
|
||||||
except KeyError:
|
namematch = re.match(r"^KCCONF_([A-Z]+)_([A-Z0-9_]+)$", name)
|
||||||
configs[prependingpath + filename]["kccomment"] = []
|
if namematch != None:
|
||||||
configs[prependingpath + filename]["kccomment"].append(value)
|
filename = namematch.group(1).lower() + ".cfg"
|
||||||
uncommentmatch = re.match(r"^KCUNCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name)
|
if prependingpath + filename not in configs:
|
||||||
if uncommentmatch != None:
|
configs[prependingpath + filename] = dict()
|
||||||
filename = uncommentmatch.group(1).lower() + ".cfg"
|
confkey = namematch.group(2).lower()
|
||||||
if prependingpath + filename not in configs:
|
configs[prependingpath + filename][confkey] = value
|
||||||
configs[prependingpath + filename] = dict()
|
# parse comment configuration commands
|
||||||
try:
|
commentmatch = re.match(r"^KCCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name)
|
||||||
configs[prependingpath + filename]["kcuncomment"].append(value)
|
if commentmatch != None:
|
||||||
except KeyError:
|
filename = commentmatch.group(1).lower() + ".cfg"
|
||||||
configs[prependingpath + filename]["kcuncomment"] = []
|
if prependingpath + filename not in configs:
|
||||||
configs[prependingpath + filename]["kcuncomment"].append(value)
|
configs[prependingpath + filename] = dict()
|
||||||
return configs
|
try:
|
||||||
|
configs[prependingpath + filename]["kccomment"].append(value)
|
||||||
|
except KeyError:
|
||||||
|
configs[prependingpath + filename]["kccomment"] = []
|
||||||
|
configs[prependingpath + filename]["kccomment"].append(value)
|
||||||
|
# parse uncomment configuration commands
|
||||||
|
uncommentmatch = re.match(r"^KCUNCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name)
|
||||||
|
if uncommentmatch != None:
|
||||||
|
filename = uncommentmatch.group(1).lower() + ".cfg"
|
||||||
|
if prependingpath + filename not in configs:
|
||||||
|
configs[prependingpath + filename] = dict()
|
||||||
|
try:
|
||||||
|
configs[prependingpath + filename]["kcuncomment"].append(value)
|
||||||
|
except KeyError:
|
||||||
|
configs[prependingpath + filename]["kcuncomment"] = []
|
||||||
|
configs[prependingpath + filename]["kcuncomment"].append(value)
|
||||||
|
return configs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user