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='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 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="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)