Coverage for sel_tools/file_export/file_content_remover.py: 100%
18 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-03 19:37 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-03 19:37 +0000
1"""Remove block(s) within delimiters defined in config file from file or multilinestring."""
3import re
4from pathlib import Path
6from sel_tools.file_export.config import EXPORT_BEGIN, EXPORT_END
7from sel_tools.utils.files import FileVisitor, is_cpp
10class SolutionsRemoverVisitor(FileVisitor):
11 """Remove.
13 - block(s) within delimiters defined in config file from file
14 - clang tidy comments
15 """
17 def visit_file(self, file: Path) -> None:
18 text_with_reduced_content = remove_lines_within_limiters_from_string(file.read_text())
19 if is_cpp(file):
20 text_with_reduced_content = remove_clang_tidy_comment_lines(text_with_reduced_content)
21 file.write_text(text_with_reduced_content)
24def remove_lines_within_limiters_from_string(multiline_string: str) -> str:
25 """Remove block(s) within delimiters defined in config file from multiline string."""
26 minimal_length_between_markers_pattern = r"\n[\S ]*" + EXPORT_BEGIN + r".*?[\S ]*" + EXPORT_END
27 return re.sub(minimal_length_between_markers_pattern, "", multiline_string, flags=re.DOTALL)
30def remove_clang_tidy_comment_lines(multiline_string: str) -> str:
31 """Remove clang-tidy comments from multiline string."""
32 clang_tidy_nolint_end_of_line = " // NOLINT.*?\n"
33 multiline_string = re.sub(clang_tidy_nolint_end_of_line, "\n", multiline_string, flags=re.DOTALL)
35 clang_tidy_nolint_next_line = "// NOLINTNEXTLINE.*?\n"
36 return re.sub(clang_tidy_nolint_next_line, "", multiline_string, flags=re.DOTALL)