from typing import Dict
from pathlib import Path
import yaml
import toml


def cfg_load(cfg) -> Dict:
    """
    There are 2 types of config file used in the project yaml an toml. Will move to al tml at some point

    This function wraps around both

    Returns
    -------
    Dictionary config
    """
    cfg = Path(cfg)

    if not cfg.is_file():
        raise FileNotFoundError(f'Cannot find required config file: {cfg}')

    if Path(cfg).suffix == '.yaml':

        try:
            with open(cfg, 'r') as fh:
                return yaml.load(fh, Loader=yaml.FullLoader)
        except Exception as e:
            raise ValueError("can't read the config file - {}".format(e))

    elif Path(cfg).suffix == '.toml':
        try:
            return toml.load(cfg)
        except Exception as e:
            raise ValueError("can't read the config file - {}".format(e))

    else:
        raise ValueError('Config file should end in .toml or .yaml')