]> kaliko git repositories - mpd-sima.git/blobdiff - sima/utils/config.py
Controls file access for config provided path
[mpd-sima.git] / sima / utils / config.py
index 157d7c3c341ce7cea04a47aa54e3ba468b30c608..cd16a14482bc45008e297b219462d47cf0e5e30c 100644 (file)
@@ -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
@@ -60,6 +61,9 @@ DEFAULT_CONF = {
             'verbosity': "info",
             'logfile': "",
             },
+        'crop': {
+            'consume': 10,
+            },
         'echonest': {
             'queue_mode': "track", #TODO control values
             'max_art': 15,
@@ -75,6 +79,7 @@ DEFAULT_CONF = {
             'track_to_add': 1,
             'album_to_add': 1,
             'depth': 1,
+            'cache': True,
             },
         'randomfallback': {
             'flavour': "sensible", # in pure, sensible
@@ -101,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')
@@ -115,20 +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 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 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_mod(self):
         """
@@ -204,7 +219,6 @@ class ConfMan(object):  # CONFIG MANAGER CLASS
 
         ## Sima sqlite DB
         self.config['sima']['var_dir'] = join(data_dir)
-        self.config['sima']['db_file'] = join(data_dir, 'sima.db')
 
         # If no conf file present, uses defaults
         if not isfile(self.conf_file):