import logging
import pathlib


def _entry_file_is_valid(key, entry):
    """Check the validity of a `file` field of an entry.

    Ensures that
    1. the entry has a `file` field,
    2. the `file` field is nonempty,
    3. the file pointed to exists, and
    4. the file pointed to is a file, not a directory.

    Returns
    -------
    bool:
        True if the file is valid by the above definitions. False otherwise.
    """
    if 'file' not in entry.keys():
        logging.warn(f'No file in entry with key `{key}`. Skipping.')
        return False
    if entry['file'] == '':
        logging.warn(f'File field in entry with key `{key}` is '
                     'empty. Skipping.')
        return False
    if not pathlib.Path(entry['file']).exists():
        logging.warn(f"File `{entry['file']}` in entry with key "
                     f"`{key}` does not exist. Skipping.")
        return False
    if not pathlib.Path(entry['file']).is_file():
        logging.warn(f"File `{entry['file']}` in entry with key "
                     f"`{key}` is not a file. Skipping.")
        return False
    return True