import os
import logging
import yaml
from pathlib import Path


class ConfigurationCore:

    config_from_file = None

    def __init__(self, config_path=None):
        try:
            if not config_path:
                config_path = os.path.join(Path.home(), '.gitlabform', 'config.yml')
            elif config_path in ['./config.yml', 'config.yml']:
                config_path = os.path.join(os.getcwd(), 'config.yml')

            logging.info("Reading config from: {}".format(config_path))

            with open(config_path, 'r') as ymlfile:
                self.config_from_file = yaml.load(ymlfile)

        except Exception as e:
            raise ConfigFileNotFoundException(config_path)

    def get(self, path):
        """
        :param path: "path" to given element in YAML file, for example for:

        group_settings:
          sddc:
            deploy_keys:
              qa_puppet:
                key: some key...
                title: some title...
                can_push: false

        ..a path to a single element array ['qa_puppet'] will be: "group_settings|sddc|deploy_keys".

        To get the dict under it use: get("group_settings|sddc|deploy_keys")

        :return: element from YAML file (dict, array, string...)
        """
        tokens = path.split('|')
        current = self.config_from_file

        try:
            for token in tokens:
                current = current[token]
        except:
            raise KeyNotFoundException

        return current


class ConfigFileNotFoundException(Exception):
    pass


class KeyNotFoundException(Exception):
    pass

import os
import logging
import yaml
from pathlib import Path


class ConfigurationCore:

    config_from_file = None

    def __init__(self, config_path=None):
        try:
            if not config_path:
                config_path = os.path.join(Path.home(), '.gitlabform', 'config.yml')
            elif config_path in ['./config.yml', 'config.yml']:
                config_path = os.path.join(os.getcwd(), 'config.yml')

            logging.info("Reading config from: {}".format(config_path))

            with open(config_path, 'r') as ymlfile:
                self.config_from_file = yaml.safe_load(ymlfile)

        except Exception as e:
            raise ConfigFileNotFoundException(config_path)

    def get(self, path):
        """
        :param path: "path" to given element in YAML file, for example for:

        group_settings:
          sddc:
            deploy_keys:
              qa_puppet:
                key: some key...
                title: some title...
                can_push: false

        ..a path to a single element array ['qa_puppet'] will be: "group_settings|sddc|deploy_keys".

        To get the dict under it use: get("group_settings|sddc|deploy_keys")

        :return: element from YAML file (dict, array, string...)
        """
        tokens = path.split('|')
        current = self.config_from_file

        try:
            for token in tokens:
                current = current[token]
        except:
            raise KeyNotFoundException

        return current


class ConfigFileNotFoundException(Exception):
    pass


class KeyNotFoundException(Exception):
    pass

Make loading config file safer
Make loading config file safer
