MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _format_block

Function _format_block

tools/format_docs_code.py:58–129  ·  view source on GitHub ↗
(
    input_block: _Block,
    exit_on_error: bool,
    errors: list[tuple[int, str, Exception]],
    is_doctest: bool,
    file: str,
    is_python_file: bool,
)

Source from the content-addressed store, hash-verified

56
57
58def _format_block(
59 input_block: _Block,
60 exit_on_error: bool,
61 errors: list[tuple[int, str, Exception]],
62 is_doctest: bool,
63 file: str,
64 is_python_file: bool,
65) -> list[str]:
66 if not is_doctest:
67 # The first line may have additional padding. Remove then restore later
68 add_padding = start_space.match(input_block[0].code).groups()[0]
69 skip = len(add_padding)
70 code = "\n".join(
71 l.code[skip:] if l.code.startswith(add_padding) else l.code
72 for l in input_block
73 )
74 else:
75 add_padding = None
76 code = "\n".join(l.code for l in input_block)
77
78 mode = PYTHON_BLACK_MODE if is_python_file else RST_BLACK_MODE
79 custom_target = CUSTOM_TARGET_VERSIONS.get(Path(file).name)
80 if custom_target:
81 mode = dataclasses.replace(
82 mode, target_versions={TargetVersion[custom_target]}
83 )
84
85 try:
86 formatted = format_str(code, mode=mode)
87 except Exception as e:
88 start_line = input_block[0].line_no
89 first_error = not errors
90 if not REPORT_ONLY_DOCTEST or is_doctest:
91 type_ = "doctest" if is_doctest else "plain"
92 errors.append((start_line, code, e))
93 if first_error:
94 print() # add newline
95 print(
96 f"--- {file}:{start_line} Could not format {type_} code "
97 f"block:\n{code}\n---Error: {e}"
98 )
99 if exit_on_error:
100 print("Exiting since --exit-on-error was passed")
101 raise
102 else:
103 print("Ignoring error")
104 return [l.line for l in input_block]
105 else:
106 formatted_code_lines = formatted.splitlines()
107 padding = input_block[0].padding
108 sql_prefix = input_block[0].sql_marker or ""
109
110 if is_doctest:
111 formatted_lines = [
112 f"{padding}{sql_prefix}>>> {formatted_code_lines[0]}",
113 *(
114 f"{padding}...{' ' if fcl else ''}{fcl}"
115 for fcl in formatted_code_lines[1:]

Callers

nothing calls this directly

Calls 6

matchMethod · 0.45
joinMethod · 0.45
startswithMethod · 0.45
getMethod · 0.45
replaceMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected