Exemplo n.º 1
0
 def _compressed_stream(self,
                        fname,
                        system_uncompress,
                        python_uncompress,
                        mode='rb'):
     """
     Try to transparently handle gzip / bzip without always getting python
     performance
     """
     # assert that python modules are always OK based on performance benchmark
     # Try to fix the way we are using them?
     fobj = None
     if self._need_a_real_file and mode[0] == "r":
         fo = python_uncompress(fname, mode)
         #            fobj = os.tmpfile()
         #problem when not administrator under certain flavors of windows
         tmpfd, tmpfn = tempfile.mkstemp()
         os.close(tmpfd)
         fobj = fabioutils.File(tmpfn, "w+b")
         fobj.write(fo.read())
         fo.close()
         fobj.seek(0)
     elif self._need_a_seek_to_read and mode[0] == "r":
         fo = python_uncompress(fname, mode)
         fobj = fabioutils.StringIO(fo.read(), fname, mode)
     else:
         fobj = python_uncompress(fname, mode)
     return fobj
Exemplo n.º 2
0
    def _open(self, fname, mode="rb"):
        """
        Try to handle compressed files, streams, shared memory etc
        Return an object which can be used for "read" and "write"
        ... FIXME - what about seek ?
        """
        fileObject = None
        self.filename = fname
        self.filenumber = fabioutils.extract_filenumber(fname)
        if hasattr(fname, "read") and hasattr(fname, "write"):
            # It is already something we can use
            return fname
        if isinstance(fname, (str, unicode)):
            self.header["filename"] = fname
            if os.path.splitext(fname)[1] == ".gz":
                fileObject = self._compressed_stream(
                    fname, fabioutils.COMPRESSORS['.gz'], fabioutils.GzipFile,
                    mode)
            elif os.path.splitext(fname)[1] == '.bz2':
                fileObject = self._compressed_stream(
                    fname, fabioutils.COMPRESSORS['.bz2'], fabioutils.BZ2File,
                    mode)
            #
            # Here we return the file even though it may be bzipped or gzipped
            # but named incorrectly...
            #
            # FIXME - should we fix that or complain about the daft naming?
            else:
                fileObject = fabioutils.File(fname, mode)
            if "name" not in dir(fileObject):
                fileObject.name = fname

        return fileObject