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

16 statements  

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

1"""Formatter module.""" 

2 

3from pathlib import Path 

4from shutil import which 

5from subprocess import run 

6 

7from sel_tools.utils.files import FileVisitor, is_cmake, is_cpp 

8 

9 

10class FormatterVisitor(FileVisitor): 

11 """Format file. 

12 

13 Auto-selects formatter depending on file suffix. Formats cpp and cmake, 

14 ignores other file types 

15 """ 

16 

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

18 if is_cpp(file): 

19 apply_clang_format(file) 

20 elif is_cmake(file): 

21 apply_cmake_format(file) 

22 

23 

24def apply_clang_format(file: Path) -> None: 

25 """Apply clang-format with default config in place to file.""" 

26 if which("clang-format"): 

27 run(f"clang-format -i {file}", shell=True, check=True) 

28 

29 

30def apply_cmake_format(file: Path) -> None: 

31 """Apply cmake-format with default config in place to file.""" 

32 if which("cmake-format"): 

33 run(f"cmake-format -i {file}", shell=True, check=True)