formate.utils
¶
Utility functions.
Functions:
|
Given a list of hooks, import the corresponding entry point and return a mapping of entry point names to |
|
Normalize the given name into lowercase, with underscores replaced by hyphens. |
|
Context manager to catch |
Classes:
|
ABC for rewriting Python source files from an AST and a token stream. |
|
Subclass of |
-
import_entry_points
(hooks)[source]¶ Given a list of hooks, import the corresponding entry point and return a mapping of entry point names to
EntryPoint
objects.- Parameters
- Raises
HookNotFoundError
if no entry point can be found for a hook.- Return type
-
normalize
(name)[source]¶ Normalize the given name into lowercase, with underscores replaced by hyphens.
-
syntaxerror_for_file
(filename)[source]¶ Context manager to catch
SyntaxError
and set its filename tofilename
if the current filename is<unknown>
.This is useful for syntax errors raised when parsing source into an AST.
- Return type
-
class
Rewriter
(source)[source]¶ Bases:
NodeVisitor
ABC for rewriting Python source files from an AST and a token stream.
Attributes:
The original source.
The tokenized source.
The parts of code to replace.
Methods:
rewrite
()Rewrite the source and return the new source.
record_replacement
(text_range, new_source)Record a region of text to be replaced.
-
tokens
¶ Type:
ASTTokens
The tokenized source.
-
replacements
¶ Type:
List
[Tuple
[Tuple
[int
,int
],str
]]The parts of code to replace.
Each element comprises a tuple of
(start char, end char)
insource
, and the new text to insert between these positions.
-
-
class
SyntaxTracebackHandler
(exception=Abort())[source]¶ Bases:
TracebackHandler
Subclass of
consolekit.tracebacks.TracebackHandler
to additionally handleSyntaxError
.