From af4f13a47320e30d45150b5c22eea28104573e09 Mon Sep 17 00:00:00 2001 From: Alfred Wingate Date: Thu, 5 Oct 2023 19:51:18 +0300 Subject: Open files with same function to allow decompression to work seamlessly * liblzma left mostly untouched, next commit will port it to lzma. * BZ2File -> open to allow plaintext reading, which is expected elsewhere in elogv. Signed-off-by: Alfred Wingate --- elogv | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/elogv b/elogv index 7e1e37f..b3455c7 100755 --- a/elogv +++ b/elogv @@ -418,18 +418,22 @@ class ElogViewer: self.logf_wrap = self.wrap_logf_lines() self.show_log() - def openfile(self, myfile): - if myfile.endswith('.xz'): + @staticmethod + def open(file, mode='rt'): + if file.endswith('.xz'): if not no_liblzma: - self.logf = liblzma.LZMAFile(myfile) + return liblzma.LZMAFile(file) else: sys.exit('You need pyliblzma library to be able to read xz compressed elog files.\nhttp://pypi.python.org/pypi/pyliblzma') - elif myfile.endswith('.gz'): - self.logf = gzip.open(myfile) - elif myfile.endswith('.bz2'): - self.logf = bz2.BZ2File(myfile) + elif file.endswith('.gz'): + return gzip.open(file, mode=mode) + elif file.endswith('.bz2'): + return bz2.open(file, mode=mode) else: - self.logf = open(myfile) + return open(file, mode=mode) + + def openfile(self, file): + self.logf = self.open(file) def refresh_file_pad(self): """ @@ -528,7 +532,7 @@ class ElogViewer: """ Get the highest elog class in a file """ - with open(filepath) as f: + with self.open(filepath) as f: classes = re.findall("LOG:|INFO:|WARN:|ERROR:", f.read()) if "ERROR:" in classes: -- cgit v1.2.3-65-gdbad From 1d95e751bd8d0628f1b83ca667cc1d4cd18066a1 Mon Sep 17 00:00:00 2001 From: Alfred Wingate Date: Thu, 5 Oct 2023 19:56:45 +0300 Subject: Port to lzma Signed-off-by: Alfred Wingate --- elogv | 12 ++---------- pyproject.toml | 3 --- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/elogv b/elogv index b3455c7..94d466a 100755 --- a/elogv +++ b/elogv @@ -33,6 +33,7 @@ import gettext import locale import gzip import bz2 +import lzma import signal _LOCALE_CATEGORY_PAIRS = ( @@ -46,12 +47,6 @@ _LOCALE_CATEGORY_PAIRS = ( (locale.LC_ALL, 'LC_ALL'), ) -no_liblzma = False -try: - import liblzma -except ImportError: - no_liblzma = True - def report_bad_locale(variable, value): py_version = '%s.%s.%s' % sys.version_info[:3] @@ -421,10 +416,7 @@ class ElogViewer: @staticmethod def open(file, mode='rt'): if file.endswith('.xz'): - if not no_liblzma: - return liblzma.LZMAFile(file) - else: - sys.exit('You need pyliblzma library to be able to read xz compressed elog files.\nhttp://pypi.python.org/pypi/pyliblzma') + return lzma.open(file, mode=mode) elif file.endswith('.gz'): return gzip.open(file, mode=mode) elif file.endswith('.bz2'): diff --git a/pyproject.toml b/pyproject.toml index 88ad65f..0476844 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,3 @@ dependencies = ["portage"] [project.urls] homepage = "https://gitweb.gentoo.org/proj/elogv.git/" - -[project.optional-dependencies] -lzma = [ "pyliblzma" ] -- cgit v1.2.3-65-gdbad