reconcile.helper: Check file specified with --beancount-file exists
This commit is contained in:
		
							parent
							
								
									7f7f325f73
								
							
						
					
					
						commit
						026f54bca1
					
				
					 1 changed files with 30 additions and 17 deletions
				
			
		| 
						 | 
					@ -20,6 +20,7 @@ from dateutil.relativedelta import relativedelta
 | 
				
			||||||
import datetime
 | 
					import datetime
 | 
				
			||||||
import decimal
 | 
					import decimal
 | 
				
			||||||
import io
 | 
					import io
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
import tempfile
 | 
					import tempfile
 | 
				
			||||||
import textwrap
 | 
					import textwrap
 | 
				
			||||||
import typing
 | 
					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'
 | 
					    return f'Financial/Controls/Reports-for-Treasurer/{end_date}_{account_name}_bank-reconciliation.csv'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Parse all the arguments
 | 
					def parse_args():
 | 
				
			||||||
parser = argparse.ArgumentParser(description='Reconciliation helper')
 | 
					    parser = argparse.ArgumentParser(description='Reconciliation helper')
 | 
				
			||||||
parser.add_argument('--beancount-file', required=True)
 | 
					    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('--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('--prev-end-date', type=datetime.date.fromisoformat)
 | 
				
			||||||
parser.add_argument('--cur-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('--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('--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('--statement-match')
 | 
				
			||||||
parser.add_argument('--cost-function', default='COST')
 | 
					    parser.add_argument('--cost-function', default='COST')
 | 
				
			||||||
parser.add_argument('--grep-output-filename')
 | 
					    parser.add_argument('--grep-output-filename')
 | 
				
			||||||
# parser.add_argument('--report-group-regex')
 | 
					    # parser.add_argument('--report-group-regex')
 | 
				
			||||||
args = parser.parse_args()
 | 
					    args = parser.parse_args()
 | 
				
			||||||
if args.month or args.period:
 | 
					    if args.month or args.period:
 | 
				
			||||||
        if not (args.month and args.period):
 | 
					        if not (args.month and args.period):
 | 
				
			||||||
            parser.error('--month and --period must be used together')
 | 
					            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:
 | 
				
			||||||
    parsed_date = datetime.datetime.strptime(args.month, '%Y-%m').date()
 | 
					    parsed_date = datetime.datetime.strptime(args.month, '%Y-%m').date()
 | 
				
			||||||
    preDate = end_of_month(parsed_date - relativedelta(months=args.period)).isoformat()
 | 
					    preDate = end_of_month(parsed_date - relativedelta(months=args.period)).isoformat()
 | 
				
			||||||
    lastDateInPeriod = end_of_month(parsed_date).isoformat()
 | 
					    lastDateInPeriod = end_of_month(parsed_date).isoformat()
 | 
				
			||||||
    month = args.month
 | 
					    month = args.month
 | 
				
			||||||
else:
 | 
					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
 | 
					    preDate = args.prev_end_date
 | 
				
			||||||
    lastDateInPeriod = args.cur_end_date.isoformat()
 | 
					    lastDateInPeriod = args.cur_end_date.isoformat()
 | 
				
			||||||
    month = args.cur_end_date.strftime('%Y-%m')
 | 
					    month = args.cur_end_date.strftime('%Y-%m')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue