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

Commit 7785906

Browse files
authored
Merge pull request #77 from DiamondLightSource/incorrect_organization_replacement_in_general_README
Changed the ignore functionality so individual lines in files can be ignored
2 parents 67dfa74 + de784b4 commit 7785906

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

src/python3_pip_skeleton/__main__.py

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

1010
import tomli
1111

@@ -20,12 +20,18 @@
2020
MERGE_BRANCH = "skeleton-merge-branch"
2121
# Extensions to change
2222
CHANGE_SUFFIXES = [".py", ".rst", ".cfg", "", ".toml"]
23-
# Files not to change
24-
IGNORE_FILES = [
25-
"test_boilerplate_removed.py",
26-
"pin-requirements.rst",
27-
"update-tools.rst",
28-
]
23+
# Files not to change where IGNORE_FILES[x] is a list of tuples where substitutions
24+
# will be ignored in that file in any substring between the two strings.
25+
# An empty list will ignore the whole file.
26+
IGNORE_FILES: Dict[str, List[Tuple[str, str]]] = {
27+
"update-tools.rst": [],
28+
"test_boilerplate_removed.py": [],
29+
"pin-requirements.rst": [],
30+
"0002-switched-to-pip-skeleton.rst:": [],
31+
"README.rst": [
32+
("adopt this skeleton project see", "that describes what your module does")
33+
],
34+
}
2935

3036
SKELETON_ROOT_COMMIT = "ededf00035e6ccfac78946213009c1ecd7c110a9"
3137

@@ -54,6 +60,48 @@ def __truediv__(self, other) -> Path:
5460
return Path(self.name) / other
5561

5662

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+
57105
def merge_skeleton(
58106
path: Path,
59107
org: str,
@@ -106,6 +154,21 @@ def replace_in_file(file_path: Path, text_from: str, text_to: str):
106154
if child.suffix in CHANGE_SUFFIXES and child.name not in IGNORE_FILES:
107155
text = replace_text(child.read_text())
108156
child.write_text(text)
157+
# Replace the file, ignoring text between specified substrings
158+
elif (
159+
child.suffix in CHANGE_SUFFIXES
160+
and child.name in IGNORE_FILES
161+
and IGNORE_FILES[child.name]
162+
):
163+
original_text = child.read_text()
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
170+
)
171+
)
109172

110173
# Change instructions in the docs to reflect which pip skeleton is in use
111174
replace_in_file(

0 commit comments

Comments
 (0)