Skip to content

Commit e5f05b4

Browse files
committed
3.5.9 - add if-different preprocessor interface
1 parent c8452e6 commit e5f05b4

File tree

9 files changed

+35
-14
lines changed

9 files changed

+35
-14
lines changed

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
7777
- run: |
7878
mk python-release owner=libre-embedded \
79-
repo=vcorelib version=3.5.8
79+
repo=vcorelib version=3.5.9
8080
if: |
8181
matrix.python-version == '3.12'
8282
&& matrix.system == 'ubuntu-latest'

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
=====================================
33
generator=datazen
44
version=3.2.0
5-
hash=dbaeb9acd4862195a49803d20ebfca2c
5+
hash=3ac333f89dda773bb84100031b12bfcb
66
=====================================
77
-->
88

9-
# vcorelib ([3.5.8](https://pypi.org/project/vcorelib/))
9+
# vcorelib ([3.5.9](https://pypi.org/project/vcorelib/))
1010

1111
[![python](https://img.shields.io/pypi/pyversions/vcorelib.svg)](https://pypi.org/project/vcorelib/)
1212
![Build Status](https://github.com/libre-embedded/vcorelib/workflows/Python%20Package/badge.svg)

local/variables/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
major: 3
33
minor: 5
4-
patch: 8
4+
patch: 9

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"
44

55
[project]
66
name = "vcorelib"
7-
version = "3.5.8"
7+
version = "3.5.9"
88
description = "A collection of core Python utilities."
99
readme = "README.md"
1010
requires-python = ">=3.12"

tests/io/test_file_writer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ def test_file_writer_different():
2525
with TemporaryDirectory() as tmpdir:
2626
out = Path(tmpdir, "out.txt")
2727

28-
with IndentedFileWriter.from_path_if_different(out) as writer:
29-
writer.write("test123")
28+
with IndentedFileWriter.from_path_if_different(
29+
out, preprocessor=lambda _: "test123"
30+
) as writer:
31+
writer.write("")
3032

3133
with IndentedFileWriter.from_path_if_different(out) as writer:
3234
writer.write("test123")

vcorelib/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# =====================================
22
# generator=datazen
33
# version=3.2.0
4-
# hash=79d8335d8e342b975f1837d3b60a5d84
4+
# hash=e578f4b742db011f745a8c05889f784c
55
# =====================================
66

77
"""
@@ -10,7 +10,7 @@
1010

1111
DESCRIPTION = "A collection of core Python utilities."
1212
PKG_NAME = "vcorelib"
13-
VERSION = "3.5.8"
13+
VERSION = "3.5.9"
1414

1515
# vcorelib-specific content.
1616
DEFAULT_INDENT = 2

vcorelib/io/file_writer.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
# internal
1717
from vcorelib import DEFAULT_ENCODING
18-
from vcorelib.paths.context import tempfile, text_stream_if_different
18+
from vcorelib.paths.context import (
19+
TextPreprocessor,
20+
tempfile,
21+
text_stream_if_different,
22+
)
1923

2024

2125
class CommentStyle(Enum):
@@ -113,11 +117,17 @@ def from_path(
113117
@staticmethod
114118
@contextmanager
115119
def from_path_if_different(
116-
path: Path, space: str = " ", per_indent: int = 1, **kwargs
120+
path: Path,
121+
space: str = " ",
122+
per_indent: int = 1,
123+
preprocessor: TextPreprocessor = None,
124+
**kwargs,
117125
) -> Iterator["IndentedFileWriter"]:
118126
"""Create an instance from a path as a managed context."""
119127

120-
with text_stream_if_different(path) as stream:
128+
with text_stream_if_different(
129+
path, preprocessor=preprocessor
130+
) as stream:
121131
yield IndentedFileWriter(
122132
stream, space=space, per_indent=per_indent, **kwargs
123133
)

vcorelib/paths/context.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from os import makedirs as _makedirs
1010
from pathlib import Path as _Path
1111
from tempfile import NamedTemporaryFile as _NamedTemporaryFile
12+
from typing import Callable as _Callable
1213
from typing import Iterator as _Iterator
1314
from typing import TextIO as _TextIO
1415

@@ -118,8 +119,13 @@ def write_text_if_different(path: _Path, data: str) -> bool:
118119
return do_write
119120

120121

122+
TextPreprocessor = _Callable[[str], str]
123+
124+
121125
@contextmanager
122-
def text_stream_if_different(path: _Path) -> _Iterator[_TextIO]:
126+
def text_stream_if_different(
127+
path: _Path, preprocessor: TextPreprocessor = None
128+
) -> _Iterator[_TextIO]:
123129
"""
124130
Writes the contents of a string stream if it differs from output path
125131
data.
@@ -131,4 +137,7 @@ def text_stream_if_different(path: _Path) -> _Iterator[_TextIO]:
131137
finally:
132138
content = stream.getvalue()
133139

140+
if preprocessor:
141+
content = preprocessor(content)
142+
134143
write_text_if_different(path, content)

0 commit comments

Comments
 (0)