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
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
1"""Formatter module."""
3from pathlib import Path
4from shutil import which
5from subprocess import run
7from sel_tools.utils.files import FileVisitor, is_cmake, is_cpp
10class FormatterVisitor(FileVisitor):
11 """Format file.
13 Auto-selects formatter depending on file suffix. Formats cpp and cmake,
14 ignores other file types
15 """
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)
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)
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)