|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import argparse |
| 4 | +import ast |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +class PythonFormatter: |
| 10 | + |
| 11 | + def __init__(self, source_code: str): |
| 12 | + self.source_lines = source_code.splitlines() |
| 13 | + self.tree = ast.parse(source_code) |
| 14 | + self.node_parents = { |
| 15 | + child: parent for parent in ast.walk(self.tree) for child in ast.iter_child_nodes(parent) |
| 16 | + } |
| 17 | + self.disabled_ranges = self._find_disabled_ranges() |
| 18 | + |
| 19 | + |
| 20 | + def _find_disabled_ranges(self): |
| 21 | + ranges = [] |
| 22 | + in_disabled_block = False |
| 23 | + start_line = 0 |
| 24 | + for i, line in enumerate(self.source_lines): |
| 25 | + if "# fmt: off" in line: |
| 26 | + in_disabled_block = True |
| 27 | + start_line = i + 1 |
| 28 | + elif "# fmt: on" in line: |
| 29 | + if in_disabled_block: |
| 30 | + ranges.append((start_line, i + 1)) |
| 31 | + in_disabled_block = False |
| 32 | + return ranges |
| 33 | + |
| 34 | + |
| 35 | + def _is_in_disabled_range(self, lineno): |
| 36 | + for start, end in self.disabled_ranges: |
| 37 | + if start <= lineno <= end: |
| 38 | + return True |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | + def get_node_start_line(self, node): |
| 43 | + if node.decorator_list: |
| 44 | + return node.decorator_list[0].lineno |
| 45 | + return node.lineno |
| 46 | + |
| 47 | + |
| 48 | + def is_method(self, node) -> bool: |
| 49 | + return isinstance(self.node_parents.get(node), ast.ClassDef) |
| 50 | + |
| 51 | + |
| 52 | + def format(self) -> str: |
| 53 | + nodes = {} |
| 54 | + for node in ast.walk(self.tree): |
| 55 | + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): |
| 56 | + start_line = self.get_node_start_line(node) |
| 57 | + nodes[start_line] = node |
| 58 | + |
| 59 | + lines = list(self.source_lines) |
| 60 | + sorted_nodes = sorted(nodes.items(), key=lambda x: x[0], reverse=True) |
| 61 | + |
| 62 | + for lineno, node in sorted_nodes: |
| 63 | + start_index = lineno - 1 |
| 64 | + num_blank_lines = 0 |
| 65 | + |
| 66 | + # Skip formatting if node is inside a "fmt: off" block |
| 67 | + if self._is_in_disabled_range(lineno): |
| 68 | + continue |
| 69 | + |
| 70 | + if isinstance(node, ast.ClassDef): |
| 71 | + num_blank_lines = 2 |
| 72 | + elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
| 73 | + if self.is_method(node): |
| 74 | + if node.name == "__init__": |
| 75 | + num_blank_lines = 1 |
| 76 | + else: |
| 77 | + num_blank_lines = 2 |
| 78 | + else: |
| 79 | + num_blank_lines = 2 |
| 80 | + |
| 81 | + i = start_index - 1 |
| 82 | + while i > 0 and not lines[i].strip(): |
| 83 | + i -= 1 |
| 84 | + |
| 85 | + if i < 0: # start of file |
| 86 | + i = -1 # will insert at 0 |
| 87 | + |
| 88 | + # For top-level nodes, we don't want to add spaces if it's the first thing in the file |
| 89 | + # after imports. Let's check if there's anything but imports above. |
| 90 | + is_truly_top_level = i == -1 |
| 91 | + if not is_truly_top_level: |
| 92 | + # Count existing blank lines |
| 93 | + existing_blank_lines = 0 |
| 94 | + for k in range(start_index - 1, i, -1): |
| 95 | + if not lines[k].strip(): |
| 96 | + existing_blank_lines += 1 |
| 97 | + |
| 98 | + # Only add lines if there are not enough |
| 99 | + if existing_blank_lines < num_blank_lines: |
| 100 | + # remove existing blank lines |
| 101 | + del lines[i + 1 : start_index] |
| 102 | + # insert new blank lines |
| 103 | + for _ in range(num_blank_lines): |
| 104 | + lines.insert(i + 1, "") |
| 105 | + |
| 106 | + result = "\n".join(line.rstrip() for line in lines) |
| 107 | + if result: |
| 108 | + result = result.strip() + "\n" |
| 109 | + |
| 110 | + return result |
| 111 | + |
| 112 | + |
| 113 | +def main(): |
| 114 | + parser = argparse.ArgumentParser(description="Python custom formatter.") |
| 115 | + parser.add_argument("files", nargs="+", type=Path) |
| 116 | + args = parser.parse_args() |
| 117 | + |
| 118 | + for path in args.files: |
| 119 | + try: |
| 120 | + source = path.read_text() |
| 121 | + # Skip empty files |
| 122 | + if not source.strip(): |
| 123 | + continue |
| 124 | + formatter = PythonFormatter(source) |
| 125 | + formatted_source = formatter.format() |
| 126 | + path.write_text(formatted_source) |
| 127 | + print(f"Formatted {path}") |
| 128 | + except Exception as e: |
| 129 | + print(f"Could not format {path}: {e}", file=sys.stderr) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + main() |
0 commit comments