From a7115ff599a7dccb9fdb1cfe0afc541429038c94 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 11 May 2017 18:09:39 +0200 Subject: [PATCH] Defining failing methods for a flile-like object. --- lib/trace_maillog/any_uncompress_file.py | 76 +++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/lib/trace_maillog/any_uncompress_file.py b/lib/trace_maillog/any_uncompress_file.py index 4b9695b..b87591b 100644 --- a/lib/trace_maillog/any_uncompress_file.py +++ b/lib/trace_maillog/any_uncompress_file.py @@ -36,7 +36,7 @@ import six # Own modules from trace_maillog import magic -__version__ = '0.3.3' +__version__ = '0.4.1' LOG = logging.getLogger(__name__) @@ -46,6 +46,12 @@ class BaseAnyUncompressError(Exception): """Base class for all exceptions defined in this module.""" pass +# ============================================================================= +class NoFilehandleError(BaseAnyUncompressError, RuntimeError): + + def __str__(self): + return "The undelaying file handle is already destroyed." + # ============================================================================= class InvalidCompressionError(BaseAnyUncompressError, NotImplementedError): @@ -166,6 +172,74 @@ class AnyUncompressFile(object): self.container.close() self.container = None + # ------------------------------------------------------------------------- + @property + def closed(self): + '''True if the stream is closed.''' + if self._fh: + return self._fh.closed + return True + + # ------------------------------------------------------------------------- + def flush(self): + if self._fh: + return self._fh.flush() + return None + + # ------------------------------------------------------------------------- + def fileno(self): + if self._fh: + return self._fh.fileno() + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def isatty(self): + if self._fh: + return self._fh.isatty() + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def next(self): + if self._fh: + return self._fh.next() + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def peek(self, size): + if self._fh: + return self._fh.peek(size) + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def read(self, size=-1): + if self._fh: + return self._fh.read(size) + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def readline(self, size=-1): + if self._fh: + return self._fh.readline(size) + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def readlines(self, hint=-1): + if self._fh: + return self._fh.readlines(hint) + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def seek(self, offset, whence=os.SEEK_SET) + if self._fh: + return self._fh.seek(offset, whence) + raise NoFilehandleError() + + # ------------------------------------------------------------------------- + def tell(self): + if self._fh: + return self._fh.tell() + raise NoFilehandleError() + # ------------------------------------------------------------------------- if HAS_BZIP2: compression_types['bz2']['supported'] = True -- 2.39.5