Skip to content

3.5.9 - add if-different preprocessor interface #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
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 .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
major: 3
minor: 5
patch: 8
patch: 9
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions tests/io/test_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions vcorelib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.2.0
# hash=79d8335d8e342b975f1837d3b60a5d84
# hash=e578f4b742db011f745a8c05889f784c
# =====================================

"""
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions vcorelib/io/file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
)
Expand Down
11 changes: 10 additions & 1 deletion vcorelib/paths/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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)