def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): """ Open the named file in the given mode. This method downloads the file contents into a local temporary file so that it can be worked on efficiently. Any changes made to the file are only sent back to cloud storage when the file is flushed or closed. @param path: Id of the file to be opened @param mode: In which mode to open the file @raise ResourceNotFoundError: If given path doesn't exist and 'w' is not in mode @return: RemoteFileBuffer object """ path = self._normpath(path) spooled_file = SpooledTemporaryFile(mode=mode, bufsize=MAX_BUFFER) # Truncate the file if requested if "w" in mode: try: self._update(path, "") except: path = self.createfile(path, True) else: try: spooled_file.write(self.client.get_file(path)) spooled_file.seek(0, 0) except Exception as e: if "w" not in mode and "a" not in mode: raise ResourceNotFoundError("%r" % e) else: path = self.createfile(path, True) return RemoteFileBuffer(self, path, mode, spooled_file)
def open(self, path, mode="rb", **kwargs): """Open the named file in the given mode. This method downloads the file contents into a local temporary file so that it can be worked on efficiently. Any changes made to the file are only sent back to cloud storage when the file is flushed or closed. :param path: Path to the file to be opened :param mode: In which mode to open the file :raise ResourceNotFoundError: If given path doesn't exist and 'w' is not in mode :return: RemoteFileBuffer object """ path = abspath(normpath(path)) spooled_file = SpooledTemporaryFile(mode=mode, bufsize=MAX_BUFFER) if "w" in mode: # Truncate the file if requested self.client.put_file(path, "", True) else: # Try to write to the spooled file, if path doesn't exist create it # if 'w' is in mode try: spooled_file.write(self.client.get_file(path).read()) spooled_file.seek(0, 0) except: if "w" not in mode: raise ResourceNotFoundError(path) else: self.createfile(path, True) # This will take care of closing the socket when it's done. return RemoteFileBuffer(self, path, mode, spooled_file)
def open(self, path, mode='r', **kwargs): if self.isdir(path): raise ResourceInvalidError(path) # Erase the contents of a file upon write. if 'w' in mode: file_obj = None self.setcontents(path, StringIO()) else: file_obj = SpooledTemporaryFile() self._retrieveFile(path, file_obj) return RemoteFileBuffer(self, path, mode, file_obj)
def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): """Open the named file in the given mode. This method downloads the file contents into a local temporary file so that it can be worked on efficiently. Any changes made to the file are only sent back to cloud storage when the file is flushed or closed. :param path: Id of the file to be opened :param mode: In which mode to open the file :raises ResourceNotFoundError: If given path doesn't exist and 'w' is not in mode :return: RemoteFileBuffer object """ path = self._normpath(path) spooled_file = SpooledTemporaryFile(mode=mode, bufsize=MAX_BUFFER) # Truncate the file if requested if "w" in mode: try: self._update(path, "") except: path = self.createfile(path, True) else: try: spooled_file.write(self.client.get_file(path)) spooled_file.seek(0, 0) except Exception as e: if "w" not in mode and "a" not in mode: raise ResourceNotFoundError("%r" % e) else: path = self.createfile(path, True) return RemoteFileBuffer(self, path, mode, spooled_file)
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)
def __init__(self, fs, filename, mode, handler, close_callback, write_on_flush=True, debug=0): self.debug = debug self.fs = fs self.filename = filename self.mode = mode self.ccasclient = handler self.close_callback = close_callback self.write_on_flush = write_on_flush self.offset = 0 self.op = None wrapped_file = SpooledTemporaryFile(max_size=self.max_size_in_memory) self._changed = False self._readlen = 0 # How many bytes already loaded from rfile 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 not self.ccasclient.exists(self.filename): # File was just created, force to write anything self.op = 'write' self._changed = True self._eof = True else: # Do not use remote file object self.op = 'write' self._eof = True self._changed = True 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)