Skip to content

Commit

Permalink
Support local gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
dpetzold committed Dec 13, 2024
1 parent 5abf2da commit 7d2d5b9
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions aws_log_parser/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib
import importlib.util
import sys
import gzip

from dataclasses import dataclass, fields, field
from pathlib import Path
Expand Down Expand Up @@ -88,6 +89,12 @@ def parse(self, content):
log_entries = self.run_plugin(plugin, log_entries)
yield from log_entries

def yield_gzipped(self, fh):
yield from [line for line in fh.read().decode("utf-8").splitlines()]

def yield_plain(self, fh):
yield from fh.readlines()

def read_file(self, path):
"""
Yield parsed log entries from the given file.
Expand All @@ -100,8 +107,11 @@ def read_file(self, path):
"""
if self.verbose:
print(f"Reading file://{path}")
with open(path) as log_data:
yield from self.parse(log_data.readlines())
open_func = gzip.open if path.suffix == ".gz" else open
open_mode = "rb" if path.suffix == ".gz" else "r"
read_func = self.yield_gzipped if path.suffix == ".gz" else self.yield_plain
with open_func(path, open_mode) as log_data:
yield from self.parse(read_func(log_data))

def read_files(self, pathname):
"""
Expand All @@ -114,11 +124,11 @@ def read_files(self, pathname):
:rtype: Dependant on log_type.
"""
path = Path(pathname)
if path.is_file():
yield from self.read_file(path)
else:
if path.is_dir():
for p in path.glob(f"**/*{self.file_suffix}"):
yield from self.read_file(p)
else:
yield from self.read_file(path)

def read_s3(self, bucket, prefix, endswith=None):
"""
Expand Down

0 comments on commit 7d2d5b9

Please sign in to comment.