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 --generate-html

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
--generate-html Generates the HTML page False

🧪 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.3.tar.gz (17.6 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.3-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flowcompass-0.1.3.tar.gz
  • Upload date:
  • Size: 17.6 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.3.tar.gz
Algorithm Hash digest
SHA256 c6d5a0599859957ab896ea815c480ee8c2439ab32874cb9ff3371abf30ec2a72
MD5 0245fcc70f50d696e8f9c3c0fd346f4b
BLAKE2b-256 ae7a8eef5de8e44a73d9189a308d252936d39f73677255e133cacbc654866753

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flowcompass-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c9d3c86330f91419f2f195e78370ff751571ba381b8c380db07952da50854e59
MD5 f9b3de670aff344587e4068ecc0d744f
BLAKE2b-256 720e8236ef8bd4d8c624becd1c05aad9d11da114fab58ba1b30e7f8343caee84

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