MCPcopy Index your code
hub / github.com/python/cpython / linear_format

Function linear_format

Tools/clinic/libclinic/formatting.py:225–270  ·  view source on GitHub ↗

Perform str.format-like substitution, except: * The strings substituted must be on lines by themselves. (This line is the "source line".) * If the substitution text is empty, the source line is removed in the output. * If the field is not recognized, the origi

(text: str, **kwargs: str)

Source from the content-addressed store, hash-verified

223
224
225def linear_format(text: str, **kwargs: str) -> str:
226 """
227 Perform str.format-like substitution, except:
228 * The strings substituted must be on lines by
229 themselves. (This line is the "source line".)
230 * If the substitution text is empty, the source line
231 is removed in the output.
232 * If the field is not recognized, the original line
233 is passed unmodified through to the output.
234 * If the substitution text is not empty:
235 * Each line of the substituted text is indented
236 by the indent of the source line.
237 * A newline will be added to the end.
238 """
239 lines = []
240 for line in text.split("\n"):
241 indent, curly, trailing = line.partition("{")
242 if not curly:
243 lines.extend([line, "\n"])
244 continue
245
246 name, curly, trailing = trailing.partition("}")
247 if not curly or name not in kwargs:
248 lines.extend([line, "\n"])
249 continue
250
251 if trailing:
252 raise ClinicError(
253 f"Text found after '{{{name}}}' block marker! "
254 "It must be on a line by itself."
255 )
256 if indent.strip():
257 raise ClinicError(
258 f"Non-whitespace characters found before '{{{name}}}' block marker! "
259 "It must be on a line by itself."
260 )
261
262 value = kwargs[name]
263 if not value:
264 continue
265
266 stripped = [line.rstrip() for line in value.split("\n")]
267 value = textwrap.indent("\n".join(stripped), indent)
268 lines.extend([value, "\n"])
269
270 return "".join(lines[:-1])

Callers

nothing calls this directly

Calls 8

ClinicErrorClass · 0.90
splitMethod · 0.45
partitionMethod · 0.45
extendMethod · 0.45
stripMethod · 0.45
rstripMethod · 0.45
indentMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…