from pathlib import Path


def read_python(path):
    """Read a Python file.

    Parameters
    ----------

    path : str or Path

    Returns
    -------

    metadata : dict
        A dictionary containing all variables defined in the Python file (with `exec()`).

    """
    path = Path(path)
    if not path.exists():  # pragma: no cover
        raise IOError("Path %s does not exist.", path)
    contents = path.read_text()
    metadata = {}
    exec(contents, {}, metadata)
    metadata = {k.lower(): v for (k, v) in metadata.items()}
    return metadata