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

Commit de784b4

Browse files
committed
Changed the ignore funcitonality so replace_text isn't ran on strings we'll later change back
1 parent 09b4571 commit de784b4

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

src/python3_pip_skeleton/__main__.py

Lines changed: 51 additions & 24 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, Optional, Tuple
8+
from typing import Callable, Dict, List, Tuple
99

1010
import tomli
1111

@@ -23,7 +23,7 @@
2323
# Files not to change where IGNORE_FILES[x] is a list of tuples where substitutions
2424
# will be ignored in that file in any substring between the two strings.
2525
# An empty list will ignore the whole file.
26-
IGNORE_FILES: Dict[str, List[Optional[Tuple[str, str]]]] = {
26+
IGNORE_FILES: Dict[str, List[Tuple[str, str]]] = {
2727
"update-tools.rst": [],
2828
"test_boilerplate_removed.py": [],
2929
"pin-requirements.rst": [],
@@ -60,6 +60,48 @@ def __truediv__(self, other) -> Path:
6060
return Path(self.name) / other
6161

6262

63+
def find_ignore_sections(
64+
file_name: str, file_text: str, ignore_sections: List[Tuple[str, str]]
65+
) -> List[re.Match]:
66+
ignore_section_matches = []
67+
for sub_strings in ignore_sections:
68+
pre_sub_string, post_sub_string = sub_strings
69+
regex = rf"(?s){pre_sub_string}(.*?){post_sub_string}"
70+
# finditer so we can throw an error if the used ignore strings ignore
71+
# more than once in the file
72+
original_substrings = list(re.finditer(regex, file_text))
73+
assert original_substrings, (
74+
f"could not find substrings {pre_sub_string} or "
75+
f"{post_sub_string} in {file_name}."
76+
)
77+
assert len(original_substrings) == 1, (
78+
f"multiple substrings found between {pre_sub_string} and "
79+
f"{post_sub_string} in {file_name}."
80+
)
81+
ignore_section_matches.append(original_substrings[0])
82+
83+
ignore_section_matches.sort(key=lambda x: x.start())
84+
return ignore_section_matches
85+
86+
87+
def replace_text_ignoring_sections(
88+
text: str,
89+
ignore_section_matches: List[re.Match],
90+
text_replacement_method: Callable,
91+
) -> str:
92+
replacement_text = ""
93+
next_start = 0
94+
for ignore_section in ignore_section_matches:
95+
replacement_text += text_replacement_method(
96+
text[next_start : ignore_section.start()]
97+
)
98+
replacement_text += text[ignore_section.start() : ignore_section.end()]
99+
next_start = ignore_section.end()
100+
101+
replacement_text += text_replacement_method(text[next_start : len(text)])
102+
return replacement_text
103+
104+
63105
def merge_skeleton(
64106
path: Path,
65107
org: str,
@@ -119,29 +161,14 @@ def replace_in_file(file_path: Path, text_from: str, text_to: str):
119161
and IGNORE_FILES[child.name]
120162
):
121163
original_text = child.read_text()
122-
replaced_text = replace_text(original_text)
123-
for sub_strings in IGNORE_FILES[child.name]:
124-
assert isinstance(sub_strings, tuple)
125-
pre_sub_string, post_sub_string = sub_strings
126-
regex = rf"(?s){pre_sub_string}(.*?){post_sub_string}"
127-
# finditer so we can allow for different contents between ignore
128-
# substrings, e.g: `a foo b` and then later in the file `a bar b`
129-
original_substrings = list(re.finditer(regex, original_text))
130-
replaced_substrings = list(re.finditer(regex, replaced_text))
131-
assert len(original_substrings) == len(replaced_substrings), (
132-
"different number of ignored substrings between "
133-
f"{pre_sub_string} and {post_sub_string} found in "
134-
f"{child.name}, did you include replaced "
135-
"text inside the substrings?"
164+
ignore_sections = find_ignore_sections(
165+
child.name, original_text, IGNORE_FILES[child.name]
166+
)
167+
child.write_text(
168+
replace_text_ignoring_sections(
169+
original_text, ignore_sections, replace_text
136170
)
137-
for original_substring, replaced_substring in zip(
138-
original_substrings,
139-
replaced_substrings,
140-
):
141-
replaced_text = replaced_text.replace(
142-
replaced_substring.group(0), original_substring.group(0)
143-
)
144-
child.write_text(replaced_text)
171+
)
145172

146173
# Change instructions in the docs to reflect which pip skeleton is in use
147174
replace_in_file(

0 commit comments

Comments
 (0)