(
filename: str,
skip_errors: bool,
format_code_block: Callable[[str], str],
)
| 153 | |
| 154 | |
| 155 | def format_file( |
| 156 | filename: str, |
| 157 | skip_errors: bool, |
| 158 | format_code_block: Callable[[str], str], |
| 159 | ) -> int: |
| 160 | with open(filename, encoding="UTF-8") as f: |
| 161 | contents = f.read() |
| 162 | new_contents, errors = format_str(contents, format_code_block) |
| 163 | for error in errors: |
| 164 | lineno = contents[: error.offset].count("\n") + 1 |
| 165 | print(f"{filename}:{lineno}: code block parse error {error.exc}") |
| 166 | if errors and not skip_errors: |
| 167 | return 1 |
| 168 | if contents != new_contents: |
| 169 | print(f"{filename}: Rewriting...") |
| 170 | with open(filename, "w", encoding="UTF-8") as f: |
| 171 | f.write(new_contents) |
| 172 | return 0 |
| 173 | else: |
| 174 | return 0 |
| 175 | |
| 176 | |
| 177 | def main(argv: Sequence[str] | None = None) -> int: |
no test coverage detected