How launchplatform/beanhub-import works
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.