# vim: set ft=python fileencoding=utf-8 tw=72 fdm=indent nowrap:
"""
Handles a YAML file by setting a default value when variable is not set.

    :copyright: © 2012-2020, J. A. Corbal
    :license: MIT
"""
import sys
import yaml


class Yamler:
    """Handle a YAML file."""

    def __init__(self, filename, logger=None):
        """Constructor.

        :param filename: YAML filename where to look for the config.
        :type filename: str
        :raise: IOError
        """
        self.filename = filename
        self.logger = logger
        self.fd = None
        try:
            with open(self.filename, 'r') as self.fd:
                self.config = yaml.safe_load(self.fd)
        except IOError:
            self.logger and self.logger.error(
                "Cannot read the configuration file")
            sys.exit(21)

    def __del__(self):
        """Destructor.

        Close all opened files, just to make sure, although those will
        be automatically closed.
        """
        if (self.fd):
            self.fd.close()

    def __getitem__(self, key):
        """Get the value from a specific key in the configuration file.

        :param key: Key to look for
        :type key: str
        :return: The value associated to that key
        :raise: KeyError
        """
        try:
            value = self.config[key]
        except KeyError:
            self.logger and self.logger.error(
                "Key not found in configuration file")
            sys.exit(22)
        else:
            return value

    def retrieve(self, key, default_value=None):
        """Get a value from a key or sets a default value.

        :param key: Key to look for
        :type key: str
        :param default_value: Default value if key is not found
        :return: The value associated to that key, or the default value

        .. todo:: Use :func:`yamler.Yamler.__getitem__`?
        """
        if key in self.config:
            value = self.config[key]
        else:
            value = default_value
        return value

