Coverage for sel_tools/file_export/file_content_remover.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-04 21:22 +0000

1"""Remove block(s) within delimiters defined in config file from file or multiline 

2string.""" 

3 

4import re 

5from pathlib import Path 

6 

7from sel_tools.file_export.config import EXPORT_BEGIN, EXPORT_END 

8from sel_tools.utils.files import FileVisitor, is_cpp 

9 

10 

11class SolutionsRemoverVisitor(FileVisitor): 

12 """Remove. 

13 

14 - block(s) within delimiters defined in config file from file 

15 - clang tidy comments 

16 """ 

17 

18 def visit_file(self, file: Path) -> None: 

19 text_with_reduced_content = remove_lines_within_limiters_from_string(file.read_text()) 

20 if is_cpp(file): 

21 text_with_reduced_content = remove_clang_tidy_comment_lines(text_with_reduced_content) 

22 file.write_text(text_with_reduced_content) 

23 

24 

25def remove_lines_within_limiters_from_string(multiline_string: str) -> str: 

26 """Remove block(s) within delimiters defined in config file from multiline 

27 string.""" 

28 minimal_length_between_markers_pattern = r"\n[\S ]*" + EXPORT_BEGIN + r".*?[\S ]*" + EXPORT_END 

29 return re.sub(minimal_length_between_markers_pattern, "", multiline_string, flags=re.DOTALL) 

30 

31 

32def remove_clang_tidy_comment_lines(multiline_string: str) -> str: 

33 """Remove clang-tidy comments from multiline string.""" 

34 clang_tidy_nolint_end_of_line = " // NOLINT.*?\n" 

35 multiline_string = re.sub(clang_tidy_nolint_end_of_line, "\n", multiline_string, flags=re.DOTALL) 

36 

37 clang_tidy_nolint_next_line = "// NOLINTNEXTLINE.*?\n" 

38 return re.sub(clang_tidy_nolint_next_line, "", multiline_string, flags=re.DOTALL)