snippet_fmt¶
Format and validate code snippets in reStructuredText files.
Classes:
|
Represents an exception raised when parsing and reformatting a code block. |
|
Reformat code snippets in a docstring from a Python file. |
|
Reformat code snippets in docstrings in a Python file. |
|
Reformat code snippets in a reStructuredText file. |
|
Base class for reformatters. |
Functions:
|
Reformat docstrings in the given Python file, and show the diff if changes were made. |
|
Reformat the given reStructuredText file, and show the diff if changes were made. |
-
namedtuple
CodeBlockError(offset, exc)[source]¶ Bases:
NamedTupleRepresents an exception raised when parsing and reformatting a code block.
- Fields
-
__repr__()¶ Return a nicely formatted representation string
-
class
Reformatter(source, filename, config)[source]¶ Bases:
objectBase class for reformatters.
- Parameters
source (
str) – The file content.filename (
str) – The file being formatted, for display in error messages.config (
SnippetFmtConfigDict) – Thesnippet_fmtconfiguration, parsed from a TOML file (or similar).
New in version 0.2.0.
Methods:
Compile the regular expression for finding directives.
get_diff()Returns the diff between the original and reformatted file content.
Load custom formatters defined via entry points.
process_match(match)Process a
re.Matchfor a single code block.report_error(error)Print the error message.
run()Run the reformatter.
Return the reformatted file as a string.
Attributes:
The
formateconfiguration, parsed from a TOML file (or similar).The filename being reformatted, as a POSIX-style path.
-
compile_regex()[source]¶ Compile the regular expression for finding directives.
New in version 0.2.0.
- Return type
-
config¶ Type:
SnippetFmtConfigDictThe
formateconfiguration, parsed from a TOML file (or similar).
-
report_error(error)[source]¶ Print the error message.
- Parameters
error (
CodeBlockError)
New in version 0.2.0.
-
class
RSTReformatter(filename, config)[source]¶ Bases:
ReformatterReformat code snippets in a reStructuredText file.
- Parameters
filename (
Union[str,Path,PathLike]) – The filename to reformat.config (
SnippetFmtConfigDict) – Thesnippet_fmtconfiguration, parsed from a TOML file (or similar).
Attributes:
The filename being reformatted.
Methods:
to_file()Write the reformatted source to the original file.
-
class
DocstringReformatter(token, filename, config)[source]¶ Bases:
ReformatterReformat code snippets in a docstring from a Python file.
- Parameters
token (
Token) – The docstring token to format.filename (
Union[str,Path,PathLike]) – The filename being reformated.config (
SnippetFmtConfigDict) – Thesnippet_fmtconfiguration, parsed from a TOML file (or similar).
New in version 0.2.0.
Methods:
get_diff()Returns the diff between the original and reformatted file content.
report_error(error)Print the error message.
run()Run the reformatter.
Return the reformatted file as a string.
to_token()Return the docstring as a token for
tokenize_rt.Attributes:
The docstring’s indentation.
Letters before the string e.g.
Quotes used for the docstring, e.g.
The docstring token being reformatted.
-
report_error(error)[source]¶ Print the error message.
- Parameters
error (
CodeBlockError)
-
token¶ Type:
TokenThe docstring token being reformatted.
-
class
PyReformatter(filename, config)[source]¶ Bases:
RSTReformatterReformat code snippets in docstrings in a Python file.
- Parameters
filename (
Union[str,Path,PathLike]) – The filename to reformat.config (
SnippetFmtConfigDict) – Thesnippet_fmtconfiguration, parsed from a TOML file (or similar).
New in version 0.2.0.
-
reformat_file(filename, config, colour=None)[source]¶ Reformat the given reStructuredText file, and show the diff if changes were made.