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
EntryPointobjects.- Parameters
- Raises
HookNotFoundErrorif 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
SyntaxErrorand set its filename tofilenameif 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:
NodeVisitorABC for rewriting Python source files from an AST and a token stream.
- Parameters
source (
str) – The original source.
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:
ASTTokensThe 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:
TracebackHandlerSubclass of
consolekit.tracebacks.TracebackHandlerto additionally handleSyntaxError.