Skip to content
Merged
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
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ repos:
types:
- python

- id: douki
name: douki
entry: douki sync
language: system
pass_filenames: true
require_serial: yes
files: "./"
types:
- python

- id: mypy
name: mypy
entry: mypy .
Expand Down
10 changes: 8 additions & 2 deletions libs/astx-transpilers/src/astx_transpilers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
"""ASTx Transpilers."""
"""
title: ASTx Transpilers.
"""

from importlib import metadata as importlib_metadata


def get_version() -> str:
"""Return the program version."""
"""
title: Return the program version.
returns:
type: str
"""
try:
return importlib_metadata.version(__name__)
except importlib_metadata.PackageNotFoundError: # pragma: no cover
Expand Down
32 changes: 22 additions & 10 deletions libs/astx-transpilers/src/astx_transpilers/python_string.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""ASTx to Python string transpiler (New Implementation)."""
"""
title: ASTx to Python string transpiler (New Implementation).
"""

import ast
import re
import sys

from typing import Union

import astx

from astx.tools.typing import typechecked
Expand All @@ -16,19 +16,31 @@
@typechecked
class ASTxPythonTranspiler:
"""
Transpiler that converts ASTx nodes to Python source code strings.

This transpiler uses the AST-based approach by first converting ASTx to
Python AST and then using ast.unparse() to generate the string
representation.
title: Transpiler that converts ASTx nodes to Python source code strings.
summary: |-

This transpiler uses the AST-based approach by first converting ASTx to
Python AST and then using ast.unparse() to generate the string
representation.
attributes:
indent_level:
type: int
indent_str:
type: str
_ast_transpiler:
type: ASTxPythonASTTranspiler
"""

indent_level: int
indent_str: str
_ast_transpiler: ASTxPythonASTTranspiler

def __init__(self) -> None:
self.indent_level = 0
self.indent_str = " "
self._ast_transpiler = ASTxPythonASTTranspiler()

def visit(self, node: Union[astx.AST, astx.ASTNodes]) -> str: # noqa: D102
def visit(self, node: astx.AST | astx.ASTNodes) -> str:
try:
python_ast = self._ast_transpiler.visit(node)
if sys.version_info >= (3, 9):
Expand Down Expand Up @@ -65,7 +77,7 @@ def visit(self, node: Union[astx.AST, astx.ASTNodes]) -> str: # noqa: D102
except Exception as e:
return f"# Error converting {type(node).__name__!s}: {e!s}"

def set_indent(self, level: int, indent_str: str = " ") -> None: # noqa: D102
def set_indent(self, level: int, indent_str: str = " ") -> None:
self.indent_level = level
self.indent_str = indent_str

Expand Down
Loading
Loading