def atomic_write(file, as_file=True, **cls_kwargs): """Write a file atomically :param file: str or :class:`os.PathLike` target to write :param bool as_file: if True, the yielded object is a :class:File. (eg, what you get with `open(...)`). Otherwise, it will be the temporary file path string :param kwargs: anything else needed to open the file :raises: FileExistsError if target exists Example:: with atomic_write("hello.txt") as f: f.write("world!") """ # You can override things just fine... with _backend_writer(file, writer_cls=SuffixWriter, **cls_kwargs) as tmp: if as_file: yield tmp else: yield tmp.name
def atomic_write(file, mode="w", as_file=True, new_default="asdf", **kwargs): """ Wrapper function for atomicwrites.atomic_write. The SuffixWriter class keeps the file extension of the original file. :param file: str or :class:'os.PathLike' target to write :param bool as_file: if True, the yielded object is a :class:File. (eg, what you get with `open(...)`). Otherwise, it will be the temporary file path string :param kwargs: anything else needed to open the file :raises: FileExistsError if target exists Example:: with atomic_write("hello.txt") as f: f.write("world!") """ # User _backend_writer to implement the writing. with _backend_writer(file, writer_cls=SuffixWriter, **kwargs) as f: if not as_file: #pragma: no cover f = os.fspath(file) # return the temporary file path string yield f
def atomic_write(file, mode="w", as_file=True, new_default="asdf", **kwargs): with _backend_writer(file, mode=mode, writer_cls=SuffixWriter, **kwargs) as f: # yield file 'f' if as_file=True, otherwise yields filepath 'file' if as_file: yield f else: yield f.name
def atomic_write(file, mode='w', as_file=True, new_default='asdf', **kwargs): """ Override the atomic_write function from atomicwrites """ with _backend_writer(file, mode=mode, writer_cls=SuffixWriter, **kwargs) as f: # Return file object if as_file, otherwise return file path if as_file: yield f else: yield f.name
def atomic_write(file, as_file=True, new_default='asdf', **kwargs): # Override atomic_write of atomicwrites. with _backend_writer(file, writer_cls=SuffixWriter, **kwargs) as f: # Logic to yield a path to the temporary file if as_file is False. if not as_file: # Convert an os.Pathlike object to a path str if needed. file = os.fspath(file) yield file # Logic to yield a file like object. else: yield f
def atomic_write(file, mode='w', as_file=True, new_default='asdf', **kwargs): # You can override things just fine... with _backend_writer(file, writer_cls=SuffixWriter, **kwargs) as f: try: # Don't forget to handle the as_file logic! if not as_file: yield f.name else: yield f except (OSError, IOError): remove_temp_file(f.name) raise finally: remove_temp_file(file)
def atomic_write(file, mode="w", as_file=True, **kwargs): # if file already exists through an exception if path.isfile(file): raise FileExistsError("File provided already exists! {}".format(file)) _, filename = path.split(file) _, ext = path.splitext(filename) with _backend_writer(file, writer_cls=EnhancedAtomicWriter, mode=mode, suffix=ext, **kwargs) as f: if as_file: yield f else: yield f.name
def atomic_write(path, writer_cls=SuffixWriter, **cls_kwargs): """overrides the atomic_write function to use modified SuffixWriter writer_cls""" with _backend_writer(path, writer_cls=SuffixWriter, **cls_kwargs) as f: # Don't forget to handle the as_file logic! yield f