Beispiel #1
0
def _set_handlers(cursor_store, checkpoint_name):
    handlers = ExtractionHandlers()
    handlers.TOTAL_EVENTS = 0

    def handle_error(exception):
        if isinstance(exception, OSError):  # let click handle it
            raise

        errors.ERRORED = True
        if hasattr(exception, "response") and hasattr(exception.response,
                                                      "text"):
            message = f"{exception}: {exception.response.text}"
        else:
            message = exception
        logger.log_error(message)

        message = str(message)
        if not message.lower().startswith("error:"):
            message = f"Error: {message}"

        secho(message, err=True, fg="red")

    handlers.handle_error = handle_error
    if cursor_store:
        handlers.record_cursor_position = lambda value: cursor_store.replace(
            checkpoint_name, value)
        handlers.get_cursor_position = lambda: cursor_store.get(checkpoint_name
                                                                )
    return handlers
Beispiel #2
0
def create_handlers(sdk, extractor_class, cursor_store, checkpoint_name,
                    formatter, force_pager):
    extractor = extractor_class(sdk, ExtractionHandlers())
    handlers = _set_handlers(cursor_store, checkpoint_name)

    @warn_interrupt(warning=INTERRUPT_WARNING)
    def handle_response(response):
        events = _get_events(sdk, handlers, extractor._key, response)
        total_events = len(events)
        handlers.TOTAL_EVENTS += total_events
        formatter.echo_formatted_list(events, force_pager=force_pager)

        # To make sure the extractor records correct timestamp event when `CTRL-C` is pressed.
        if total_events:
            _record_timestamp(extractor, handlers, events[-1])

    handlers.handle_response = handle_response
    return handlers
def _set_handlers(cursor_store, checkpoint_name):
    handlers = ExtractionHandlers()
    handlers.TOTAL_EVENTS = 0

    def handle_error(exception):
        if isinstance(exception, OSError):  # let click handle it
            raise

        errors.ERRORED = True
        if hasattr(exception, "response") and hasattr(exception.response,
                                                      "text"):
            message = "{}: {}".format(exception, exception.response.text)
        else:
            message = exception
        logger.log_error(message)
        secho(str(message), err=True, fg="red")

    handlers.handle_error = handle_error
    if cursor_store:
        handlers.record_cursor_position = lambda value: cursor_store.replace(
            checkpoint_name, value)
        handlers.get_cursor_position = lambda: cursor_store.get(checkpoint_name
                                                                )
    return handlers