Formatters
formatters
Output formatters for axm-ast.
Provides four output formats (text, JSON, Mermaid, compressed) at three detail levels (summary, detailed, full) with optional budget-based truncation and PageRank-based symbol ranking.
Example
from axm_ast.formatters import format_text output = format_text(pkg, detail="summary") print(output)
filter_modules(pkg, modules)
Return a shallow copy of pkg with modules filtered by name.
Each term in modules is matched as a case-insensitive substring
against the dotted module name. If modules is None or empty,
the original package is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg
|
PackageInfo
|
Analyzed package info. |
required |
modules
|
list[str] | None
|
Substring filters (OR logic). |
required |
Returns:
| Type | Description |
|---|---|
PackageInfo
|
PackageInfo with only matching modules (shallow copy). |
Source code in packages/axm-ast/src/axm_ast/formatters.py
format_compressed(pkg)
Format package as a compressed AI-friendly summary.
Produces an intermediate format between stub and full:
keeps signatures, first docstring line, constants, __all__,
and relative imports — drops function bodies, full docstrings,
absolute imports, and private symbols (unless in __all__).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg
|
PackageInfo
|
Analyzed package info. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Compressed text string. |
Example
print(format_compressed(pkg))
Source code in packages/axm-ast/src/axm_ast/formatters.py
format_json(pkg, *, detail='summary')
Format package info as a JSON-serializable dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg
|
PackageInfo
|
Analyzed package info. |
required |
detail
|
DetailLevel
|
Level of detail — |
'summary'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
JSON-serializable dictionary. |
Example
data = format_json(pkg, detail="summary") data["name"] 'sample_pkg'
Source code in packages/axm-ast/src/axm_ast/formatters.py
format_mermaid(pkg)
Format the import graph as a Mermaid flowchart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg
|
PackageInfo
|
Analyzed package info. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Mermaid diagram string. |
Example
print(format_mermaid(pkg)) graph TD cli --> core
Source code in packages/axm-ast/src/axm_ast/formatters.py
format_module_inspect_text(mod)
Format a single module for human-readable inspect output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mod
|
ModuleInfo
|
Module info object. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted text string. |
Source code in packages/axm-ast/src/axm_ast/formatters.py
format_symbol_text(symbol)
Format a single symbol for human-readable inspect output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
FunctionInfo | ClassInfo
|
A function or class info object. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted text string. |
Source code in packages/axm-ast/src/axm_ast/formatters.py
format_text(pkg, *, detail='summary', budget=None, rank=False)
Format package info as human-readable text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg
|
PackageInfo
|
Analyzed package info. |
required |
detail
|
DetailLevel
|
Level of detail — |
'summary'
|
budget
|
int | None
|
Maximum number of output lines. Truncates intelligently. |
None
|
rank
|
bool
|
When True with a budget, sort symbols by importance (PageRank) so the most relevant appear first. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted text string. |
Example
text = format_text(pkg, detail="summary") print(text)
Source code in packages/axm-ast/src/axm_ast/formatters.py
format_toc(pkg)
Format package as a table-of-contents — module names and counts only.
Returns lightweight module summaries WITHOUT individual function/class details. Useful for agents to decide which modules to drill into.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg
|
PackageInfo
|
Analyzed package info. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of module dicts with name, docstring, symbol_count, |
list[dict[str, Any]]
|
function_count, class_count. |
Example
toc = format_toc(pkg) toc[0]["name"] 'core.analyzer'