# kivicode/pipzap architecture

> Architecture diagram of the GitHub repository kivicode/pipzap, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kivicode/pipzap
- Repository: https://github.com/kivicode/pipzap

## Overview

##### Project Type and Purpose
- This is a Python command-line tool (CLI) for dependency pruning and format conversion.
- It ingests a dependency file (`requirements.txt`, `pyproject.toml` for Poetry or uv), resolves the full graph via the uv library, prunes transitive dependencies, and emits a minimal list in the chosen format.

##### High-Level Components
- **CLI Layer** (pipzap/cli.py, __main__.py)
- Argument parsing (file path, python version, output format, verbosity)
- Entrypoint dispatch to internal pipeline
- **Parsing Layer** (pipzap/parsing)
- `parser.py`: Detects input format and extracts raw dependencies
- `converter.py`: Normalizes legacy Poetry keys to standard project metadata
- `workspace.py`: Builds a temporary isolated `pyproject.toml` for uv resolution
- **Core Layer** (pipzap/core)
- `dependencies.py`: Interfaces with uv to build the full dependency graph
- `pruner.py`: Walks the graph to remove transitively satisfied dependencies
- `source_format.py`: Captures source file metadata (python version, extras, indexes)
- **Formatting Layer** (pipzap/formatting)
- `base.py`: Defines the formatter interface
- `_uv_to_poetry.py`, `poetry.py`, `requirements.py`, `uv.py`: Implement output in uv, Poetry, or pip-style formats
- **Utilities & Exceptions** (pipzap/utils, pipzap/exceptions.py)
- I/O helpers, debug logging, string utilities, custom error classes
- **External Service**
- The `uv` library (astral-sh/uv) for dependency resolution

##### Data Flow & Interactions
- User runs `pipzap <input-file> [options]`.
→ **CLI Layer** reads args
→ reads the input file via **IO util**
→ **Parsing Layer** detects format and extracts direct dependencies
→ **Workspace** writes a temp `pyproject.toml`
→ **Core Layer** calls uv resolver → gets full dependency graph
→ **Pruner** removes redundancies → returns pruned list + metadata
→ **Formatting Layer** renders the output format
→ writes to stdout or output file via **IO util**

##### Architectural Patterns & Design Principles
- **Single Responsibility**: Each subpackage (cli, parsing, core, formatting) has a well-defined role.
- **Pipeline**: Clear linear flow from input → parse → resolve → prune → format → output.
- **Dependency Injection**: Formatter implementations plug into the CLI via the base interface.
- **Isolation**: Uses a temporary workspace to avoid side effects in the user’s environment.

##### Technologies & Frameworks
- Python 3.8+
- `click` or `argparse` (in cli.py) for argument parsing
- `uv` library for dependency resolution
- Poetry/pyproject metadata parsing
- pytest for tests, pre-commit/ruff for linting, GitHub Actions for CI

