def cancel(self): """ After calling this method the session cannot be used anymore. """ if self._state < self.REQUESTING or self._state >= self.CACHED: return self.log("Canceling caching session for %s", self.url) self.strategy._onSessionCanceled(self) self.cache_stats.onCopyCancelled(self.size, self._bytes) self._close() error = fileprovider.FileOutOfDate("File out of date") self._fireError(error) if self._request: self.debug("Caching canceled for %s (%d/%d Bytes ~ %d %%)", self.url, self._bytes, self.size, self.size and int(self._bytes * 100 / self.size)) self._request.cancel() self._request = None else: self.debug("Caching canceled before starting to cache") self._state = self.CANCELED
def read(self, offset, size): if self._state == self.CANCELED: raise fileprovider.FileOutOfDate("File out of date") if self._state == self.ABORTED: return None if self._state >= self.CLOSED: raise fileprovider.FileClosedError("Session Closed") if self._file is None: return None if min(self.size, offset + size) > self._bytes: return None self._file.seek(offset) return self._file.read(size)
def streamNotAvailable(self, getter, code, message): assert self._deferred is not None, "Not retrieving anything" error = fileprovider.FileOutOfDate(message) self._deferred.errback(error) self._cleanup()
def conditionFail(self, getter, code, message): assert self._deferred is not None, "Not retrieving anything" self._deferred.errback(fileprovider.FileOutOfDate(message)) self._cleanup()