from pathlib import Path


def get_toplevel_dirpath(path):
    """
    Provide the top level directory for the given path.

    The top level directory contains the ``controls.json`` file.
    This function returns ``None`` if a top level path can not be found.

    :param path: absolute or relative path to a file or directory.

    :returns: the absolute path to the top level directory.
    """
    if path is None:
        return
    paths = list(Path(path).resolve().parents)
    if Path(path).resolve().is_dir():
        paths = [Path(path).resolve()] + paths
    for path in paths[:-1]:
        if Path(path, 'controls.json').is_file():
            return str(path)