Changeset - 71bf8137a56f
[Not reviewed]
0 1 0
Ben Sturmfels (bsturmfels) - 20 months ago 2023-01-11 08:36:43
ben@sturm.com.au
reconcile.helper: Add entry_point to avoid traceback
1 file changed with 13 insertions and 3 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reconcile/helper.py
Show inline comments
...
 
@@ -105,51 +105,51 @@ def reconciliation_report(account, end_date, bank_account_balance, uncleared, pr
 
        w.writerow(['Items Still', 'Outstanding On', prev_end_date, 'Appeared By', end_date])
 
        w.writerow([])
 
        for trans in prev_uncleared:
 
            w.writerow(trans)
 
    return output.getvalue()
 

	
 

	
 
def reconciliation_report_path(account, end_date):
 
    *_, account_name = account.rpartition(':')
 
    return f'Financial/Controls/Reports-for-Treasurer/{end_date}_{account_name}_bank-reconciliation.csv'
 

	
 

	
 
def parse_args():
 
def parse_args(argv):
 
    parser = argparse.ArgumentParser(description='Reconciliation helper')
 
    parser.add_argument('--beancount-file', required=True)
 
    parser.add_argument('--account', help='Full account name, e.g. "Liabilities:CreditCard:AMEX"', required=True)
 
    parser.add_argument('--prev-end-date', type=datetime.date.fromisoformat)
 
    parser.add_argument('--cur-end-date', type=datetime.date.fromisoformat)
 
    parser.add_argument('--month', help='YYYY-MM of ending month. Use with --period.')
 
    parser.add_argument('--period', help='Months in the past to consider. Use with --month.', type=int, choices=[1, 3, 12])
 
    parser.add_argument('--statement-match')
 
    parser.add_argument('--cost-function', default='COST')
 
    parser.add_argument('--grep-output-filename')
 
    # parser.add_argument('--report-group-regex')
 
    args = parser.parse_args()
 
    args = parser.parse_args(args=argv[1:])
 
    if args.month or args.period:
 
        if not (args.month and args.period):
 
            parser.error('--month and --period must be used together')
 
    else:
 
        if not (args.cur_end_date and args.prev_end_date):
 
            parser.error(' --prev-end-date and --cur-end-date must be used together')
 
    return args
 

	
 

	
 
def beancount_file_exists(path):
 
    return os.path.isfile(path)
 

	
 

	
 
args = parse_args()
 
def main(args):
 
    if not beancount_file_exists(args.beancount_file):
 
        sys.exit(f'Beancount file does not exist: {args.beancount_file}')
 
    if args.month or args.period:
 
        parsed_date = datetime.datetime.strptime(args.month, '%Y-%m').date()
 
        preDate = end_of_month(parsed_date - relativedelta(months=args.period)).isoformat()
 
        lastDateInPeriod = end_of_month(parsed_date).isoformat()
 
        month = args.month
 
    else:
 
        preDate = args.prev_end_date
 
        lastDateInPeriod = args.cur_end_date.isoformat()
 
        month = args.cur_end_date.strftime('%Y-%m')
 
    grep_output_file: typing.IO
...
 
@@ -236,12 +236,22 @@ for desc, query in QUERIES.items():
 
            headers = [c[0].capitalize() for c in rtypes]
 
            if desc.startswith('03'):
 
                uncleared_rows = rrows
 
            print(desc)
 
            print(textwrap.indent(tabulate(rrows, headers=headers), '    '))
 

	
 
    uncleared = [(r[0], r[2], r[4] or r[3], r[1]) for r in uncleared_rows]
 
    report_path = os.path.join(os.getenv('CONSERVANCY_REPOSITORY', ''), reconciliation_report_path(account, lastDateInPeriod))
 
    # TODO: Make the directory if it doesn't exist.
 
    with open(report_path, 'w') as f:
 
        f.write(reconciliation_report(account, lastDateInPeriod, cleared_balance, uncleared, '1900-01-01', all_trans_balance, []))
 
    print(f'Wrote reconciliation report: {report_path}.')
 

	
 

	
 
if __name__ == '__main__':
 
    args = parse_args(sys.argv)
 
    main(args)
 

	
 

	
 
def entry_point():
 
    args = parse_args(sys.argv)
 
    main(args)
0 comments (0 inline, 0 general)