From 009696ba6b8ca68e3cae69349a6db8d9da12b0c8 Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Sat, 23 Mar 2024 00:30:57 +0100 Subject: [PATCH 1/4] Add plugin entry points Allow other packages to customise both the formatters and the FormatParser class by using custom entrypoints Signed-off-by: Phil Ewels --- src/toolong/format_parser.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/toolong/format_parser.py b/src/toolong/format_parser.py index 36c36fa..a6f6ecc 100644 --- a/src/toolong/format_parser.py +++ b/src/toolong/format_parser.py @@ -1,5 +1,6 @@ from __future__ import annotations from datetime import datetime +from importlib_metadata import entry_points import json import re from typing_extensions import TypeAlias @@ -111,6 +112,11 @@ def parse(self, line: str) -> ParseResult | None: # DefaultLogFormat(), ] +# Plugin entry point for other packages to overwrite formatters +for entry_point in entry_points(group="toolong.application.formats"): + format_plugin = entry_point.load() + FORMATS = format_plugin(FORMATS) + default_log_format = DefaultLogFormat() @@ -135,3 +141,9 @@ def parse(self, line: str) -> ParseResult: if parse_result is not None: return parse_result return None, "", Text() + + +# Plugin entry point for other packages to overwrite FormatParser +for entry_point in entry_points(group="toolong.application.format_parsers"): + format_parser_plugin = entry_point.load() + FormatParser = format_parser_plugin(FormatParser) From b09540359bd5a6f7646a64bbea3430ba69c6b35d Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Sat, 23 Mar 2024 23:40:40 +0100 Subject: [PATCH 2/4] Add entry point for toolong init --- src/toolong/ui.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/toolong/ui.py b/src/toolong/ui.py index c14c33e..a209338 100644 --- a/src/toolong/ui.py +++ b/src/toolong/ui.py @@ -3,6 +3,7 @@ import locale from pathlib import Path +from importlib_metadata import entry_points from rich import terminal_theme from textual.app import App, ComposeResult @@ -118,6 +119,11 @@ def __init__( self.watcher = get_watcher() super().__init__() + # Plugin entry point for other packages + for entry_point in entry_points(group="toolong.application.on_init"): + on_init_plugin = entry_point.load() + on_init_plugin(self) + async def on_mount(self) -> None: self.ansi_theme_dark = terminal_theme.DIMMED_MONOKAI await self.push_screen(LogScreen()) From f661cee70923fddbe0e70d479592d1c2779287e0 Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Mon, 25 Mar 2024 23:34:18 +0100 Subject: [PATCH 3/4] Reduce 3 entry-points to one, move to a different location --- src/toolong/format_parser.py | 10 +--------- src/toolong/log_file.py | 5 +++++ src/toolong/ui.py | 6 ------ 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/toolong/format_parser.py b/src/toolong/format_parser.py index a6f6ecc..3285adb 100644 --- a/src/toolong/format_parser.py +++ b/src/toolong/format_parser.py @@ -1,6 +1,5 @@ from __future__ import annotations from datetime import datetime -from importlib_metadata import entry_points import json import re from typing_extensions import TypeAlias @@ -112,10 +111,6 @@ def parse(self, line: str) -> ParseResult | None: # DefaultLogFormat(), ] -# Plugin entry point for other packages to overwrite formatters -for entry_point in entry_points(group="toolong.application.formats"): - format_plugin = entry_point.load() - FORMATS = format_plugin(FORMATS) default_log_format = DefaultLogFormat() @@ -143,7 +138,4 @@ def parse(self, line: str) -> ParseResult: return None, "", Text() -# Plugin entry point for other packages to overwrite FormatParser -for entry_point in entry_points(group="toolong.application.format_parsers"): - format_parser_plugin = entry_point.load() - FormatParser = format_parser_plugin(FormatParser) + diff --git a/src/toolong/log_file.py b/src/toolong/log_file.py index adeb948..4d191fc 100644 --- a/src/toolong/log_file.py +++ b/src/toolong/log_file.py @@ -1,6 +1,7 @@ from __future__ import annotations from datetime import datetime +from importlib_metadata import entry_points import os import mmap import mimetypes @@ -36,6 +37,10 @@ def __init__(self, path: str) -> None: self.timestamp_scanner = TimestampScanner() self.format_parser = FormatParser() self._lock = Lock() + # Plugin entry point for other packages to overwrite the format parsers + for entry_point in entry_points(group="toolong.application.format_parsers"): + format_parser_plugin = entry_point.load() + self.format_parser = format_parser_plugin(self) def __rich_repr__(self) -> rich.repr.Result: yield self.name diff --git a/src/toolong/ui.py b/src/toolong/ui.py index a209338..c14c33e 100644 --- a/src/toolong/ui.py +++ b/src/toolong/ui.py @@ -3,7 +3,6 @@ import locale from pathlib import Path -from importlib_metadata import entry_points from rich import terminal_theme from textual.app import App, ComposeResult @@ -119,11 +118,6 @@ def __init__( self.watcher = get_watcher() super().__init__() - # Plugin entry point for other packages - for entry_point in entry_points(group="toolong.application.on_init"): - on_init_plugin = entry_point.load() - on_init_plugin(self) - async def on_mount(self) -> None: self.ansi_theme_dark = terminal_theme.DIMMED_MONOKAI await self.push_screen(LogScreen()) From e19996259de2eb7e121b8425e15f35a47b7298cf Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Tue, 26 Mar 2024 00:03:37 +0100 Subject: [PATCH 4/4] Remove superflous whitespace that crept in --- src/toolong/format_parser.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/toolong/format_parser.py b/src/toolong/format_parser.py index 3285adb..36c36fa 100644 --- a/src/toolong/format_parser.py +++ b/src/toolong/format_parser.py @@ -111,7 +111,6 @@ def parse(self, line: str) -> ParseResult | None: # DefaultLogFormat(), ] - default_log_format = DefaultLogFormat() @@ -136,6 +135,3 @@ def parse(self, line: str) -> ParseResult: if parse_result is not None: return parse_result return None, "", Text() - - -