Utilities

modify_repos.utils.run_cmd(*args, **kwargs)

Wrapper around subprocess.run(). Args are passed positionally rather than as a list. Stdout and stderr are combined, and use text mode. The initial command is echoed, and if the return code is not 0, the output and code are echoed.

Parameters:
  • args (str | PathLike[str]) – Command line arguments.

  • kwargs (Any) – Other arguments passed to subprocess.run.

Return type:

CompletedProcess[str]

modify_repos.utils.wrap_text(text, width=80)

Wrap a multi-line, multi-paragraph string. The text is dedented and empty spaces and lines are stripped, to support triple-quoted strings. Paragraphs are separated by a blank line \n\n. Tabs are converted to 4 spaces, and very long words are not wrapped.

Parameters:
  • text (str) – The text to process.

  • width (int) – The number of characters to wrap at.

Return type:

str

modify_repos.utils.read_text(path, strip=True)

Read a file as UTF-8 text.

Parameters:
  • path (str | PathLike[str]) – The path to the file to read.

  • strip (bool) – Whether to strip empty spaces and lines. Enabled by default. This makes the text more predictable to work with when inserting it into another text.

Return type:

str

modify_repos.utils.write_text(path, text, end_nl=True)

Write a file as UTF-8 text.

Parameters:
  • path (str | PathLike[str]) – The path to the file to write.

  • text (str) – The text to write.

  • end_nl (bool) – Whether to ensure the text ends with exactly one newline \n. This keeps file endings consistent.

Return type:

None