reconcile.helper: Check file specified with --beancount-file exists

This commit is contained in:
Ben Sturmfels 2023-01-11 19:11:45 +11:00
parent 7f7f325f73
commit 026f54bca1
Signed by: bsturmfels
GPG key ID: 023C05E2C9C068F0

View file

@ -20,6 +20,7 @@ from dateutil.relativedelta import relativedelta
import datetime
import decimal
import io
import sys
import tempfile
import textwrap
import typing
@ -113,29 +114,41 @@ def reconciliation_report_path(account, end_date):
return f'Financial/Controls/Reports-for-Treasurer/{end_date}_{account_name}_bank-reconciliation.csv'
# Parse all the arguments
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()
def parse_args():
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()
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()
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:
if not (args.month and args.period):
parser.error('--month and --period must be used together')
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:
if not (args.cur_end_date and args.prev_end_date):
parser.error(' --prev-end-date and --cur-end-date must be used together')
preDate = args.prev_end_date
lastDateInPeriod = args.cur_end_date.isoformat()
month = args.cur_end_date.strftime('%Y-%m')