Skip to main content

A tool to generate flow diagrams from source code

Project description

📘 Project Documentation: Python Function Execution Flow Tracer


📌 Overview

This tool statically analyzes a Python codebase to trace the full execution flow of a given top-level function. It parses all files, identifies all function definitions and call relationships, and generates:

  • A printable tree view of the function call hierarchy.
  • A nested JSON report with detailed metadata for each function.
  • A html flow chart with the function call hierarchy and detailed codeflow.

Installation

pip install flowcompass

⚙️ Command-Line Usage

flowcompass --source-dir ./PATH_OF_SOURCE_DIR --function-name FUNCTION_NAME

Note:- PATH_OF_SOURCE_DIR is the path of your project directory and the FUNCTION_NAME is your name of your project function.

Available Flags

Flag Description Default
--source-dir Path to root codebase Required
--function-name Function to trace Required
--include-external Include undefined/external calls False
--skip-threaded Skip threaded/parallel calls False
--output-json Output path for JSON report <function_name>_report.json
--print-tree Print tree to stdout True

🧪 Example

Given the function create_book_api, the output might be:

create_book_api (app.py) [Main api handler for creating book]
|-- validate_input (helpers/create_book_helper.py) [validates the input given by user]
|   |-- input_validation (utility/inputvalidator.py) [simple input check]
|-- format_response (helpers/format_response.py) []

JSON saved as:

create_book_api_report.json

✅ Key Features

Feature Description
Recursive Analysis Builds a full tree of function calls starting from any named function.
Source-Aware Resolves internal calls across multiple files and directories.
External Call Detection Optionally includes external library or undefined calls.
Threaded Call Tracking Detects calls made via Thread, ThreadPoolExecutor.submit(), etc.
Recursion Detection Flags recursive calls to avoid infinite loops.
Call Metadata Captures function name, docstring, file, line number, call line, and source code.

🏗️ Architecture

1. Function Indexer

  • Walks through all .py files in the provided directory.
  • Uses ast to find all top-level and class-level function definitions.
  • Stores them in a function index:
    Dict[str, List[FunctionMeta]]

2. AST Analyzer

  • Recursively visits ast.Call nodes within a function body.
  • Resolves which functions are being called (via name or attribute).
  • Creates a tree of FunctionCallNode objects representing call flow.

3. Call Tree Builder

  • Builds a call tree with these fields:
    • func_name, qualified_name, file_path, lineno, call_line
    • threaded, external_call, is_recursive
    • child_calls: nested list of other FunctionCallNodes

4. Output Renderer

  • Tree print view using |-- format.
  • JSON export for structured output or further integration.

🧩 JSON Output Schema

Each node in the call tree has this format:

{
  "func_name": "validate_input",
  "qualified_name": "helpers.input.validate_input",
  "file_path": "helpers/input.py",
  "lineno": 12,
  "call_line": 25,
  "func_docstring": "Validates the input",
  "func_code": "def validate_input(...): ...",
  "call_order": 1,
  "external_call": false,
  "is_recursive": false,
  "threaded": false,
  "child_calls": [ ... ]
}

🧵 Threaded Call Detection

The tool detects function calls passed into:

  • executor.submit(...)
  • threading.Thread(target=...)

These are flagged as:

"threaded": true

🚧 Handling Edge Cases

Case Handling Strategy
External calls Skipped by default, include with --include-external
Threaded calls Included by default, skip with --skip-threaded
Recursive calls Tracked with visited set and is_recursive flag
Function name collisions Disambiguated via file + qualified name
Missing root function Graceful error message

🔄 Future Enhancements

Idea Description
HTML report Interactive call tree in browser
Graphviz/DOT export For visual flow diagrams
Full class-method resolution Map all method calls to their class
Asynchronous (async def) support Track coroutine flows

🧠 Requirements

  • Python 3.6+
  • No external libraries required — uses ast, os, json, argparse

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

flowcompass-0.1.2.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

flowcompass-0.1.2-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file flowcompass-0.1.2.tar.gz.

File metadata

  • Download URL: flowcompass-0.1.2.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for flowcompass-0.1.2.tar.gz
Algorithm Hash digest
SHA256 91a3a3b136feb2558d53f262acc634d6ef38aa00de97c0d86217a89b3590d90f
MD5 d4f60bad8539f82ae42394e696638617
BLAKE2b-256 502e8aa33d372157ca2d61267415d71fb1e5074e3918f8b12fd442151b2d092a

See more details on using hashes here.

File details

Details for the file flowcompass-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: flowcompass-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for flowcompass-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bab17a6ae0786a43d7c1c5c1372ce9e203da53d711fcb6d615497460598730e3
MD5 55e90ca974de7eb8328a8ddf101f2e58
BLAKE2b-256 9ca35c7c70c9524cebe3fc035153c9812516bcf28c592a7523a166865e1c676f

See more details on using hashes here.

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