MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / format_file

Function format_file

tools/format_docs_code.py:149–324  ·  view source on GitHub ↗
(
    file: Path, exit_on_error: bool, check: bool
)

Source from the content-addressed store, hash-verified

147
148
149def format_file(
150 file: Path, exit_on_error: bool, check: bool
151) -> tuple[bool, int]:
152 buffer = []
153 if not check:
154 print(f"Running file {file} ..", end="")
155 original = file.read_text("utf-8")
156 doctest_block: _Block | None = None
157 plain_block: _Block | None = None
158
159 is_python_file = file.suffix == ".py"
160
161 plain_code_section = False
162 plain_padding = None
163 plain_padding_len = None
164 sql_section = False
165
166 errors = []
167
168 do_doctest_format = partial(
169 _format_block,
170 exit_on_error=exit_on_error,
171 errors=errors,
172 is_doctest=True,
173 file=str(file),
174 is_python_file=is_python_file,
175 )
176
177 def doctest_format():
178 nonlocal doctest_block
179 if doctest_block:
180 buffer.extend(do_doctest_format(doctest_block))
181 doctest_block = None
182
183 do_plain_format = partial(
184 _format_block,
185 exit_on_error=exit_on_error,
186 errors=errors,
187 is_doctest=False,
188 file=str(file),
189 is_python_file=is_python_file,
190 )
191
192 def plain_format():
193 nonlocal plain_block
194 if plain_block:
195 buffer.extend(do_plain_format(plain_block))
196 plain_block = None
197
198 disable_format = False
199 for line_no, line in enumerate(original.splitlines(), 1):
200 if (
201 line
202 and not disable_format
203 and start_code_section.match(line.strip())
204 ):
205 # start_code_section regexp requires no spaces at the start
206 plain_format()

Callers 1

mainFunction · 0.85

Calls 9

plain_formatFunction · 0.85
BlockLineClass · 0.85
doctest_formatFunction · 0.85
stripMethod · 0.80
matchMethod · 0.45
appendMethod · 0.45
startswithMethod · 0.45
replaceMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected