Beispiel #1
0
    def setup_parser(self):
        """
        Setup a parser object in the main runtime
        """
        # Reset the status for new writes
        self.reset_parser()
        self.reset_regex_status()

        # Store previous message pointer
        if self.messages is self.stderr_messages:
            self.previous_messages = self.stderr_messages
        elif self.messages is self.stdout_messages:
            self.previous_messages = self.stdout_messages

        # Stick to top to show options
        self.manually_controlled_line = False
        self.stick_to_bottom = True
        self.stick_to_top = False

        # Overwrite the messages pointer
        self.messages = Parser().show_patterns()
        self.previous_render = None
        self.render_text_in_output()
        while True:
            time.sleep(self.poll_rate)
            self.activate_prompt()
            command = self.box.gather().strip()
            if command == 'q':
                self.reset_parser()
                return
            else:
                try:
                    parser = Parser()
                    parser.load(Parser().patterns()[int(command)])
                    break
                except JSONDecodeError as err:
                    self.messages.append(
                        f'Invalid JSON: {err.msg} on line {err.lineno}, char {err.colno}'
                    )
                except ValueError:
                    pass

        # Overwrite a different list this time, and reset it when done
        self.messages = parser.display_example()
        self.previous_render = None
        self.render_text_in_output()
        while True:
            time.sleep(self.poll_rate)
            self.activate_prompt()
            command = self.box.gather().strip()
            if command == 'q':
                self.reset_parser()
                return
            else:
                try:
                    command = int(command)
                    assert command < len(self.messages)
                    self.parser_index = int(command)
                    self.current_status = f'Parsing with {parser.get_name()}, field {parser.get_analytics_for_index(command)}'
                    self.write_to_command_line(self.current_status)
                    break
                except ValueError:
                    pass
                except AssertionError:
                    pass

        # Set parser
        self.parser = parser

        # Put pointer back to new array
        self.messages = self.parsed_messages

        # Render immediately
        self.previous_render = None

        # Stick to bottom again
        self.last_index_processed = 0
        self.stick_to_bottom = True
        self.stick_to_top = False
Beispiel #2
0
def setup_parser(logria: 'Logria'):  # type: ignore
    """
    Setup a parser object in the main runtime
    """
    # Reset the status for new writes
    reset_parser(logria)
    reset_regex_status(logria)

    # Store previous message pointer
    if logria.messages is logria.stderr_messages:
        logria.previous_messages = logria.stderr_messages
    elif logria.messages is logria.stdout_messages:
        logria.previous_messages = logria.stdout_messages

    # Stick to top to show options
    logria.manually_controlled_line = False
    logria.stick_to_bottom = True
    logria.stick_to_top = False

    # Only set when there are no pattenrs to display so we can update the user
    custom_message = ''

    # Create parser object
    parser = Parser()

    # Overwrite the messages pointer
    logria.messages = parser.show_patterns()
    logria.redraw()
    while True:
        if not parser.show_patterns():
            parser = None  # type: ignore
            custom_message = 'No parsers found! Enter :config to build one. Press z to cancel.'
            break
        time.sleep(logria.poll_rate)
        logria.activate_prompt()
        command = logria.box.gather().strip()
        if command == ':q':
            logria.messages = logria.previous_messages
            parser = None  # type: ignore
            break
        if ':r ' in command[:3]:
            items_to_remove = resolve_delete_command(command)
            sessions = parser.patterns()
            for item in items_to_remove:
                if item in sessions:
                    parser.remove(sessions[item])
            logria.messages = parser.show_patterns()
            logria.redraw()
            continue
        try:
            parser.load(parser.patterns()[int(command)])
            break
        except KeyError:
            continue
        except JSONDecodeError as err:
            logria.messages.append(
                f'Invalid JSON: {err.msg} on line {err.lineno}, char {err.colno}'
            )
        except ValueError:
            pass

    # Ensure we didn't exit early already
    if parser:
        # Overwrite a different list this time, and reset it when done
        logria.messages = parser.display_example()
        logria.previous_render = None
        logria.render_text_in_output()
        while True:
            time.sleep(logria.poll_rate)
            logria.activate_prompt()
            command = logria.box.gather().strip()
            if command == ':q':
                logria.messages = logria.previous_messages
                logria.previous_render = None
                reset_parser(logria)
                return  # no need to do any other work
            try:
                command = int(command)
                if command > len(logria.messages):
                    raise ValueError(
                        f'Invalid index {command} not in range 0..{len(logria.messages)}'
                    )
                logria.parser_index = int(command)
                logria.current_status = f'Parsing with {parser.get_name()}, field {parser.get_analytics_for_index(command)}'
                logria.write_to_command_line(logria.current_status)
                break
            except ValueError:
                pass
            except AssertionError:
                pass
        # Set parser
        logria.parser = parser

        # Put pointer back to new list
        logria.messages = logria.parsed_messages
    else:
        logria.messages = logria.previous_messages
        reset_parser(logria, custom_message=custom_message)

    # Render immediately
    logria.previous_render = None

    # Stick to bottom again
    logria.last_index_processed = 0
    logria.stick_to_bottom = True
    logria.stick_to_top = False