/usr/lib/python3/dist-packages/skytools/config.py is in python3-skytools 3.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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | """Nicer config class."""
from __future__ import division, absolute_import, print_function
import os
import os.path
import re
import socket
import skytools
try:
from configparser import ( # noqa
NoOptionError, NoSectionError, InterpolationError, InterpolationDepthError,
Error as ConfigError, ConfigParser, MAX_INTERPOLATION_DEPTH,
ExtendedInterpolation, Interpolation)
except ImportError:
from ConfigParser import ( # noqa
NoOptionError, NoSectionError, InterpolationError, InterpolationDepthError,
Error as ConfigError, SafeConfigParser, MAX_INTERPOLATION_DEPTH)
class Interpolation(object):
"""Define Interpolation API from Python3."""
def before_get(self, parser, section, option, value, defaults):
return value
def before_set(self, parser, section, option, value):
return value
def before_read(self, parser, section, option, value):
return value
def before_write(self, parser, section, option, value):
return value
class ConfigParser(SafeConfigParser):
"""Default Python's ConfigParser that uses _DEFAULT_INTERPOLATION"""
_DEFAULT_INTERPOLATION = None
def _interpolate(self, section, option, rawval, defs):
if self._DEFAULT_INTERPOLATION is None:
return SafeConfigParser._interpolate(self, section, option, rawval, defs)
return self._DEFAULT_INTERPOLATION.before_get(self, section, option, rawval, defs)
__all__ = [
'Config',
'NoOptionError', 'ConfigError',
'ConfigParser', 'ExtendedConfigParser', 'ExtendedCompatConfigParser'
]
class Config(object):
"""Bit improved ConfigParser.
Additional features:
- Remembers section.
- Accepts defaults in get() functions.
- List value support.
"""
def __init__(self, main_section, filename, sane_config=None,
user_defs=None, override=None, ignore_defs=False):
"""Initialize Config and read from file.
"""
# use config file name as default job_name
if filename:
job_name = os.path.splitext(os.path.basename(filename))[0]
else:
job_name = main_section
# initialize defaults, make them usable in config file
if ignore_defs:
self.defs = {}
else:
self.defs = {
'job_name': job_name,
'service_name': main_section,
'host_name': socket.gethostname(),
}
if filename:
self.defs['config_dir'] = os.path.dirname(filename)
self.defs['config_file'] = filename
if user_defs:
self.defs.update(user_defs)
self.main_section = main_section
self.filename = filename
self.override = override or {}
self.cf = ConfigParser()
if filename is None:
self.cf.add_section(main_section)
elif not os.path.isfile(filename):
raise ConfigError('Config file not found: '+filename)
self.reload()
def reload(self):
"""Re-reads config file."""
if self.filename:
self.cf.read(self.filename)
if not self.cf.has_section(self.main_section):
raise NoSectionError(self.main_section)
# apply default if key not set
for k, v in self.defs.items():
if not self.cf.has_option(self.main_section, k):
self.cf.set(self.main_section, k, v)
# apply overrides
if self.override:
for k, v in self.override.items():
self.cf.set(self.main_section, k, v)
def get(self, key, default=None):
"""Reads string value, if not set then default."""
if not self.cf.has_option(self.main_section, key):
if default is None:
raise NoOptionError(key, self.main_section)
return default
return str(self.cf.get(self.main_section, key))
def getint(self, key, default=None):
"""Reads int value, if not set then default."""
if not self.cf.has_option(self.main_section, key):
if default is None:
raise NoOptionError(key, self.main_section)
return default
return self.cf.getint(self.main_section, key)
def getboolean(self, key, default=None):
"""Reads boolean value, if not set then default."""
if not self.cf.has_option(self.main_section, key):
if default is None:
raise NoOptionError(key, self.main_section)
return default
return self.cf.getboolean(self.main_section, key)
def getfloat(self, key, default=None):
"""Reads float value, if not set then default."""
if not self.cf.has_option(self.main_section, key):
if default is None:
raise NoOptionError(key, self.main_section)
return default
return self.cf.getfloat(self.main_section, key)
def getlist(self, key, default=None):
"""Reads comma-separated list from key."""
if not self.cf.has_option(self.main_section, key):
if default is None:
raise NoOptionError(key, self.main_section)
return default
s = self.get(key).strip()
res = []
if not s:
return res
for v in s.split(","):
res.append(v.strip())
return res
def getdict(self, key, default=None):
"""Reads key-value dict from parameter.
Key and value are separated with ':'. If missing,
key itself is taken as value.
"""
if not self.cf.has_option(self.main_section, key):
if default is None:
raise NoOptionError(key, self.main_section)
return default
s = self.get(key).strip()
res = {}
if not s:
return res
for kv in s.split(","):
tmp = kv.split(':', 1)
if len(tmp) > 1:
k = tmp[0].strip()
v = tmp[1].strip()
else:
k = kv.strip()
v = k
res[k] = v
return res
def getfile(self, key, default=None):
"""Reads filename from config.
In addition to reading string value, expands ~ to user directory.
"""
fn = self.get(key, default)
if fn == "" or fn == "-":
return fn
# simulate that the cwd is script location
#path = os.path.dirname(sys.argv[0])
# seems bad idea, cwd should be cwd
fn = os.path.expanduser(fn)
return fn
def getbytes(self, key, default=None):
"""Reads a size value in human format, if not set then default.
Examples: 1, 2 B, 3K, 4 MB
"""
if not self.cf.has_option(self.main_section, key):
if default is None:
raise NoOptionError(key, self.main_section)
s = default
else:
s = self.cf.get(self.main_section, key)
return skytools.hsize_to_bytes(s)
def get_wildcard(self, key, values=(), default=None):
"""Reads a wildcard property from conf and returns its string value, if not set then default."""
orig_key = key
keys = [key]
for wild in values:
key = key.replace('*', wild, 1)
keys.append(key)
keys.reverse()
for k in keys:
if self.cf.has_option(self.main_section, k):
return self.cf.get(self.main_section, k)
if default is None:
raise NoOptionError(orig_key, self.main_section)
return default
def sections(self):
"""Returns list of sections in config file, excluding DEFAULT."""
return self.cf.sections()
def has_section(self, section):
"""Checks if section is present in config file, excluding DEFAULT."""
return self.cf.has_section(section)
def clone(self, main_section):
"""Return new Config() instance with new main section on same config file."""
return Config(main_section, self.filename)
def options(self):
"""Return list of options in main section."""
return self.cf.options(self.main_section)
def has_option(self, opt):
"""Checks if option exists in main section."""
return self.cf.has_option(self.main_section, opt)
def items(self):
"""Returns list of (name, value) for each option in main section."""
return self.cf.items(self.main_section)
# define some aliases (short-cuts / backward compatibility cruft)
getbool = getboolean
class ExtendedInterpolationCompat(Interpolation):
_EXT_VAR_RX = r'\$\$|\$\{[^(){}]+\}'
_OLD_VAR_RX = r'%%|%\([^(){}]+\)s'
_var_rc = re.compile('(%s|%s)' % (_EXT_VAR_RX, _OLD_VAR_RX))
_bad_rc = re.compile('[%$]')
def before_get(self, parser, section, option, rawval, defaults):
dst = []
self._interpolate_ext(dst, parser, section, option, rawval, defaults, set())
return ''.join(dst)
def before_set(self, parser, section, option, value):
sub = self._var_rc.sub('', value)
if self._bad_rc.search(sub):
raise ValueError("invalid interpolation syntax in %r" % value)
return value
def _interpolate_ext(self, dst, parser, section, option, rawval, defaults, loop_detect):
if not rawval:
return rawval
if len(loop_detect) > MAX_INTERPOLATION_DEPTH:
raise InterpolationDepthError(option, section, rawval)
xloop = (section, option)
if xloop in loop_detect:
raise InterpolationError(section, option, 'Loop detected: %r in %r' % (xloop, loop_detect))
loop_detect.add(xloop)
parts = self._var_rc.split(rawval)
for i, frag in enumerate(parts):
fullkey = None
use_vars = defaults
if i % 2 == 0:
dst.append(frag)
continue
if frag in ('$$', '%%'):
dst.append(frag[0])
continue
if frag.startswith('${') and frag.endswith('}'):
fullkey = frag[2:-1]
# use section access only for new-style keys
if ':' in fullkey:
ksect, key = fullkey.split(':', 1)
use_vars = None
else:
ksect, key = section, fullkey
elif frag.startswith('%(') and frag.endswith(')s'):
fullkey = frag[2:-2]
ksect, key = section, fullkey
else:
raise InterpolationError(section, option, 'Internal parse error: %r' % frag)
key = parser.optionxform(key)
newpart = parser.get(ksect, key, raw=True, vars=use_vars)
if newpart is None:
raise InterpolationError(ksect, key, 'Key referenced is None')
self._interpolate_ext(dst, parser, ksect, key, newpart, defaults, loop_detect)
loop_detect.remove(xloop)
try:
ExtendedInterpolation
except NameError:
class ExtendedInterpolationPy2(ExtendedInterpolationCompat):
_var_rc = re.compile('(%s)' % ExtendedInterpolationCompat._EXT_VAR_RX)
_bad_rc = re.compile('[$]')
ExtendedInterpolation = ExtendedInterpolationPy2
class ExtendedConfigParser(ConfigParser):
"""ConfigParser that uses Python3-style extended interpolation by default.
Syntax: ${var} and ${section:var}
"""
_DEFAULT_INTERPOLATION = ExtendedInterpolation()
class ExtendedCompatConfigParser(ExtendedConfigParser):
r"""Support both extended "${}" syntax from python3 and old "%()s" too.
New ${} syntax allows ${key} to refer key in same section,
and ${sect:key} to refer key in other sections.
"""
_DEFAULT_INTERPOLATION = ExtendedInterpolationCompat()
|