import logging


def _filter_databases(databases, include=None, exclude=None):
    """Filter a list of databases given an inclusion/exclusion list"""
    dbs = []

    if include is not None:
        for db in include:
            if db in databases:
                dbs.append(db)
            else:
                msg = ('Database not found in CellBase: "' + str(db) + '"')
                logging.warning(msg)
        databases = sorted(dbs)

    if exclude is not None:
        for database in databases:
            if database not in exclude:
                dbs.append(database)
        databases = sorted(dbs)

    if not databases:
        msg = 'No databases selected'
        logging.warning(msg)

    return databases