Packages
metastatic
0.15.1
0.26.0
0.25.0
0.24.1
0.24.0
0.23.0
0.22.2
0.22.1
0.22.0
0.21.3
0.21.2
0.21.1
0.21.0
0.20.3
0.20.2
0.20.1
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.1
0.15.0
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Cross-language code meta-model library using unified MetaAST representation. Parse, transform, and translate code across Python, Elixir, Ruby, Erlang, Haskell, and more via a shared three-tuple AST format.
Current section
Files
Jump to
Current section
Files
lib/metastatic/supplemental/python/asyncio.ex
defmodule Metastatic.Supplemental.Python.Asyncio do
@moduledoc """
Supplemental module for Python's `asyncio` library.
Transforms async/await constructs into asyncio API calls. This supplemental
handles:
- `:async_await` - async/await patterns → asyncio.run()
- `:async_context` - async context managers → async with
- `:gather` - parallel execution → asyncio.gather()
## Examples
# Transform async function call
iex> ast = {:async_operation, :async_await,
...> {:function_call, "fetch_data", [
...> {:literal, :string, "https://api.example.com"}
...> ]}}
iex> {:ok, result} = Metastatic.Supplemental.Python.Asyncio.transform(ast, :python, %{})
iex> match?({:function_call, "asyncio.run", _}, result)
true
# Transform gather for parallel execution
iex> ast = {:async_operation, :gather, [
...> {:function_call, "fetch_user", [{:literal, :integer, 1}]},
...> {:function_call, "fetch_posts", [{:literal, :integer, 1}]}
...> ]}
iex> {:ok, result} = Metastatic.Supplemental.Python.Asyncio.transform(ast, :python, %{})
iex> match?({:function_call, "asyncio.gather", _}, result)
true
"""
@behaviour Metastatic.Supplemental
alias Metastatic.Supplemental.Info
@impl true
def info do
%Info{
name: :asyncio,
language: :python,
constructs: [:async_await, :async_context, :gather],
requires: ["asyncio"],
description: "Python asyncio library support for async/await patterns"
}
end
@impl true
def transform(ast, language, opts \\ %{})
def transform({:async_operation, :async_await, async_call}, :python, _opts) do
# Transform: async_await(call) → asyncio.run(call)
result = {:function_call, "asyncio.run", [async_call]}
{:ok, result}
end
def transform({:async_operation, :async_context, {resource, body}}, :python, _opts) do
# Transform: async_context(resource, body) → async with pattern
# Represented as language_specific since async with is Python-specific syntax
result =
{:language_specific, :python,
%{
type: :async_with,
resource: resource,
body: body
}}
{:ok, result}
end
def transform({:async_operation, :gather, tasks}, :python, _opts) when is_list(tasks) do
# Transform: gather([task1, task2, ...]) → asyncio.gather(task1, task2, ...)
result = {:function_call, "asyncio.gather", tasks}
{:ok, result}
end
def transform(ast, :python, _opts) do
{:error,
{:unsupported_construct, "Asyncio supplemental does not support construct: #{inspect(ast)}"}}
end
def transform(_ast, language, _opts) do
{:error,
{:incompatible_language, "Asyncio supplemental only supports Python, got: #{language}"}}
end
end