X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Futils%2Fconfig.py;h=cd16a14482bc45008e297b219462d47cf0e5e30c;hb=d62b8c3db5a87accb40a49d0347255bfe467911b;hp=e9da8f8f4c245f72b38a27f58f4e97df654c0002;hpb=f4bfa3663d95a4c03f1e626ae8a3b9aab59bb48c;p=mpd-sima.git diff --git a/sima/utils/config.py b/sima/utils/config.py index e9da8f8..cd16a14 100644 --- a/sima/utils/config.py +++ b/sima/utils/config.py @@ -25,11 +25,12 @@ Parse configuration file and set defaults for missing options. # IMPORTS import configparser +import logging import sys from configparser import Error -from os import (makedirs, environ, stat, chmod) -from os.path import (join, isdir, isfile) +from os import (access, makedirs, environ, stat, chmod, W_OK, R_OK) +from os.path import (join, isdir, isfile, dirname, exists) from stat import (S_IMODE, ST_MODE, S_IRWXO, S_IRWXG) from . import utils @@ -105,8 +106,8 @@ class ConfMan(object): # CONFIG MANAGER CLASS * command line options (overrides previous) """ - def __init__(self, logger, options=None): - self.log = logger + def __init__(self, options=None): + self.log = logging.getLogger('sima') # options settings priority: # defauts < env. var. < conf. file < command line self.conf_file = options.get('conf_file') @@ -119,22 +120,30 @@ class ConfMan(object): # CONFIG MANAGER CLASS ## INIT CALLS self.init_config() self.supersedes_config_with_cmd_line_options() + # Controls files access + self.control_facc() # generate dbfile self.config['sima']['db_file'] = join(self.config['sima']['var_dir'], 'sima.db') - def get_pw(self): - try: - self.config.getboolean('MPD', 'password') - self.log.debug('No password set, proceeding without ' + - 'authentication...') - return None - except ValueError: - # ValueError if password not a boolean, hence an actual password. - pwd = self.config.get('MPD', 'password') - if not pwd: - self.log.debug('Password set as an empty string.') - return None - return pwd + def control_facc(self): + """TODO: redundant with startopt cli args controls + """ + ok = True + for ftochk in [self.config['log']['logfile'], + self.config['daemon']['pidfile'],]: + if not exists(ftochk): + # Is parent directory writable then + filedir = dirname(ftochk) + if not access(filedir, W_OK): + self.log.critical('no write access to "{0}"'.format(filedir)) + ok = False + else: + if not access(ftochk, W_OK): + self.log.critical('no write access to "{0}"'.format(ftochk)) + ok = False + if not ok: + sys.exit(2) + def control_mod(self): """