The Python AST library is used by Blueprint engine plugins. Node.js plugins use different methods for code generation as detailed in the Node.js Plugin Events documentation.
The Python AST library provides functionality for generating Python code through an abstract syntax tree in the Amplication platform. It enables the creation of type-safe, well-structured Python code.
The Python AST library is not intended to cover all Python language functionality. Instead, it focuses on the elements needed to create foundation and boilerplate code with Amplication plugins. The library provides building blocks for generating well-structured Python code for common patterns and use cases.When more specialized or custom code is needed, the CodeBlock class can be used as a generic node that can include any code as a string:
Copy
import { CodeBlock } from '@amplication/python-ast';// Create a custom code block for specialized logicconst customLogic = new CodeBlock({ code: `try: result = process_data() return resultexcept ValueError as e: logger.error(f"Invalid data: {e}") raisefinally: cleanup_resources() `, references: [ new ClassReference({ name: 'ValueError' }), new ClassReference({ name: 'logger', moduleName: 'logging' }) ]});
This flexibility allows you to generate both structured AST-based code and custom code blocks when necessary.