Skip to main content

AST-level operations for Python code - CLI tool

Project description

ast-workers-py

中文文档

Python AST manipulation CLI tool. Edit Python code at the Abstract Syntax Tree level with guaranteed syntax correctness.

Installation

pip install ast-workers-py

Usage

ast-py <operation> [options]

Operations

Command Alias Description
insert-function if Insert a function
update-function uf Update a function
insert-class ic Insert a class
insert-class-variable icv Insert a class variable
insert-slots is Insert __slots__ into a class
insert-dunder-all iall Insert or update __all__
insert-import ii Insert an import statement
list-functions lf List functions in a module/class
list-classes lc List classes in a module
list-imports li List imports in a module
find-symbol fs Find a symbol in a module
delete-function df Delete a function
delete-class dc Delete a class
rename-symbol rn Rename a symbol
show s Show symbol with context
validate v Validate Python syntax
format fmt Format Python code
batch b Execute multiple operations from JSON

Examples

Query Operations

# List functions in a module
ast-py list-functions -m src/auth.py

# List methods in a class
ast-py list-functions -m src/auth.py -c AuthService

# List classes in a module
ast-py list-classes -m src/models.py

# Show a symbol with context (supports scoped naming)
ast-py show -m src/auth.py -n AuthService.login

# Find a symbol
ast-py find-symbol -m src/auth.py -n validate_token -t function

Insert Operations

# Insert a simple function
ast-py insert-function -m src/auth.py -n validate_token -p "token:str" -r bool -b "return len(token) > 10"

# Insert a method into a class
ast-py insert-function -m src/auth.py -c AuthService -n check_permissions -p "user:User, action:str" -r bool

# Insert with advanced parameters
ast-py insert-function -m src/utils.py -n process -p "data, /, strict:bool=True, *, encoding:str='utf-8', **kwargs"

# Insert with decorators
ast-py insert-function -m src/api.py -n get_user -p "user_id:int" -d "@route('/users/<int:user_id>'), @login_required"

# Insert async function
ast-py insert-function -m src/async_ops.py -n fetch_data --is-async -p "url:str" -r "dict"

# Insert with docstring
ast-py insert-function -m src/auth.py -n login -p "user:str, password:str" --docstring "Authenticate user and return session"

# Insert a class
ast-py insert-class -m src/models.py -n User -b "BaseModel" --docstring "User model" --class-vars "id:int, name:str"

# Insert a class variable
ast-py insert-class-variable -m src/models.py -c User -n count -t int -v 0

# Insert __slots__
ast-py insert-slots -m src/models.py -c User --slots id name email

# Insert __all__
ast-py insert-dunder-all -m src/auth.py --names login logout authenticate

# Insert an import
ast-py insert-import -m src/auth.py --from typing --name "Optional, Dict"

Update Operations

# Update function body
ast-py update-function -m src/auth.py -n validate_token -b "return token is not None"

# Replace entire parameter list
ast-py update-function -m src/auth.py -n login -p "self, user_id:int, timeout:int=30"

# Add parameters
ast-py update-function -m src/auth.py -n login --add-params "remember:bool=False"

# Remove parameters
ast-py update-function -m src/auth.py -n login --remove-params deprecated_param

# Change return type
ast-py update-function -m src/auth.py -n get_user --return-type "Optional[User]"

# Add decorators
ast-py update-function -m src/api.py -n endpoint --add-decorators "@cache(3600)"

# Remove decorators
ast-py update-function -m src/api.py -n endpoint --remove-decorators old_decorator

# Update docstring
ast-py update-function -m src/auth.py -n login --docstring "Authenticate user"

Delete Operations

# Delete a function
ast-py delete-function -m src/auth.py -n old_function

# Delete a class
ast-py delete-class -m src/models.py -n DeprecatedModel

Rename Operations

# Rename a function
ast-py rename-symbol -m src/auth.py -o old_name -n new_name -t function

# Rename a class
ast-py rename-symbol -m src/models.py -o OldUser -n User -t class

Utility Operations

# Validate syntax
ast-py validate -m src/auth.py

# Format code
ast-py format -m src/auth.py

# Batch operations from JSON
ast-py batch -m src/auth.py --json ops.json

Parameter Syntax

The -p/--params argument supports full Python parameter syntax:

positional, /, pos_or_kw, *, kw_only, **kwargs

Examples:

  • Basic: -p "x, y, z"
  • With types: -p "x:int, y:str, z:float"
  • With defaults: -p "x:int=0, y:str='hello'"
  • Positional-only: -p "a, b, /, c, d" (a, b are positional-only)
  • Keyword-only: -p "*, c, d=1" (c, d must be keyword args)
  • *args/**kwargs: -p "*args:int, **kwargs:dict"

Structured Body Format

For multi-line code with proper indentation, use JSON arrays:

# Nested lists represent indentation levels
ast-py insert-function -m src/auth.py -n process -p "data:dict" -b '["if data:", ["result = process(data)", "return result"], "return None"]'

Result:

def process(data: dict):
    if data:
        result = process(data)
        return result
    return None

Scoped Naming

Use dot notation for nested symbols:

# Class method
ast-py show -m src/auth.py -n AuthService.login

# Nested class
ast-py show -m src/models.py -n OuterClass.InnerClass

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast_workers_py-0.1.7.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ast_workers_py-0.1.7-py3-none-any.whl (43.4 kB view details)

Uploaded Python 3

File details

Details for the file ast_workers_py-0.1.7.tar.gz.

File metadata

  • Download URL: ast_workers_py-0.1.7.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ast_workers_py-0.1.7.tar.gz
Algorithm Hash digest
SHA256 d115a5dc4015c4f1013f47f0d2c9c10cb95df2d56467296c79964255b97a7e19
MD5 3e502bb0f9da34f348ce522b44224fe5
BLAKE2b-256 0dd7aff73fb3dd575d3e345ef4bf91b8bb60a71e34301a663f028b986ae6747c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ast_workers_py-0.1.7.tar.gz:

Publisher: publish.yml on nostalgiatan/AST-workers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ast_workers_py-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: ast_workers_py-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 43.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ast_workers_py-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d9af27cdc7066dbea671dc748b49826beaaedb5a733462ce585d6b86ff1593f8
MD5 93f480c4f859cc990b17307075dbb82f
BLAKE2b-256 f3dc42afc076d645d1bd61bca6577c844b4d09fa016f2d8e311133c852e1814c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ast_workers_py-0.1.7-py3-none-any.whl:

Publisher: publish.yml on nostalgiatan/AST-workers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page