/usr/share/pyshared/moksha/common/config.py is in python-moksha.common 1.2.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import os
try:
import configparser
except ImportError:
import ConfigParser as configparser
class EnvironmentConfigParser(configparser.ConfigParser):
"""ConfigParser which is able to substitute environment variables.
"""
def get(self, section, option, raw=0, vars=None):
try:
val = configparser.ConfigParser.get(
self, section, option, raw=raw, vars=vars)
except Exception:
val = configparser.ConfigParser.get(
self, section, option, raw=1, vars=vars)
return self._interpolate(section, option, val, vars)
def _interpolate(self, section, option, rawval, vars):
"""Adds the additional key-value pairs to the interpolation.
Also allows to use default values in %(KEY:-DEFAULT)s"""
if not vars:
vars = {}
for k, v in os.environ.items():
if not k in vars.keys():
vars[k] = v
value = rawval
depth = configparser.MAX_INTERPOLATION_DEPTH
while depth: # Loop through this until it's done
depth -= 1
start = value.find("%(")
if start > -1:
end = value.find(")s", start)
if end <= start:
raise ValueError(
"configparser: no \")s\" found "
"after \"%(\" : " + rawval)
rawkey = value[start + 2:end]
default = None
key = rawkey
sep = rawkey.find(':-')
if sep > -1:
default = rawkey[sep + 2:]
key = key[:sep]
if key in vars:
value = value.replace("%(" + rawkey + ")s", vars[key], 1)
else:
if default:
value = value.replace("%(" + rawkey + ")s", default, 1)
else:
raise ValueError(
"configparser: Key %s not found in: %s" % (
rawval, key))
else:
break
if value.find("%(") != -1:
raise ValueError(
"configparser: Interpolation Depth error: %s" % rawval)
return value
|