Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable writing module code directly to file #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions libcst/_nodes/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# LICENSE file in the root directory of this source tree.


import io
from contextlib import contextmanager
from dataclasses import dataclass, field
from typing import Iterable, Iterator, List, Optional, Sequence, TYPE_CHECKING, Union
Expand Down Expand Up @@ -70,6 +71,30 @@ def record_syntactic_position(
yield


@add_slots
@dataclass(frozen=False)
class CodegenWriter(CodegenState):
"""
A CodegenState that writes to a file-like object.
"""

writer: io.TextIOBase = None # need a default value for dataclass

def __post_init__(self) -> None:
if self.writer is None:
raise TypeError("writer must be provided")

def add_indent_tokens(self) -> None:
for token in self.indent_tokens:
self.writer.write(token)

def add_token(self, value: str) -> None:
self.writer.write(value)

def pop_trailing_newline(self) -> None:
pass


def visit_required(
parent: "CSTNode", fieldname: str, node: CSTNodeT, visitor: "CSTVisitorT"
) -> CSTNodeT:
Expand Down
26 changes: 25 additions & 1 deletion libcst/_nodes/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import io
from dataclasses import dataclass
from typing import cast, Optional, Sequence, TYPE_CHECKING, TypeVar, Union

from libcst._add_slots import add_slots
from libcst._nodes.base import CSTNode
from libcst._nodes.internal import CodegenState, visit_body_sequence, visit_sequence
from libcst._nodes.internal import (
CodegenState,
CodegenWriter,
visit_body_sequence,
visit_sequence,
)
from libcst._nodes.statement import (
BaseCompoundStatement,
get_docstring_impl,
Expand Down Expand Up @@ -136,6 +142,24 @@ def code_for_node(self, node: CSTNode) -> str:
node._codegen(state)
return "".join(state.tokens)

def write_code(self, writer: io.TextIOBase):
"""
Like :meth:`code`, but writes the code to the given file-like object.
"""
self.write_code_for_node(self, writer)

def write_code_for_node(self, node: CSTNode, writer: io.TextIOBase):
"""
Like :meth:`code_for_node`, but writes the code to the given file-like object.
"""

state = CodegenWriter(
default_indent=self.default_indent,
default_newline=self.default_newline,
writer=writer,
)
node._codegen(state)

@property
def config_for_parsing(self) -> "PartialParserConfig":
"""
Expand Down