示例#1
0
    def __init__(self, *args, **kwds):
        """CacheFSMixin constructor.

        The optional keyword argument 'cache_timeout' specifies the cache
        timeout in seconds.  The default timeout is 1 second.  To prevent
        cache entries from ever timing out, set it to None.

        The optional keyword argument 'max_cache_size' specifies the maximum
        number of entries to keep in the cache.  To allow the cache to grow
        without bound, set it to None.  The default is 1000.
        """
        self.cache_timeout = kwds.pop("cache_timeout", 1)
        self.max_cache_size = kwds.pop("max_cache_size", 1000)
        self.__cache = PathMap()
        self.__cache_size = 0
        self.__cache_lock = threading.RLock()
        super(CacheFSMixin, self).__init__(*args, **kwds)
示例#2
0
    def __init__(self, fs, path, mode, rfile=None, write_on_flush=True):
        """RemoteFileBuffer constructor.

        The owning filesystem, path and mode must be provided.  If the
        optional argument 'rfile' is provided, it must be a read()-able
        object or a string containing the initial file contents.
        """
        wrapped_file = SpooledTemporaryFile(max_size=self.max_size_in_memory)
        self.fs = fs
        self.path = path
        self.write_on_flush = write_on_flush
        self._changed = False
        self._readlen = 0  # How many bytes already loaded from rfile
        self._rfile = None  # Reference to remote file object
        self._eof = False  # Reached end of rfile?
        if getattr(fs, "_lock", None) is not None:
            self._lock = fs._lock.__class__()
        else:
            self._lock = threading.RLock()

        if "r" in mode or "+" in mode or "a" in mode:
            if rfile is None:
                # File was just created, force to write anything
                self._changed = True
                self._eof = True

            if not hasattr(rfile, "read"):
                #rfile = StringIO(unicode(rfile))
                rfile = StringIO(rfile)

            self._rfile = rfile
        else:
            # Do not use remote file object
            self._eof = True
            self._rfile = None
            self._changed = True
            if rfile is not None and hasattr(rfile, "close"):
                rfile.close()
        super(RemoteFileBuffer, self).__init__(wrapped_file, mode)
        # FIXME: What if mode with position on eof?
        if "a" in mode:
            # Not good enough...
            self.seek(0, SEEK_END)
示例#3
0
 def __setstate__(self, state):
     super(CacheFSMixin, self).__setstate__(state)
     self.__cache = PathMap()
     self.__cache_size = 0
     self.__cache_lock = threading.RLock()