from pathlib import Path
import toml


def _parse_config(path: Path, env: dict) -> dict:
    """
    Parse config file, with optional environment variables

    Any values matching $VALUE pattern will be substituted by env[VALUE]

    """
    path = Path(path)
    if not path.exists():
        raise IOError("could not find config file: {}".format(path))

    s = path.open().read()
    s = s.format(**env)

    config = toml.loads(s)

    return config