Package: prompt-importer
Prompt ImporterThis package provides aPromptImporterbase class to build other beancount importers with.
The functionalityPromptImporterprovides 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.UsageEventsTo create class that derives fromPromptImporteryou must first define a subclass ofprompt_importer.Event.
Events represent single transactions from whatever type of file you are importing.
For example, if you are importing acsvfile, you may have one event per row.
The subclass should implement the following methods:get_field(self, field: str) -> strEach 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) -> strThis should return theglobally uniqueid associated with an event.display(self) -> strThis is how the event will be displayed to the user before the prompt.get_transaction(self, filename: str, index: int, recipient_account: str) -> data.TransactionThis 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 thedata.Transactiontype refers to thedatafrombeancount.core.ImportersOnce you have defined an event you can create a subclass ofPromptImporter.
To do so, you must implement the typical methods associated with the beancountimporter.ImporterProtocolclass.Important:the value the methodname(self)returns shouldnotcontain 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.ExampleThe 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.importcsvfromprompt_importer.importerimportPromptImporter,Eventfrombeancount.coreimportamount,data,flagsfrombeancount.core.numberimportDfromdateutil.parserimportparseclassBofaCCEvent(Event):def__init__(self,row):self.data={"Posted Date":row["Posted Date"],"Amount":row["Amount"],"Payee":row["Payee"],"Reference Number":row["Reference Number"],}defget_field(self,field:str)->str:returnself.data[field]defget_id(self)->str:returnf"{self.data['Reference Number']}"defdisplay(self)->str:return(f"{self.data['Posted Date']}:{self.data['Payee']},{self.data['Amount']}")defget_transaction(self,filename:str,index:int,recipient_account:str)->data.Transaction:returndata.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),],)classBofaCCImporter(PromptImporter):def__init__(self,db_file):super().__init__(db_file)defname(self):return"BofaCCImporter"defget_events(self,f)->list[Event]:withopen(f.name)asinfile:return[BofaCCEvent(row)forrowincsv.DictReader(infile)]