1
0
mirror of https://github.com/zokradonh/kopano-docker synced 2025-06-26 17:26:19 +00:00

New: KCCONF is now capable of adding new conf lines

This commit is contained in:
Andre Zoledziowski 2018-06-29 11:55:18 +02:00
parent b5d30b13d7
commit cd4d3caf79
No known key found for this signature in database
GPG Key ID: 2A72044119624966

View File

@ -1,4 +1,4 @@
"""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
@ -6,31 +6,45 @@ import os
import os.path import os.path
def configkopano(configs): def configkopano(configs):
""" Changes configuration files according to configs typically returned from parseenvironmentvariables(..)"""
for filename, config in configs.items(): for filename, config in configs.items():
if not os.path.exists(filename): if not os.path.exists(filename):
return return
# read configuration file
with open(filename) as f: with open(filename) as f:
contents = f.read() contents = f.read()
f.close() f.close()
for key, newvalue in config.items(): for key, newvalue in config.items():
if key == "kccomment": if key == "kccomment":
# comment lines
for line in newvalue: for line in newvalue:
contents = re.sub(r"^\s*" + re.escape(line), r"#{}".format(line), contents, 0, re.MULTILINE) contents = re.sub(r"^\s*" + re.escape(line), r"#{}".format(line), contents, 0, re.MULTILINE)
elif key == "kcuncomment": elif key == "kcuncomment":
# uncomment 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*#\s*" + re.escape(line) , line, contents, 0, re.MULTILINE)
else: else:
contents = re.sub(r"^\s*#?\s*{}\s*=.*".format(key), r"{} = {}".format(key, newvalue), contents, 0, re.MULTILINE) # find config line
if re.search(r"^\s*#?\s*{}\s*=.*".format(key), contents, re.MULTILINE) == None:
# add configuration as new line
contents += "\n{} = {}".format(key, newvalue)
else:
# change existing line
contents = re.sub(r"^\s*#?\s*{}\s*=.*".format(key), r"{} = {}".format(key, newvalue), contents, 0, re.MULTILINE)
# save new configuration
with open(filename, "w") as f: with open(filename, "w") as f:
f.write(contents) f.write(contents)
f.close() f.close()
def parseenvironmentvariables(prependingpath): def parseenvironmentvariables(prependingpath):
""" Parse all environment variables starting with KCCONF_, KCCOMMENT_ and KCUNCOMMENT_ and
return as multi dimensional dict """
configs = dict() configs = dict()
for name, value in os.environ.items(): for name, value in os.environ.items():
# parse change/add configuration commands
namematch = re.match(r"^KCCONF_([A-Z]+)_([A-Z0-9_]+)$", name) namematch = re.match(r"^KCCONF_([A-Z]+)_([A-Z0-9_]+)$", name)
if namematch != None: if namematch != None:
filename = namematch.group(1).lower() + ".cfg" filename = namematch.group(1).lower() + ".cfg"
@ -38,6 +52,7 @@ def parseenvironmentvariables(prependingpath):
configs[prependingpath + filename] = dict() configs[prependingpath + filename] = dict()
confkey = namematch.group(2).lower() confkey = namematch.group(2).lower()
configs[prependingpath + filename][confkey] = value configs[prependingpath + filename][confkey] = value
# parse comment configuration commands
commentmatch = re.match(r"^KCCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name) commentmatch = re.match(r"^KCCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name)
if commentmatch != None: if commentmatch != None:
filename = commentmatch.group(1).lower() + ".cfg" filename = commentmatch.group(1).lower() + ".cfg"
@ -48,6 +63,7 @@ def parseenvironmentvariables(prependingpath):
except KeyError: except KeyError:
configs[prependingpath + filename]["kccomment"] = [] configs[prependingpath + filename]["kccomment"] = []
configs[prependingpath + filename]["kccomment"].append(value) configs[prependingpath + filename]["kccomment"].append(value)
# parse uncomment configuration commands
uncommentmatch = re.match(r"^KCUNCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name) uncommentmatch = re.match(r"^KCUNCOMMENT_([A-Z]+)_([A-Z0-9_]+)$", name)
if uncommentmatch != None: if uncommentmatch != None:
filename = uncommentmatch.group(1).lower() + ".cfg" filename = uncommentmatch.group(1).lower() + ".cfg"