import yaml
from typing import Generator

### init logging ###
import logging
LOG_FORMAT = (
    '%(levelname) -5s %(asctime)s %(name)s:%(funcName) -35s %(lineno) -5d:  %(message)s')
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
LOGGER = logging.getLogger(__name__)


class ClusteringConfig:
    '''Contains the configuration for the clustering algorithm defined in configs/clustering.yaml.'''

    config_path = 'configs/clustering.yaml'
    config: dict = None

    def __init__(self):
        self.config = self._load_config()

    def _load_config(self) -> dict:
        '''Loads the whole configuration from file.'''
        config = None

        with open(self.config_path, 'r') as stream:
            try:
                config = yaml.safe_load(stream)
            except yaml.YAMLError as exc:
                LOGGER.error(exc)
                config = {}

        return config

    def get_config(self):
        return self.config

    def get_layer_configs(self) -> Generator[dict, None, None]:
        """
        Returns a generator for the individual layer configs.
        
        Layer configs are dicts including a layer-name.
        """
        for key, layer in self.config['layers'].items():
            layer['layer-name'] = key
            yield layer
