コード例 #1
0
    def fhandle(self):  # was get_handle
        """File handle for CXTgeo (read only)"""

        if self._fhandle and "Swig Object of type 'FILE" in str(self._fhandle):
            return self._fhandle

        fhandle = None
        if (isinstance(self._name, io.BytesIO) and self._mode == "rb"
                and plfsys() == "Linux"):
            buf = self._name.getvalue(
            )  # bytes type in Python3, str in Python2

            # note that the typemap in swig computes the length for the buf!
            fhandle = _cxtgeo.xtg_fopen_bytestream(buf, self._mode)
            logger.debug("Filehandle for byte stream: %s", fhandle)

        elif (isinstance(self._name, io.BytesIO) and self._mode == "rb"
              and (plfsys() == "Windows" or plfsys() == "Darwin")):
            # windows/mac miss fmemopen; write buffer to a tmp instead as workaround
            fds, tmpfile = mkstemp(prefix="tmpxtgeoio")
            os.close(fds)
            with open(tmpfile, "wb") as newfile:
                newfile.write(self._name.getvalue())

            # now open this a regular fhandle
            fhandle = _cxtgeo.xtg_fopen(tmpfile, self._mode)
            self._tmpfile = tmpfile

        else:
            fhandle = _cxtgeo.xtg_fopen(self._name, self._mode)

        self._fhandle = fhandle
        return self._fhandle
コード例 #2
0
ファイル: sys.py プロジェクト: magnesj/xtgeo
    def fhandle(self):  # was get_handle
        """SWIG file handle for CXTgeo, the filehandle is a read only property"""

        logger.info("Get SWIG fhandle...")

        if self._fhandle and "Swig Object of type 'FILE" in str(self._fhandle):
            return self._fhandle

        fhandle = None
        if (isinstance(self._file, io.BytesIO) and self._mode == "rb"
                and plfsys() == "Linux"):
            if six.PY2:
                raise NotImplementedError(
                    "Reading BytesIO not fully supported in Python 2")

            fobj = self._file.getvalue(
            )  # bytes type in Python3, str in Python2

            # note that the typemap in swig computes the length for the buf/fobj!
            self._memstream = True

        elif (isinstance(self._file, io.BytesIO) and self._mode == "wb"
              and plfsys() == "Linux"):
            if six.PY2:
                raise NotImplementedError(
                    "Writing to BytesIO not supported in Python 2")

            fobj = bytes()
            self._memstream = True

        elif (isinstance(self._file, io.BytesIO) and self._mode == "rb"
              and (plfsys() == "Windows" or plfsys() == "Darwin")):
            # windows/mac miss fmemopen; write buffer to a tmp instead as workaround
            fds, fobj = mkstemp(prefix="tmpxtgeoio")
            os.close(fds)
            with open(fobj, "wb") as newfile:
                newfile.write(self._file.getvalue())

            self._tmpfile = fobj

        else:
            fobj = self.name

        if self._memstream:
            fhandle = _cxtgeo.xtg_fopen_bytestream(fobj, self._mode)

        else:
            try:
                fhandle = _cxtgeo.xtg_fopen(fobj, self._mode)
            except TypeError as err:
                reason = ""
                if six.PY2:
                    reason = "In Python 2, do not use __future__ UnicodeLiterals!"
                logger.critical("Cannot open file: %s. %s", err, reason)

        self._fhandle = fhandle
        return self._fhandle
コード例 #3
0
    def get_cfhandle(self):  # was get_handle
        """
        Get SWIG C file handle for CXTgeo

        This is tied to cfclose() which closes the file.

        if _cfhandle already exists, then _cfhandlecount is increased with 1

        """

        # differ on Linux and other OS as Linux can use fmemopen() in C
        islinux = True
        if plfsys() != "Linux":
            islinux = False

        if self._cfhandle and "Swig Object of type 'FILE" in str(
                self._cfhandle):
            self._cfhandlecount += 1
            logger.info("Get SWIG C fhandle no %s", self._cfhandlecount)
            return self._cfhandle

        if isinstance(self._file,
                      io.BytesIO) and self._mode == "rb" and islinux:

            fobj = self._file.getvalue(
            )  # bytes type in Python3, str in Python2

            # note that the typemap in swig computes the length for the buf/fobj!
            self._memstream = True

        elif isinstance(self._file,
                        io.BytesIO) and self._mode == "wb" and islinux:
            fobj = bytes()
            self._memstream = True

        elif (isinstance(self._file, io.BytesIO) and self._mode == "rb"
              and not islinux  # Windows or Darwin
              ):
            # windows/mac miss fmemopen; write buffer to a tmp instead as workaround
            fds, self._tmpfile = mkstemp(prefix="tmpxtgeoio")
            os.close(fds)
            with open(self._tmpfile, "wb") as newfile:
                newfile.write(self._file.getvalue())

        else:
            fobj = self.name

        if self._memstream:
            if islinux:
                cfhandle = _cxtgeo.xtg_fopen_bytestream(fobj, self._mode)
            else:
                cfhandle = _cxtgeo.xtg_fopen(self._tmpfile, self._mode)

        else:
            try:
                cfhandle = _cxtgeo.xtg_fopen(fobj, self._mode)
            except TypeError as err:
                logger.critical("Cannot open file: %s", err)

        self._cfhandle = cfhandle
        self._cfhandlecount = 1

        logger.info("Get initial SWIG C fhandle no %s", self._cfhandlecount)
        return self._cfhandle