from typing import Union
from pathlib import Path
import yaml


def load_cfg(cfg_file: Union[str, Path]) -> dict:
    """Load the PCC algs config file in YAML format with custom tag 
    !join.

    Parameters
    ----------
    cfg_file : `Union[str, Path]`
        The YAML config file.

    Returns
    -------
    `dict`
        A dictionary object loaded from the YAML config file.
    """
    # [ref.] https://stackoverflow.com/a/23212524
    ## define custom tag handler
    def join(loader, node):
        seq = loader.construct_sequence(node)
        return ''.join([str(i) for i in seq])

    ## register the tag handler
    yaml.add_constructor('!join', join)
    
    with open(cfg_file, 'r') as f:
        cfg = yaml.load(f, Loader=yaml.FullLoader)
    
    return cfg