Skip to content

Commit 8de6eb8

Browse files
committed
file frags: use pattern key for consistency with e.g. tool.hatch.version
1 parent 2b8a547 commit 8de6eb8

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ Additionally it’s possible to cut away parts of the file before appending it:
102102

103103
- **`start-after`** cuts away everything before the string specified.
104104
- **`end-before`** cuts away everything after.
105-
- **`regexp`** takes a regular expression and returns the first group from it.
105+
- **`pattern`** takes a [*regular expression*](https://docs.python.org/3/library/re.html) and returns the first group from it (you probably want to make your capture group non-greedy by appending a question mark: `(.*)?`).
106106
Internally, it uses
107107

108108
```python
109-
re.search(regexp, whatever_is_left_after_slicing, re.DOTALL).group(1)
109+
re.search(pattern, whatever_is_left_after_slicing, re.DOTALL).group(1)
110110
```
111111

112112
to find it.
@@ -132,7 +132,7 @@ together with:
132132
path = "path.md"
133133
start-after = "<!-- cut after this -->\n\n"
134134
end-before = "\n\n<!-- but before this -->"
135-
regexp = "the (.*) body"
135+
pattern = "the (.*) body"
136136
```
137137

138138
would append:
@@ -151,7 +151,7 @@ to your readme.
151151
>
152152
> 1. `start-after`
153153
> 2. `end-before`
154-
> 3. `regexp`
154+
> 3. `pattern`
155155
156156
For a complete example, please see our [example configuration][example-config].
157157

src/hatch_fancy_pypi_readme/_fragments.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def from_config(cls, cfg: dict[str, str]) -> Fragment:
103103
path = Path(cfg.pop("path"))
104104
start_after = cfg.pop("start-after", None)
105105
end_before = cfg.pop("end-before", None)
106-
regexp = cfg.pop("regexp", None)
106+
pattern = cfg.pop("pattern", None)
107107

108108
errs: list[str] = []
109109
if cfg:
@@ -129,12 +129,12 @@ def from_config(cls, cfg: dict[str, str]) -> Fragment:
129129
f"file fragment: 'end_before' {end_before!r} not found."
130130
)
131131

132-
if regexp:
132+
if pattern:
133133
try:
134-
m = re.search(regexp, contents, re.DOTALL)
134+
m = re.search(pattern, contents, re.DOTALL)
135135
if not m:
136136
errs.append(
137-
f"file fragment: pattern {regexp!r} not found."
137+
f"file fragment: pattern {pattern!r} not found."
138138
)
139139
else:
140140
try:
@@ -145,7 +145,7 @@ def from_config(cls, cfg: dict[str, str]) -> Fragment:
145145
"defined."
146146
)
147147
except re.error as e:
148-
errs.append(f"file fragment: invalid pattern {regexp!r}: {e}")
148+
errs.append(f"file fragment: invalid pattern {pattern!r}: {e}")
149149

150150
if errs:
151151
raise ConfigurationError(errs)

tests/example_pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Now let's add an extract from [`tests/example_changelog.md`](https://github.com/
3535

3636
[[tool.hatch.metadata.hooks.fancy-pypi-readme.fragments]]
3737
path = "tests/example_changelog.md"
38-
regexp = "<!-- changelog follows -->\n\n\n(.*)\n\n## "
38+
pattern = "<!-- changelog follows -->\n\n\n(.*)?\n\n## "
3939

4040
[[tool.hatch.metadata.hooks.fancy-pypi-readme.fragments]]
4141
text = "\n---\n\nPretty **cool**, huh?"

tests/test_fragments.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,62 +142,62 @@ def test_start_after_end_before_not_found(self, txt_path):
142142
"file fragment: 'end_before' 'also nope' not found.",
143143
] == ei.value.errors
144144

145-
def test_invalid_regexp(self, txt_path):
145+
def test_invalid_pattern(self, txt_path):
146146
"""
147147
re-compilation errors are caught and reported.
148148
"""
149149
with pytest.raises(ConfigurationError) as ei:
150150
FileFragment.from_config(
151151
{
152-
"regexp": r"**",
153152
"path": str(txt_path),
153+
"pattern": r"**",
154154
}
155155
)
156156
assert [
157157
"file fragment: invalid pattern '**': nothing to repeat at "
158158
"position 0"
159159
] == ei.value.errors
160160

161-
def test_regexp_no_match(self, txt_path):
161+
def test_pattern_no_match(self, txt_path):
162162
"""
163-
If the regexp doesn't match, a helpful error is raises.
163+
If the pattern doesn't match, a helpful error is raises.
164164
"""
165165
with pytest.raises(ConfigurationError) as ei:
166166
FileFragment.from_config(
167167
{
168-
"regexp": r"wtf",
169168
"path": str(txt_path),
169+
"pattern": r"wtf",
170170
}
171171
)
172172

173173
assert ["file fragment: pattern 'wtf' not found."] == ei.value.errors
174174

175-
def test_regexp_no_group(self, txt_path):
175+
def test_pattern_no_group(self, txt_path):
176176
"""
177-
If the regexp matches but lacks a group, tell the user.
177+
If the pattern matches but lacks a group, tell the user.
178178
"""
179179
with pytest.raises(ConfigurationError) as ei:
180180
FileFragment.from_config(
181181
{
182-
"regexp": r"Uninteresting",
183182
"path": str(txt_path),
183+
"pattern": r"Uninteresting",
184184
}
185185
)
186186

187187
assert [
188188
"file fragment: pattern matches, but no group defined."
189189
] == ei.value.errors
190190

191-
def test_regexp_ok(self, txt_path):
191+
def test_pattern_ok(self, txt_path):
192192
"""
193-
If the regexp matches and has a group, return it.
193+
If the pattern matches and has a group, return it.
194194
"""
195195
assert (
196196
"*interesting*"
197197
== FileFragment.from_config(
198198
{
199-
"regexp": r"the (.*) body",
200199
"path": str(txt_path),
200+
"pattern": r"the (.*) body",
201201
}
202202
).render()
203203
)

0 commit comments

Comments
 (0)