When processing Jupyter Notebooks, add the given magic to the list of known python-magics (capture, prun, pypy, python, python3, time, timeit). Useful for formatting cells with custom python magics.
@lazy(name="blacken_docs")defrun(*paths:str|Path,exts:Sequence[str]|None=None,exclude:Sequence[str|Pattern]|None=None,skip_errors:bool=False,rst_literal_blocks:bool=False,line_length:int|None=None,string_normalization:bool=True,is_pyi:bool=False,is_ipynb:bool=False,skip_source_first_line:bool=False,magic_trailing_comma:bool=True,python_cell_magics:set[str]|None=None,preview:bool=False,check_only:bool=False,)->int:"""Run `blacken-docs`. Parameters: *paths: Directories and files to format. exts: List of extensions to select files with. exclude: List of regular expressions to exclude files. skip_errors: Don't exit non-zero for errors from Black (normally syntax errors). rst_literal_blocks: Also format literal blocks in reStructuredText files (more below). line_length: How many characters per line to allow. string_normalization: Normalize string quotes or prefixes. is_pyi: Format all input files like typing stubs regardless of file extension. is_ipynb: Format all input files like Jupyter Notebooks regardless of file extension. skip_source_first_line: Skip the first line of the source code. magic_trailing_comma: Use trailing commas as a reason to split lines. python_cell_magics: When processing Jupyter Notebooks, add the given magic to the list of known python-magics (capture, prun, pypy, python, python3, time, timeit). Useful for formatting cells with custom python magics. preview: Enable potentially disruptive style changes that may be added to Black's main functionality in the next major release. check_only: Don't modify files but indicate when changes are necessary with a message and non-zero return code. Returns: Success/failure. """importblackfromblacken_docsimportformat_fileexts=("md","py")ifextsisNoneelsetuple(ext.lstrip(".")forextinexts)ifexclude:exclude=tuple(re.compile(regex,re.I)ifisinstance(regex,str)elseregexforregexinexclude)filepaths=set()forpathinpaths:path=Path(path)# noqa: PLW2901ifpath.is_file():filepaths.add(path.as_posix())else:forextinexts:filepaths|={filepath.as_posix()forfilepathinpath.rglob(f"*.{ext}")}black_mode=black.Mode(line_length=line_lengthorblack.DEFAULT_LINE_LENGTH,string_normalization=string_normalization,is_pyi=is_pyi,is_ipynb=is_ipynb,skip_source_first_line=skip_source_first_line,magic_trailing_comma=magic_trailing_comma,python_cell_magics=python_cell_magicsorset(),preview=preview,)retv=0forfilepathinsorted(filepaths):retv|=format_file(filepath,black_mode,skip_errors=skip_errors,rst_literal_blocks=rst_literal_blocks,check_only=check_only,)returnretv