43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
import sys
 | 
						|
import argparse
 | 
						|
import os
 | 
						|
 | 
						|
import capital_one
 | 
						|
import database
 | 
						|
 | 
						|
parser = argparse.ArgumentParser(prog="BudgetBear", description="Calculate a budget fit for a bear.")
 | 
						|
parser.add_argument('files', type=str, nargs='+', help='File to parse transactions from.')
 | 
						|
args = parser.parse_args()
 | 
						|
 | 
						|
# Get my user.
 | 
						|
database.instance.connect()
 | 
						|
user = database.User.select().where(database.User.username == 'ciphercules').get()
 | 
						|
database.instance.close()
 | 
						|
 | 
						|
file_parsers = [capital_one.Parser()]
 | 
						|
for f in args.files:
 | 
						|
    filename=os.path.basename(f),
 | 
						|
    for file_parser in file_parsers:
 | 
						|
        # Use the first successful parser.
 | 
						|
        transactions = file_parser.parse(f)
 | 
						|
        if not transactions:
 | 
						|
            continue
 | 
						|
 | 
						|
        # Add to database
 | 
						|
        database.instance.connect()
 | 
						|
        with database.instance.atomic():
 | 
						|
            # Add each transaction
 | 
						|
            for transaction in transactions:
 | 
						|
                date, description, amount = transaction
 | 
						|
                database.Transaction.create(
 | 
						|
                    user=user,
 | 
						|
                    transaction_date=date,
 | 
						|
                    description=description,
 | 
						|
                    amount=amount,
 | 
						|
                    source_file=filename,
 | 
						|
                    type=file_parser.source
 | 
						|
                )
 | 
						|
        database.instance.close()
 | 
						|
 | 
						|
        # We successfully updated database with this parser, exit loop.
 | 
						|
        break |