Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit 14a16cb

Browse files
committed
Changed the ignore functionality to regex
1 parent 12179a8 commit 14a16cb

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/python3_pip_skeleton/__main__.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pathlib import Path
66
from subprocess import STDOUT, CalledProcessError, call, check_output
77
from tempfile import TemporaryDirectory
8-
from typing import Dict, List
8+
from typing import Dict, List, Optional, Tuple
99

1010
from . import __version__
1111

@@ -17,14 +17,17 @@
1717
MERGE_BRANCH = "skeleton-merge-branch"
1818
# Extensions to change
1919
CHANGE_SUFFIXES = [".py", ".rst", ".cfg", "", ".toml"]
20-
# Files not to change where IGNORE_FILES[x] is a list containing line numbers
21-
# in the file x to be ignored. An empty list will ignore the whole file.
22-
IGNORE_FILES: Dict[str, List[int]] = {
20+
# Files not to change where IGNORE_FILES[x] is a list of tuples where substitutions
21+
# will be ignored in that file in any substring between the two strings.
22+
# An empty list will ignore the whole file.
23+
IGNORE_FILES: Dict[str, List[Optional[Tuple[str, str]]]] = {
2324
"update-tools.rst": [],
2425
"test_boilerplate_removed.py": [],
2526
"pin-requirements.rst": [],
2627
"0002-switched-to-pip-skeleton.rst:": [],
27-
"README.rst": [10],
28+
"README.rst": [
29+
("adopt this skeleton project see", "that describes what your module does")
30+
],
2831
}
2932

3033
SKELETON_ROOT_COMMIT = "ededf00035e6ccfac78946213009c1ecd7c110a9"
@@ -108,11 +111,29 @@ def replace_text(text: str) -> str:
108111
and IGNORE_FILES[child.name]
109112
):
110113
original_text = child.read_text()
111-
original_lines = original_text.splitlines()
112-
replaced_lines = replace_text(original_text).splitlines()
113-
for ignored_line in IGNORE_FILES[child.name]:
114-
replaced_lines[ignored_line - 1] = original_lines[ignored_line - 1]
115-
child.write_text("\n".join(replaced_lines))
114+
replaced_text = replace_text(original_text)
115+
for sub_strings in IGNORE_FILES[child.name]:
116+
assert isinstance(sub_strings, tuple)
117+
pre_sub_string, post_sub_string = sub_strings
118+
regex = rf"(?s){pre_sub_string}(.*?){post_sub_string}"
119+
# finditer so we can allow for different contents between ignore
120+
# substrings, e.g: `a foo b` and then later in the file `a bar b`
121+
original_substrings = list(re.finditer(regex, original_text))
122+
replaced_substrings = list(re.finditer(regex, replaced_text))
123+
assert len(original_substrings) == len(replaced_substrings), (
124+
"different number of ignored substrings between "
125+
f"{pre_sub_string} and {post_sub_string} found in "
126+
f"{child.name}, did you include replaced "
127+
"text inside the substrings?"
128+
)
129+
for original_substring, replaced_substring in zip(
130+
original_substrings,
131+
replaced_substrings,
132+
):
133+
replaced_text = replaced_text.replace(
134+
replaced_substring.group(0), original_substring.group(0)
135+
)
136+
child.write_text(replaced_text)
116137

117138
# Commit what we have and push to the original repo
118139
git_tmp("commit", "-a", "-m", f"Rename python3-pip-skeleton -> {repo}")

0 commit comments

Comments
 (0)