From a51235e25e1effa8bf51d741a8314e798f5f8941 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 19 May 2022 12:19:17 +0200 Subject: [PATCH] Create more convenient constructor for LogParser --- src/log_read.py | 24 +++++++++++++++++++++++- 1 files changed, 23 insertions(+), 1 deletions(-) diff --git a/src/log_read.py b/src/log_read.py index e50e046..3b973bf 100644 --- a/src/log_read.py +++ b/src/log_read.py @@ -50,12 +50,12 @@ INTERFACE ------------------------------------------------------ """ - import os import os.path import re from warnings import warn import logging +from contextlib import contextmanager from .iter_helpers import zip_longest from .type_helpers import is_str_or_byte, is_file_obj @@ -404,3 +404,25 @@ class LogParser(LineReader): else: self.last_unparsed_line = raw_line yield description, None, idx + + @classmethod + @contextmanager + def create_for(cls, filename, *args, **kwargs): + """ + Open single file, yield LogParser. Ensures file is closed afterwards. + + This allows opening file and creation LogParser for it to one line: + + with LogParser.create_for('/var/log/messages', SYS_LOG_PATTERN) as parser: + for _, matches, _ in parser: + try: + print(matches.groupdict()) + except Exception: + print(f'UNPARSED: {parser.last_unparsed_line}') + + :param str filename: something that :py:meth:`open` accepts + :param args: Forwarded to constructor + :param kwargs: Forwarded to constructor + """ + with open(filename) as file_handle: + yield cls(file_handle, *args, **kwargs) -- 1.7.1