import yaml


local_fname = 'myconfig.yaml'

def get_config(fname=None, validate_present=None):
    """ Read in a yaml config file from 
    * the file name supplied as an argument, or
    * the local config file, if it exists

    Optionally check that the config file contains any keys given 
    in the validate_present iterable.

    Returns None if the config file was empty and no validation was requested, 
    or if validation was requested and it failed.

    Blows up if the config files it tries to open do not exist.
    """
    cfg = None
    if fname:
        with open(fname, 'r') as f:
            print(f'Using config file {fname}') 
            cfg = yaml.load(f)
    else:
        with open(local_fname, 'r') as f:
            print('Using local config file')
            cfg = yaml.load(f)

    if validate_present:
        if not all(k in cfg for k in validate_present):
            print(f'Config file is missing one of the following required entries: {validate_present}')
            return None
        
    return cfg
