Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.bumpversion]
allow_dirty = true
current_version = "0.182.4"
current_version = "0.182.5"

[[tool.bumpversion.files]]
filename = "pyproject.toml"
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"coloredlogs>=15.0.1, <16",
"coverage-conditional-plugin>=0.9.0, <1",
"dycw-pytest-only>=2.1.1, <3",
"dycw-utilities[test]>=0.182.3, <1",
"dycw-utilities[test]>=0.182.4, <1",
"pyright>=1.1.408, <2",
"pytest-cov>=7.0.0, <8",
"pytest-timeout>=2.4.0, <3",
Expand Down Expand Up @@ -128,7 +128,7 @@
name = "dycw-utilities"
readme = "README.md"
requires-python = ">= 3.12"
version = "0.182.4"
version = "0.182.5"

[project.entry-points.pytest11]
pytest-randomly = "utilities.pytest_plugins.pytest_randomly"
Expand Down
26 changes: 26 additions & 0 deletions src/tests/test_atomicwrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
copy,
move,
move_many,
write_text,
writer,
)
from utilities.text import strip_and_dedent

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -212,6 +214,30 @@ def test_many(self, *, tmp_path: Path) -> None:
assert file.read_text() == str(i - 1)


class TestWriteText:
@mark.parametrize("tail", [param(""), param("\n"), param("\n\n"), param("\n\n\n")])
def test_main(self, *, temp_path_not_exist: Path, tail: str) -> None:
text = (
strip_and_dedent("""
a
b
c
""")
+ tail
)
write_text(temp_path_not_exist, text)
result = temp_path_not_exist.read_text()
expected = strip_and_dedent(
"""
a
b
c
""",
trailing=True,
)
assert result == expected


class TestWriter:
def test_main(self, *, temp_path_not_exist: Path) -> None:
with writer(temp_path_not_exist) as temp:
Expand Down
3 changes: 2 additions & 1 deletion src/tests/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pytest import fixture, raises

from utilities.atomicwrites import write_text
from utilities.compression import compress_paths, yield_compressed_contents
from utilities.iterables import one

Expand Down Expand Up @@ -53,7 +54,7 @@ def test_single_file(
tmp_path: Path,
temp_file: Path,
) -> None:
_ = temp_file.write_text("text")
write_text(temp_file, "text")
dest = tmp_path / "dest"
compress_paths(writer, temp_file, dest)
with reader(dest) as buffer:
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/utilities/atomicwrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ def move_many(*paths: tuple[PathLike, PathLike], overwrite: bool = False) -> Non
##


def write_text(path: PathLike, text: str, /, *, overwrite: bool = False) -> None:
"""Write text to a file, ensuring there is exactly 1 trailing new line."""
text_use = text.rstrip("\n") + "\n"
with writer(path, overwrite=overwrite) as temp:
_ = temp.write_text(text_use)


##


@enhanced_context_manager
def writer(
path: PathLike, /, *, compress: bool = False, overwrite: bool = False
Expand Down Expand Up @@ -229,5 +239,6 @@ def __str__(self) -> str:
"copy",
"move",
"move_many",
"write_text",
"writer",
]
5 changes: 2 additions & 3 deletions src/utilities/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
VARIABLE_START_STRING,
)

from utilities.atomicwrites import writer
from utilities.atomicwrites import write_text
from utilities.text import kebab_case, pascal_case, snake_case

if TYPE_CHECKING:
Expand Down Expand Up @@ -108,8 +108,7 @@ def run(self) -> None:
"""Run the job."""
match self.mode:
case "write":
with writer(self.target, overwrite=True) as temp:
_ = temp.write_text(self.rendered)
write_text(self.target, self.rendered, overwrite=True)
case "append":
with self.target.open(mode="a") as fh:
_ = fh.write(self.rendered)
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from tempfile import gettempdir as _gettempdir
from typing import TYPE_CHECKING, override

from utilities.atomicwrites import write_text
from utilities.contextlib import enhanced_context_manager
from utilities.warnings import suppress_warnings

Expand Down Expand Up @@ -134,7 +135,7 @@ def _temporary_file_outer(
if data is not None:
_ = temp.write_bytes(data)
if text is not None:
_ = temp.write_text(text)
write_text(temp, text)
yield temp


Expand Down
6 changes: 2 additions & 4 deletions src/utilities/throttle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from whenever import ZonedDateTime

from utilities.atomicwrites import writer
from utilities.atomicwrites import write_text
from utilities.constants import SECOND
from utilities.functions import in_timedelta
from utilities.os import get_env_var
Expand Down Expand Up @@ -131,9 +131,7 @@ def _try_raise(*, raiser: Callable[[], NoReturn] | None = None) -> None:


def _write_throttle(*, path: MaybeCallablePathLike = Path.cwd) -> None:
path = to_path(path)
with writer(path, overwrite=True) as temp:
_ = temp.write_text(get_now_local().format_iso())
write_text(to_path(path), get_now_local().format_iso(), overwrite=True)


@dataclass(kw_only=True, slots=True)
Expand Down
5 changes: 2 additions & 3 deletions src/utilities/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from traceback import TracebackException
from typing import TYPE_CHECKING, override

from utilities.atomicwrites import writer
from utilities.atomicwrites import write_text
from utilities.errors import repr_error
from utilities.iterables import OneEmptyError, one
from utilities.pathlib import module_path, to_path
Expand Down Expand Up @@ -272,8 +272,7 @@ def _make_except_hook_inner(
max_depth=max_depth,
expand_all=expand_all,
)
with writer(path_log, overwrite=True) as temp:
_ = temp.write_text(full)
write_text(path_log, full, overwrite=True)
if path_max_age is not None:
_make_except_hook_purge(path, path_max_age)
if slack_url is not None: # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading