import os
import logging

from configparser import ConfigParser


class Env(object):
    """
        NIAV Environment
    """

    def __init__(self, caller=None):
        """
            Parse env.ini file(s):
                - from constructor
                - from NIAV_ENV environment variable

            :raises: IOError if none of the paths are valid 
        """
        self.log = logging.getLogger("niav")
        self.config = ConfigParser()
        config_files = []

        if caller is not None:
            try:
                auto_conf = os.path.join(os.path.split(caller)[0], "env.ini")
                if os.path.exists(auto_conf):
                    config_files.append(auto_conf)

                auto_conf = os.path.join(os.path.split(caller)[0], "local.ini")
                if os.path.exists(auto_conf):
                    config_files.append(auto_conf)
            except:
                pass

        conf = os.environ.get('NIAV_ENV')

        if conf:
            config_files = [filename.strip() for filename in conf.split(",")]

        if not config_files:
            raise TypeError("NIAV .ini not found")

        for conf_file in config_files:
            if not os.path.exists(conf_file):
                raise IOError("file missing: %s, path does not exists" % conf_file)
            else:
                self.config.read(conf_file)
                self.log.info("using environment: %s" % conf_file)

        log_level = self.get_unsafe("log.level") if self.get_unsafe("log.level") is not None else logging.INFO
        self.log.setLevel(log_level)

    def get(self, mixed_key):
        """
            Get key value from NIAV_ENV config file

            :param mixed_key: Section and key to read as "section.key"
            :type mixed_key: string
            :return: Value of "key" in "section" from ini file
            :rtype: string
            :raises: Exception if "Section" or "Key" does not exist
        """
        section, key = mixed_key.split(".")
        if section not in self.config.sections():
            raise KeyError("section '%s' does not exists" % section)
        elif key not in self.config.options(section):
            raise KeyError("key '%s' does not exists" % key)
        return self.config.get(section, key)

    def get_unsafe(self, mixed_key):
        """
            Get key value from NIAV_ENV env.ini file.
            This method doesn't raise exception.

            :param mixed_key: Section and key to read as "section.key"
            :type mixed_key: string
            :return: Value of "key" in "section" from ini file. None if key or value doesn't exist.
            :rtype: string
        """
        section, key = mixed_key.split(".")
        if section not in self.config.sections():
            return None
        elif key not in self.config.options(section):
            return None
        return self.config.get(section, key)

    def get_int(self, mixed_key):
        """
            Get key value from NIAV_ENV env.ini file

            :param mixed_key: Section and key to read as "section.key"
            :type mixed_key: string
            :return: Value of "key" in "section" from ini file
            :rtype: int
            :raises: Exception if "Section" or "Key" does not exist
        """
        section, key = mixed_key.split(".")
        if section not in self.config.sections():
            raise KeyError("section '%s' does not exists" % section)
        elif key not in self.config.options(section):
            raise KeyError("key '%s' does not exists" % key)
        return self.config.getint(section, key)

    def get_int_unsafe(self, mixed_key):
        """
            Get key value from NIAV_ENV env.ini file.
            This method doesn't raise exception.

            :param mixed_key: section and key to read as "section.key"
            :type mixed_key: string
            :return: value of "key" in "section" from ini file. None if key or value doesn't exist.
            :rtype: int
        """
        section, key = mixed_key.split(".")
        if section not in self.config.sections():
            return None
        elif key not in self.config.options(section):
            return None
        return self.config.getint(section, key)

@@ -2767,32 +2767,94 @@
 fig.sections():%0A
+            self.log.info(%22section '%25s' not found%22 %25 section)%0A
             retu
@@ -2907,32 +2907,113 @@
 tions(section):%0A
+            self.log.info(%22key '%25s' not found in section '%25s'%22 %25 (key, section))%0A
             retu
@@ -4253,32 +4253,94 @@
 fig.sections():%0A
+            self.log.info(%22section '%25s' not found%22 %25 section)%0A
             retu
@@ -4393,32 +4393,113 @@
 tions(section):%0A
+            self.log.info(%22key '%25s' not found in section '%25s'%22 %25 (key, section))%0A
             retu

add some logs