diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index a82af21..2d58c9d 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -76,7 +76,7 @@ jobs: - run: | mk python-release owner=libre-embedded \ - repo=vcorelib version=3.5.8 + repo=vcorelib version=3.5.9 if: | matrix.python-version == '3.12' && matrix.system == 'ubuntu-latest' diff --git a/README.md b/README.md index 63dc83e..3251f9c 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ===================================== generator=datazen version=3.2.0 - hash=dbaeb9acd4862195a49803d20ebfca2c + hash=3ac333f89dda773bb84100031b12bfcb ===================================== --> -# vcorelib ([3.5.8](https://pypi.org/project/vcorelib/)) +# vcorelib ([3.5.9](https://pypi.org/project/vcorelib/)) [![python](https://img.shields.io/pypi/pyversions/vcorelib.svg)](https://pypi.org/project/vcorelib/) ![Build Status](https://github.com/libre-embedded/vcorelib/workflows/Python%20Package/badge.svg) diff --git a/config b/config index e1445ac..fdfdd6f 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit e1445ac3705e2dad143a0c85435c00b88e82c857 +Subproject commit fdfdd6f30d1762c519526a288c3d95e19d3d5ed1 diff --git a/local/variables/package.yaml b/local/variables/package.yaml index 34f98be..36747a2 100644 --- a/local/variables/package.yaml +++ b/local/variables/package.yaml @@ -1,4 +1,4 @@ --- major: 3 minor: 5 -patch: 8 +patch: 9 diff --git a/pyproject.toml b/pyproject.toml index 4fcaf28..9e393de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__" [project] name = "vcorelib" -version = "3.5.8" +version = "3.5.9" description = "A collection of core Python utilities." readme = "README.md" requires-python = ">=3.12" diff --git a/tests/io/test_file_writer.py b/tests/io/test_file_writer.py index aaa8581..805218a 100644 --- a/tests/io/test_file_writer.py +++ b/tests/io/test_file_writer.py @@ -25,8 +25,10 @@ def test_file_writer_different(): with TemporaryDirectory() as tmpdir: out = Path(tmpdir, "out.txt") - with IndentedFileWriter.from_path_if_different(out) as writer: - writer.write("test123") + with IndentedFileWriter.from_path_if_different( + out, preprocessor=lambda _: "test123" + ) as writer: + writer.write("") with IndentedFileWriter.from_path_if_different(out) as writer: writer.write("test123") diff --git a/vcorelib/__init__.py b/vcorelib/__init__.py index d055059..3222e51 100644 --- a/vcorelib/__init__.py +++ b/vcorelib/__init__.py @@ -1,7 +1,7 @@ # ===================================== # generator=datazen # version=3.2.0 -# hash=79d8335d8e342b975f1837d3b60a5d84 +# hash=e578f4b742db011f745a8c05889f784c # ===================================== """ @@ -10,7 +10,7 @@ DESCRIPTION = "A collection of core Python utilities." PKG_NAME = "vcorelib" -VERSION = "3.5.8" +VERSION = "3.5.9" # vcorelib-specific content. DEFAULT_INDENT = 2 diff --git a/vcorelib/io/file_writer.py b/vcorelib/io/file_writer.py index 430a957..05543f0 100644 --- a/vcorelib/io/file_writer.py +++ b/vcorelib/io/file_writer.py @@ -15,7 +15,11 @@ # internal from vcorelib import DEFAULT_ENCODING -from vcorelib.paths.context import tempfile, text_stream_if_different +from vcorelib.paths.context import ( + TextPreprocessor, + tempfile, + text_stream_if_different, +) class CommentStyle(Enum): @@ -113,11 +117,17 @@ def from_path( @staticmethod @contextmanager def from_path_if_different( - path: Path, space: str = " ", per_indent: int = 1, **kwargs + path: Path, + space: str = " ", + per_indent: int = 1, + preprocessor: TextPreprocessor = None, + **kwargs, ) -> Iterator["IndentedFileWriter"]: """Create an instance from a path as a managed context.""" - with text_stream_if_different(path) as stream: + with text_stream_if_different( + path, preprocessor=preprocessor + ) as stream: yield IndentedFileWriter( stream, space=space, per_indent=per_indent, **kwargs ) diff --git a/vcorelib/paths/context.py b/vcorelib/paths/context.py index c893a62..80f9b76 100644 --- a/vcorelib/paths/context.py +++ b/vcorelib/paths/context.py @@ -9,6 +9,7 @@ from os import makedirs as _makedirs from pathlib import Path as _Path from tempfile import NamedTemporaryFile as _NamedTemporaryFile +from typing import Callable as _Callable from typing import Iterator as _Iterator from typing import TextIO as _TextIO @@ -118,8 +119,13 @@ def write_text_if_different(path: _Path, data: str) -> bool: return do_write +TextPreprocessor = _Callable[[str], str] + + @contextmanager -def text_stream_if_different(path: _Path) -> _Iterator[_TextIO]: +def text_stream_if_different( + path: _Path, preprocessor: TextPreprocessor = None +) -> _Iterator[_TextIO]: """ Writes the contents of a string stream if it differs from output path data. @@ -131,4 +137,7 @@ def text_stream_if_different(path: _Path) -> _Iterator[_TextIO]: finally: content = stream.getvalue() + if preprocessor: + content = preprocessor(content) + write_text_if_different(path, content)