React to import request. Look into user's directory and react to files
    user uploaded there.

    Behavior of this function can be set by setting variables in
    :mod:`ftp.settings`.

    Args:
        username (str): Name of the user who triggered the import request.
        path (str): Path to the file, which triggered import request.
        timestamp (float): Timestamp of the event.
        logger_handler (object): Python logger. See :py:mod:`logging` for
                                 details.

    Returns:
        ::class:`.ImportRequest`.

def process_import_request(username, path, timestamp, logger_handler):
    """
    React to import request. Look into user's directory and react to files
    user uploaded there.

    Behavior of this function can be set by setting variables in
    :mod:`ftp.settings`.

    Args:
        username (str): Name of the user who triggered the import request.
        path (str): Path to the file, which triggered import request.
        timestamp (float): Timestamp of the event.
        logger_handler (object): Python logger. See :py:mod:`logging` for
                                 details.

    Returns:
        ::class:`.ImportRequest`.
    """
    items = []
    error_protocol = []

    # import logger into local namespace
    global logger
    logger = logger_handler

    # read user configuration
    user_conf = passwd_reader.read_user_config(username)

    # lock directory to prevent user to write during processing of the batch
    logger.info("Locking user´s directory.")
    recursive_chmod(path, 0555)

    try:
        # pick up pairs in directories
        for root, dirs, files in os.walk(path):
            for dn in dirs + [path]:
                dn = os.path.join(root, dn)
                dir_list = map(lambda fn: dn + "/" + fn, os.listdir(dn))
                files = _filter_files(dir_list)

                logger.info("Processing directory '%s'." % dn)

                items.extend(
                    _process_directory(
                        files,
                        user_conf,
                        error_protocol,
                    )
                )

        if conf_merger(user_conf, "ISBN_PAIRING"):
            logger.debug("ISBN_PAIRING is ON.")
            logger.info("Pairing user's files by ISBN filename.")
            items = _isbn_pairing(items)

        # parse metadata and remove files from disk
        items = _process_items(items, user_conf, error_protocol)

        # unlink blank directories left by processing files
        logger.info("Removing blank directories.")
        all_dirs = []
        for root, dirs, files in os.walk(path):
            all_dirs.extend(
                map(lambda x: os.path.join(root, x), dirs)
            )

        for dn in sorted(all_dirs, reverse=True):
            content = os.listdir(dn)
            if not content:
                logger.debug("Removing blank directory '%s'." % dn)
                shutil.rmtree(dn)
            else:
                logger.debug("Leaving non-blank directory: '%s'" % dn)
                logger.debug("Content:\n\t%s" % "\n\t".join(content))

    finally:
        # unlock directory
        logger.info("Unlocking user´s directory.")
        recursive_chmod(path, 0775)
        logger.info("Creating lock file '%s'." % settings.LOCK_FILENAME)
        create_lock_file(path + "/" + settings.LOCK_FILENAME)

        # process errors if found
        if error_protocol:
            logger.error(
                "Found %d error(s)." % len(error_protocol)
            )

            err_path = path + "/" + settings.USER_ERROR_LOG
            with open(err_path, "w") as fh:
                fh.write("\n".join(error_protocol))

            logger.error("Error protocol saved to '%s'." % err_path)

        # process import log
        import_log = _create_import_log(items)
        if import_log and conf_merger(user_conf, "CREATE_IMPORT_LOG"):
            logger.debug("CREATE_IMPORT_LOG is on.")

            imp_path = path + "/" + settings.USER_IMPORT_LOG
            with open(imp_path, "w") as f:
                if error_protocol:
                    f.write("Status: Error\n\n")
                    f.write("Error: Import only partially successful.\n")
                    f.write(
                        "See '%s' for details.\n" % settings.USER_ERROR_LOG
                    )
                    f.write("\n--- Errors ---\n")
                    f.write("\n".join(error_protocol))
                    f.write("\n\n--- Successfully imported files ---\n")
                else:
                    f.write("Status: Ok\n\n")
                    f.write("--- Sucess ---\n")
                f.write("\n".join(import_log))

            logger.info("Created import protocol '%s'." % imp_path)

    return ImportRequest(
        username=username,
        requests=items,
        import_log="\n".join(import_log),
        error_log="\n".join(error_protocol)
    )