Skip to main content

A tool for mapping directory structures

Project description

CLAUDE.md - TreeMapper

Project Overview

treemapper is a Python tool that converts directory structures to YAML format, designed specifically for use with Large Language Models (LLMs). It maps entire codebases into structured YAML files, making it easy to analyze code, document projects, and work with AI tools.

Installation

Requires Python 3.9+:

pip install treemapper

Development Environment

  • Python 3.9+ required
  • Package dependencies: pathspec, pyyaml

Building and Installation

# Install in development mode
pip install -e .

# Install with development dependencies
pip install -e ".[dev]"

# Build distribution package
python -m build

# Install from PyPI
pip install treemapper

Testing

# Run all tests
pytest

# Run specific test file
pytest tests/test_basic.py

# Run specific test
pytest tests/test_basic.py::test_basic_mapping

# Run tests with coverage
pytest --cov=src/treemapper

# Run tests in verbose mode
pytest -v

Linting and Formatting

# Run flake8 linter
flake8 src/treemapper

# Run black formatter
black src/treemapper

# Run type checking with mypy
mypy src/treemapper

# Run autoflake to remove unused imports
autoflake --remove-all-unused-imports -i src/treemapper/*.py

# Run pre-commit hooks on all files
pre-commit run --all-files

# Run isort (import sorting)
isort src/treemapper tests

Code Quality Tools

The project includes comprehensive code quality checks via CI and pre-commit hooks:

# Complexity analysis
radon cc src/treemapper/ --min B  # Cyclomatic complexity
radon mi src/treemapper/ --min B  # Maintainability index

# Mutation testing (test effectiveness)
mutmut run --paths-to-mutate=src/treemapper/

# Architecture checks (import contracts)
lint-imports

# Coverage reporting
pytest --cov=src/treemapper --cov-report=html
open htmlcov/index.html

CI/CD Workflows

The project has two CI/CD workflows:

  1. Main CI (.github/workflows/ci.yml): Comprehensive quality checks

    • Pre-commit hook validation (all hooks)
    • Linting and type checking (flake8, black, mypy)
    • Cross-platform testing (Linux, macOS, Windows)
    • Python version matrix (3.9, 3.10, 3.11, 3.12)
    • PyPy compatibility testing (pypy-3.9, pypy-3.10)
    • Test coverage with 80% threshold and branch analysis
    • Mutation testing (test effectiveness validation)
    • Complexity and maintainability metrics (radon)
    • Architecture/import contract validation (import-linter)
    • SonarCloud quality gate (code quality analysis)
  2. CD (Release) (.github/workflows/cd.yml): Atomic releases

    • Version bump with git bundles
    • Multi-platform binary builds (Linux, macOS, Windows)
    • PyPI publishing (optional)
    • GitHub release creation with assets

Project Architecture

The codebase is organized as follows:

  • src/treemapper/: Main package
    • treemapper.py: Entry point and main orchestration
    • cli.py: Command-line argument parsing
    • ignore.py: Logic for handling ignore patterns (gitignore, treemapperignore)
    • tree.py: Core tree building functionality
    • writer.py: YAML output formatting and file writing
    • logger.py: Logging configuration

The application flow is:

  1. Parse command-line arguments (cli.py)
  2. Set up logging based on verbosity level (logger.py)
  3. Load ignore patterns from various sources (ignore.py)
  4. Build the directory tree structure (tree.py)
  5. Write the tree structure to a YAML file (writer.py)

Usage

Generate a structured representation of a directory:

# Map current directory to stdout (YAML format)
treemapper .

# Map specific directory to stdout
treemapper /path/to/dir

# Save to a file
treemapper . -o my-tree.yaml

# Use "-" to explicitly output to stdout
treemapper . -o -

# Output in JSON format
treemapper . --format json

# Output in plain text format
treemapper . --format text -o output.txt

# Limit directory traversal depth
treemapper . --max-depth 3

# Skip file contents (structure only)
treemapper . --no-content

# Limit file size for content reading
treemapper . --max-file-bytes 10000

# Custom ignore patterns
treemapper . -i ignore.txt

# Disable all default ignores
treemapper . --no-default-ignores

# Combine multiple options
treemapper . -o tree.json --format json --max-depth 5 --max-file-bytes 50000

# Set verbosity level (0=ERROR, 1=WARNING, 2=INFO, 3=DEBUG)
treemapper . -v 3

Options

treemapper [OPTIONS] [DIRECTORY]

Arguments:
  DIRECTORY                    Directory to analyze (default: current directory)

Options:
  -o, --output-file PATH      Output file (default: stdout)
                             Use "-" to force stdout output
  --format {yaml,json,text}   Output format (default: yaml)
  -i, --ignore-file PATH      Custom ignore patterns file
  --no-default-ignores        Disable all default ignores (.gitignore, .treemapperignore, etc.)
  --max-depth N               Maximum depth to traverse (default: unlimited)
  --no-content                Skip reading file contents (structure-only mode)
  --max-file-bytes N          Maximum file size to read in bytes (default: unlimited)
                             Larger files will show a placeholder
  -v, --verbosity [0-3]       Logging verbosity (default: 0)
                             0=ERROR, 1=WARNING, 2=INFO, 3=DEBUG
  --version                   Show version and exit
  -h, --help                  Show this help

Ignore Patterns

By default, treemapper ignores:

  • The output file itself (when using -o)
  • All .git directories
  • Python cache directories (__pycache__, .pytest_cache, .mypy_cache, etc.)
  • Python build artifacts (*.pyc, *.egg-info, dist/, build/, etc.)
  • Patterns from .gitignore files (in the scanned directory and subdirectories)
  • Patterns from .treemapperignore file (in the scanned root directory)
  • Symbolic links (always skipped)

Use --no-default-ignores to disable all default ignores and only use patterns from -i/--ignore-file.

Example Output

YAML format (default):

name: my-project
type: directory
children:
  - name: src
    type: directory
    children:
      - name: main.py
        type: file
        content: |
          def main():
              print("Hello World")
  - name: README.md
    type: file
    content: |
      # My Project
      Documentation here...

JSON format (--format json):

{
  "name": "my-project",
  "type": "directory",
  "children": [
    {
      "name": "src",
      "type": "directory",
      "children": [
        {
          "name": "main.py",
          "type": "file",
          "content": "def main():\n    print(\"Hello World\")\n"
        }
      ]
    },
    {
      "name": "README.md",
      "type": "file",
      "content": "# My Project\nDocumentation here...\n"
    }
  ]
}

Text format (--format text):

================================================================================
Directory Tree: my-project
================================================================================

src/ (directory)
  main.py (file)
    --- BEGIN CONTENT ---
    def main():
        print("Hello World")
    --- END CONTENT ---

README.md (file)
  --- BEGIN CONTENT ---
  # My Project
  Documentation here...
  --- END CONTENT ---

Creating a Distribution Package

# Build package
python -m build

# Create executable with PyInstaller
pyinstaller treemapper.spec

License

Apache License 2.0 - see LICENSE for details.

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

treemapper-1.0.4.tar.gz (39.1 kB view details)

Uploaded Source

Built Distribution

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

treemapper-1.0.4-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file treemapper-1.0.4.tar.gz.

File metadata

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

File hashes

Hashes for treemapper-1.0.4.tar.gz
Algorithm Hash digest
SHA256 5d2cbcd33c29c24ac021aeb33e778a4cb0dae15c0b2ed4e04469e68da8d522cc
MD5 2f66a44c28360c41a69948dac8d73b8b
BLAKE2b-256 4b00e55cc8c6d28651bfb0526d4042f1c5098464e8233f4b576ffc3bfcc4c52e

See more details on using hashes here.

Provenance

The following attestation bundles were made for treemapper-1.0.4.tar.gz:

Publisher: cd.yml on nikolay-e/treemapper

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

File details

Details for the file treemapper-1.0.4-py3-none-any.whl.

File metadata

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

File hashes

Hashes for treemapper-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 137ae5c15a4cfe91e383307519f2847608f6f370278aa441159f0e6098bcdaa3
MD5 37fe23bc51a78834b87199f4dd8e21d6
BLAKE2b-256 0c96dc5b3216000226f861177565226c52622b47d37e393aa4054bfa930b4e6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for treemapper-1.0.4-py3-none-any.whl:

Publisher: cd.yml on nikolay-e/treemapper

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