Skip to main content

A base class for building prompt-based beancount importers

Project description

Prompt Importer

This package provides a PromptImporter base class to build other beancount importers with. The functionality PromptImporter provides is to prompt the user to input recipient accounts for each transaction while keeping a list of common accounts for easy access. For example:

02/05/2022: Online payment from CHK 1234, 100.00
What should the recipient account be? ('x' to not extract a transaction)
1. Expenses:Groceries   2. Expenses:Restaurants   3. Assets:Checking
>>

It also provides functionality to store regexes with which to automatically identify recipient accounts in the future.

Usage

Events

To create class that derives from PromptImporter you must first define a subclass of prompt_importer.Event. Events represent single transactions from whatever type of file you are importing. For example, if you are importing a csv file, you may have one event per row. The subclass should implement the following methods:

get_field(self, field: str) -> str

Each event may have different fields associated with it. This method should return the value of a field given an associated field name.

get_id(self) -> str

This should return the globally unique id associated with an event.

display(self) -> str

This is how the event will be displayed to the user before the prompt.

get_transaction(self, filename: str, index: int, recipient_account: str) -> data.Transaction

This should return a transaction associated with an event. To help build the transaction, it takes the file the event was sourced from, its index within the file, and the account that should be the "recipient" of the transaction.

Note that the data.Transaction type refers to the data from beancount.core.

Importers

Once you have defined an event you can create a subclass of PromptImporter. To do so, you must implement the typical methods associated with the beancountimporter.ImporterProtocol class. Important: the value the method name(self) returns should not contain characters not allowed in SQLite table names, such as periods.

The importer should also implement the following method:

get_events(self, f) -> list[Event]

Given a beancount file this should return a list of events for the importer to process.

Example

The following is an example of an event importer for Bank of America credit card reports. The implementation of the typical beancount importer methods (identify, file, etc.) are omitted as they are not the focus.

import csv

from prompt_importer.importer import PromptImporter, Event

from beancount.core import amount, data, flags
from beancount.core.number import D
from dateutil.parser import parse

class BofaCCEvent(Event):
    def __init__(self, row):
        self.data = {
            "Posted Date": row["Posted Date"],
            "Amount": row["Amount"],
            "Payee": row["Payee"],
            "Reference Number": row["Reference Number"],
        }

    def get_field(self, field: str) -> str:
        return self.data[field]

    def get_id(self) -> str:
        return f"{self.data['Reference Number']}"

    def display(self) -> str:
        return (
            f"{self.data['Posted Date']}: {self.data['Payee']}, {self.data['Amount']}"
        )

    def get_transaction(
        self, filename: str, index: int, recipient_account: str
    ) -> data.Transaction:
        return data.Transaction(
            meta=data.new_metadata(filename, index),
            date=parse(self.data["Posted Date"]),
            flag=flags.FLAG_OKAY,
            payee=self.data["Payee"],
            narration="",
            tags=set(),
            links=set(),
            postings=[
                data.Posting(
                    "Liabilities:BofaCreditCard",
                    amount.Amount(D(self.data["Amount"]), "USD"),
                    None,
                    None,
                    None,
                    None,
                ),
                data.Posting(recipient_account, None, None, None, None, None),
            ],
        )


class BofaCCImporter(PromptImporter):
    def __init__(self, db_file):
        super().__init__(db_file)

    def name(self):
        return "BofaCCImporter"

    def get_events(self, f) -> list[Event]:
        with open(f.name) as infile:
            return [BofaCCEvent(row) for row in csv.DictReader(infile)]

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

prompt_importer-0.1.1.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

prompt_importer-0.1.1-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file prompt_importer-0.1.1.tar.gz.

File metadata

  • Download URL: prompt_importer-0.1.1.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/20.5.0

File hashes

Hashes for prompt_importer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 91c9f3209193d45511ca8fc65c27c186f9ff4a078d390b31ecc8ecf650a3c916
MD5 620f752f879cfd74c862831ca5e0987f
BLAKE2b-256 0987cb973b0cf536f3f764991ae6f728a4053066d845597ca4ab8f16b06c5c2b

See more details on using hashes here.

File details

Details for the file prompt_importer-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: prompt_importer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/20.5.0

File hashes

Hashes for prompt_importer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 94e43ee07ff7b38299de4dd0e374ef915e8f2860bf45fefc2a10c24a834d44f7
MD5 dc92e307d0b6c460cb04ee582b7dd056
BLAKE2b-256 bfbc7bcade766483bb669e42bb101cedf88347bb21c5d67a3f02a2cfc5f1f04e

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