]> Frank Brehm's Git Trees - pixelpark/trace-maillog.git/commitdiff
Defining failing methods for a flile-like object.
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 May 2017 16:09:39 +0000 (18:09 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 May 2017 16:09:39 +0000 (18:09 +0200)
lib/trace_maillog/any_uncompress_file.py

index 4b9695b6fb2bf56d65409debb0b2427e55837f18..b87591b4960e8b4510a35dd26f0cf4884f8958d3 100644 (file)
@@ -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