99
1010
1111def format_python_code (
12- code : str , black_args : List [str ]
12+ code : str , ruff_args : List [str ]
1313) -> str : # numpydoc ignore=RT01
14- """Format Python code using Black with custom arguments."""
14+ """Format Python code using Ruff with custom arguments."""
1515 try :
16- cmd = ["black " , "-" ] + black_args
16+ cmd = ["ruff " , "format" , " -" ] + ruff_args
1717 result = subprocess .run (
1818 cmd ,
1919 input = code ,
@@ -24,28 +24,28 @@ def format_python_code(
2424 return result .stdout
2525 except subprocess .CalledProcessError :
2626 print (
27- "Error: Failed to format Python code with Black ." , file = sys .stderr
27+ "Error: Failed to format Python code with Ruff ." , file = sys .stderr
2828 )
2929 return code
3030
3131
3232def replace_code_block (
33- match : Match [str ], black_args : List [str ]
33+ match : Match [str ], ruff_args : List [str ]
3434) -> str : # numpydoc ignore=RT01
3535 """Replace code block with formatted version."""
36- return f"{ match .group (1 )} \n { format_python_code (match .group (2 ), black_args )} { match .group (3 )} "
36+ return f"{ match .group (1 )} \n { format_python_code (match .group (2 ), ruff_args )} { match .group (3 )} "
3737
3838
3939def process_file (
40- filepath : Path , black_args : List [str ]
40+ filepath : Path , ruff_args : List [str ]
4141) -> None : # numpydoc ignore=RT01
4242 """Process the given file, formatting Python code blocks."""
4343 python_code_block_pattern = r"(```\{python\})(.*?)(```)"
4444 try :
4545 content = filepath .read_text ()
4646 formatted_content = re .sub (
4747 python_code_block_pattern ,
48- lambda m : replace_code_block (m , black_args ),
48+ lambda m : replace_code_block (m , ruff_args ),
4949 content ,
5050 flags = re .DOTALL ,
5151 )
@@ -63,11 +63,11 @@ def process_file(
6363if __name__ == "__main__" :
6464 if len (sys .argv ) < 3 :
6565 print (
66- 'Usage: python hook_scripts/quarto_python_formatter.py "BLACK_ARGS " <filename1.qmd> [filename2.qmd ...]'
66+ 'Usage: python hook_scripts/quarto_python_formatter.py "RUFF_ARGS " <filename1.qmd> [filename2.qmd ...]'
6767 )
6868 sys .exit (1 )
6969
70- black_args = sys .argv [1 ].split ()
70+ ruff_args = sys .argv [1 ].split ()
7171
7272 missing_files = [file for file in sys .argv [2 :] if not Path (file ).exists ()]
7373 if missing_files :
@@ -76,4 +76,4 @@ def process_file(
7676 )
7777 for filepath in sys .argv [2 :]:
7878 path = Path (filepath )
79- process_file (path , black_args )
79+ process_file (path , ruff_args )
0 commit comments