from pathlib import Path
import yaml


def is_playbook(file: Path) -> bool:
    """
    Check if file is a playbook
    :param file: Path to file
    :return: True or False
    """
    # Used from https://github.com/ansible-community/ansible-lint/blob/main/src/ansiblelint/utils.py
    playbook_keys = {
        "gather_facts",
        "hosts",
        "import_playbook",
        "post_tasks",
        "pre_tasks",
        "roles",
        "tasks",
    }

    with file.open() as f:
        try:
            loaded_yaml = yaml.safe_load(f)
            if isinstance(loaded_yaml, list):
                if playbook_keys.intersection(loaded_yaml[0].keys()):
                    return True
            if isinstance(loaded_yaml, dict):
                if playbook_keys.intersection(loaded_yaml.keys()):
                    return True
        except yaml.YAMLError:
            return False

    return False