Skip to content

Commit

Permalink
no need for read(), do not offer write(), use >with open()< instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
relleums committed Sep 15, 2023
1 parent bb02e2c commit 72fedb9
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 52 deletions.
14 changes: 0 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,6 @@ the source-file.
rnw.move(src="machine/with/src", dst="other/machine/in/network/dst")
write
=====

When ``with open(...`` is too much. Writes to temporary file in targt's
directory first before moving the temporary file to the target.

.. code-block:: python
import rename_after_writing as rnw
rnw.write(content="My text.", path="another/machine/dst.txt", mode="wt")
.. |BlackStyle| image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black

Expand Down
37 changes: 0 additions & 37 deletions rename_after_writing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,6 @@ def move(src, dst):
raise


def write(content, path, mode="wt"):
"""
Writes content to `path.tmp` and attempt an atomic move to `path`.
Parameters
----------
content : bytes / str
Payload to be written.
path : str
Path to destination.
mode : str (`wt`)
Mode of writing.
"""
copy_id = uuid.uuid4().__str__()
tmp_path = "{:s}.{:s}.tmp".format(path, copy_id)
with open(tmp_path, mode) as f:
f.write(content)
move(src=tmp_path, dst=path)


def read(path, mode="rt"):
"""
I think reading an entire file is rather safe across the nfs.
But in case I am wrong, here is the wrapper.
Parameters
----------
path : str
Path to source.
mode : str (`rt`)
Mode of reading.
"""
with open(path, mode) as f:
content = f.read()
return content


def open(file, mode, tmp_dir=None):
if "r" in str.lower(mode):
return builtins.open(file=file, mode=mode)
Expand Down
2 changes: 1 addition & 1 deletion rename_after_writing/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.3"
__version__ = "0.0.4"

0 comments on commit 72fedb9

Please sign in to comment.