# launchplatform/beanhub-import architecture

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

- Interactive diagram: https://gitdiagram.com/launchplatform/beanhub-import
- Repository: https://github.com/launchplatform/beanhub-import

## Overview

Based on our analysis of the file tree and README, beanhub-import is a Python library designed to import extracted transactions (typically from CSV files produced by beanhub-extract) and generate Beancount transactions by applying a set of declarative rules defined in YAML. The architecture can be depicted as a pipeline that “ingests” input files, applies configurable extraction and matching rules (using Jinja2 templates), and then produces idempotent transaction files with options like auto-updating and auto-migration.

##### Main Components
- Configuration Loader & Parser: Represents the component that reads declarative YAML files (e.g., .beanhub/imports.yaml as shown in the README example) and loads global contexts, input rules (including filters, loops, extra attributes, etc.), and import rules.
- Input and Extractor Handler: This part is responsible for reading the raw data files (CSV) and applying the appropriate extractor (such as “mercury” or “chase_credit_card”). This is primarily handled within the “processor.py” module.
- Transaction Processor and Rule Engine: Using the extracted data and the loaded configuration, the core matching logic then decides which rules apply to each transaction. This module (with parts in “processor.py” and “data_types.py”) also involves condition checking (such as filter and loop rule evaluation).
- Template Renderer: The “templates.py” module leverages Jinja2 templating to transform extracted data into proper transaction outputs following the rules. This component integrates the context variables defined in the YAML.
- Post-Processing: Once the transactions are created, the “post_processor.py” component takes over to handle idempotency, auto-migration of transactions (removing or adding transactions based on changes to rules), and any further adjustments required.
- Testing & Continuous Integration: While not a processing component per se, the tests (in the tests folder) and CI configurations (in .circleci/config.yml, pre-commit configurations) ensure reliability and conformity of the architecture.

##### Relationships and Interactions
- Then, indicate the Rule Engine/Processor which applies the YAML-defined rules to map raw data to structured transaction data.
- The Template Renderer uses Jinja2 to generate formatted Beancount transactions.
- Finally, the Post-Processor adjusts and writes output to the designated Beancount files.
- Optionally, indicate integration with external systems (e.g., beanhub-extract or the CLI tool, beanhub-cli) that might serve as the entry point to the library.

##### Architectural Patterns or Principles
- Separation between the extraction logic and the transformation (template rendering) logic.
- The design is declarative: the behavior of the import process is determined by user-defined YAML without hardcoding Python logic.
- Idempotence and auto-update/migration are important design principles that should be noted.
- The modular design, with explicit components (configuration, extraction, templating, post-processing), invites extensibility and testing.

##### Relevant Technologies and Libraries
- Python as the primary programming language.
- YAML for configuration files.
- Jinja2 for template rendering.
- Testing frameworks (likely pytest given the tests structure).
- CI tools (CircleCI with configurations in .circleci/config.yml).

##### Instructing the Principal Software Engineer
- An Input Reader that imports CSV files and selects the proper Extractor based on configuration.
- Template Renderer as a sub-component that uses Jinja2 templates to generate Beancount transactions.
- Finally, incorporate the Post-Processor that ensures idempotency and manages file migrations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Configuration & Input Layer"
        A["YAML Configuration Loader & Parser"]:::config
        B["Input & Extractor Handler"]:::input
    end

    subgraph "Processing Layer"
        C["Transaction Processor & Rule Engine"]:::logic
        D["Template Renderer (Jinja2)"]:::render
        E["Post-Processing (Idempotency & Auto-Migration)"]:::post
    end

    subgraph "Support Tools"
        F["Testing & Continuous Integration"]:::support
    end

    A -->|"provides-config"| B
    A -->|"provides-config"| C
    B -->|"raw-CSV-data"| C
    C -->|"template-variables"| D
    D -->|"formatted-transactions"| E

    F -.-> A
    F -.-> B
    F -.-> C
    F -.-> D
    F -.-> E

    click A "https://github.com/launchplatform/beanhub-import/blob/master/beanhub_import/processor.py"
    click B "https://github.com/launchplatform/beanhub-import/blob/master/beanhub_import/processor.py"
    click C "https://github.com/launchplatform/beanhub-import/blob/master/beanhub_import/processor.py"
    click C "https://github.com/launchplatform/beanhub-import/blob/master/beanhub_import/data_types.py"
    click D "https://github.com/launchplatform/beanhub-import/blob/master/beanhub_import/templates.py"
    click E "https://github.com/launchplatform/beanhub-import/blob/master/beanhub_import/post_processor.py"
    click F "https://github.com/launchplatform/beanhub-import/tree/master/tests/"
    click F "https://github.com/launchplatform/beanhub-import/blob/master/.circleci/config.yml"
    click F "https://github.com/launchplatform/beanhub-import/blob/master/.pre-commit-config.yaml"

    classDef config fill:#ffcccc,stroke:#000000,stroke-width:2px;
    classDef input fill:#ccffcc,stroke:#000000,stroke-width:2px;
    classDef logic fill:#ccccff,stroke:#000000,stroke-width:2px;
    classDef render fill:#ffffcc,stroke:#000000,stroke-width:2px;
    classDef post fill:#ffddaa,stroke:#000000,stroke-width:2px;
    classDef support fill:#d9d9d9,stroke:#000000,stroke-width:2px;
```