##### Checklist
- [ ] Each package/module is a labeled node.
- [ ] External dependencies (uv library, file system I/O) are distinctly marked.
- [ ] Options (format choice, python version) are annotated near the CLI node.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    subgraph "CLI Layer"
        CLI_Main["__main__.py"]:::cli
        CLI_CLI["cli.py\n(options: --python-version, --format)"]:::cli
    end

    %% Parsing Layer
    subgraph "Parsing Layer"
        Parser["parser.py"]:::parsing
        Converter["converter.py"]:::parsing
        Workspace["workspace.py\n(temp pyproject.toml)"]:::parsing
    end

    %% Core Layer
    subgraph "Core Layer"
        Dependencies["dependencies.py"]:::core
        Pruner["pruner.py"]:::core
        SourceFormat["source_format.py"]:::core
    end

    %% Formatting Layer
    subgraph "Formatting Layer"
        Base["base.py"]:::formatting
        UVtoPoetry["_uv_to_poetry.py"]:::formatting
        PoetryFmt["poetry.py"]:::formatting
        RequirementsFmt["requirements.py"]:::formatting
        UVFmt["uv.py"]:::formatting
    end

    %% Utilities & Exceptions
    subgraph "Utilities & Exceptions"
        IOUtil["io.py"]:::utils
        DebugUtil["debug.py"]:::utils
        PrettyStr["pretty_string.py"]:::utils
        ReqStr["requirement_string.py"]:::utils
        Exceptions["exceptions.py"]:::exceptions
    end

    %% External Service
    UVService(["uv Library"]):::external

    %% Data Flow
    CLI_Main -->|"dispatch"| CLI_CLI
    CLI_CLI -->|"read input file"| IOUtil
    IOUtil -->|"file content"| Parser
    Parser -->|"raw dependency list"| Converter
    Converter -->|"normalized metadata"| Workspace
    Workspace -->|"temp pyproject.toml"| Dependencies
    Dependencies -->|"resolve call"| UVService
    UVService -->|"resolved graph"| Dependencies
    Dependencies -->|"full dependency graph"| Pruner
    Pruner -->|"pruned list + metadata"| SourceFormat
    SourceFormat -->|"enriched metadata"| Base
    Base -->|"format choice"| UVtoPoetry
    Base -->|"format choice"| PoetryFmt
    Base -->|"format choice"| RequirementsFmt
    Base -->|"format choice"| UVFmt
    UVtoPoetry -->|"formatted output"| IOUtil
    PoetryFmt -->|"formatted output"| IOUtil
    RequirementsFmt -->|"formatted output"| IOUtil
    UVFmt -->|"formatted output"| IOUtil

    %% Click Events
    click CLI_Main "https://github.com/kivicode/pipzap/blob/main/pipzap/__main__.py"
    click CLI_CLI "https://github.com/kivicode/pipzap/blob/main/pipzap/cli.py"
    click Parser "https://github.com/kivicode/pipzap/blob/main/pipzap/parsing/parser.py"
    click Converter "https://github.com/kivicode/pipzap/blob/main/pipzap/parsing/converter.py"
    click Workspace "https://github.com/kivicode/pipzap/blob/main/pipzap/parsing/workspace.py"
    click Dependencies "https://github.com/kivicode/pipzap/blob/main/pipzap/core/dependencies.py"
    click Pruner "https://github.com/kivicode/pipzap/blob/main/pipzap/core/pruner.py"
    click SourceFormat "https://github.com/kivicode/pipzap/blob/main/pipzap/core/source_format.py"
    click Base "https://github.com/kivicode/pipzap/blob/main/pipzap/formatting/base.py"
    click UVtoPoetry "https://github.com/kivicode/pipzap/blob/main/pipzap/formatting/_uv_to_poetry.py"
    click PoetryFmt "https://github.com/kivicode/pipzap/blob/main/pipzap/formatting/poetry.py"
    click RequirementsFmt "https://github.com/kivicode/pipzap/blob/main/pipzap/formatting/requirements.py"
    click UVFmt "https://github.com/kivicode/pipzap/blob/main/pipzap/formatting/uv.py"
    click IOUtil "https://github.com/kivicode/pipzap/blob/main/pipzap/utils/io.py"
    click DebugUtil "https://github.com/kivicode/pipzap/blob/main/pipzap/utils/debug.py"
    click PrettyStr "https://github.com/kivicode/pipzap/blob/main/pipzap/utils/pretty_string.py"
    click ReqStr "https://github.com/kivicode/pipzap/blob/main/pipzap/utils/requirement_string.py"
    click Exceptions "https://github.com/kivicode/pipzap/blob/main/pipzap/exceptions.py"

    %% Styles
    classDef cli fill:#AED6F1,stroke:#3498DB,color:#1B4F72;
    classDef parsing fill:#D5F5E3,stroke:#27AE60,color:#145A32;
    classDef core fill:#FAD7A0,stroke:#EB984E,color:#7E5109;
    classDef formatting fill:#E8DAEF,stroke:#9B59B6,color:#4A235A;
    classDef utils fill:#EBEBEB,stroke:#BDC3C7,color:#566573;
    classDef exceptions fill:#EBEBEB,stroke:#BDC3C7,color:#566573;
    classDef external fill:#FCF3CF,stroke:#F4D03F,color:#7D6608;
```
