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
|
import logging
_log = logging.getLogger(__name__)
import os
from functools import reduce
from glob import glob
try:
from ConfigParser import SafeConfigParser as ConfigParser
except ImportError:
from configparser import ConfigParser
# used by unit tests to redirect config directories
_testprefix = None
def getgendir(user=False):
if _testprefix:
return _testprefix+'/procServ.d'
elif user:
return os.path.expanduser('~/.config/procServ.d')
else:
return '/etc/procServ.d'
def getrundir(user=False):
if _testprefix:
return _testprefix+'/run'
elif user:
return os.environ['XDG_RUNTIME_DIR']
else:
return '/run'
def getconffiles(user=False):
"""Return a list of config file names
Only those which actualy exist
"""
if _testprefix:
prefix = _testprefix
elif user:
prefix = os.path.expanduser('~/.config')
else:
prefix = '/etc'
files = map(os.path.expanduser, [
prefix+'/procServ.conf',
prefix+'/procServ.d/*.conf',
])
_log.debug('Config files %s', files)
# glob('') -> ['',]
# map(glob) produces a list of lists
# reduce by concatination into a single list
return reduce(list.__add__, map(glob, files), [])
_defaults = {
'user':'nobody',
'group':'nogroup',
'chdir':'/',
'port':'0',
'instance':'1',
}
def getconf(user=False):
"""Return a ConfigParser with one section per procServ instance
"""
from glob import glob
C = ConfigParser(_defaults)
C.read(getconffiles(user=user))
return C
|