From dbe8d02b7807163e5df6f5ee72df8ced48b4362a 2022-01-27 23:11:48 From: Ben Sturmfels Date: 2022-01-27 23:11:48 Subject: [PATCH] reconcile: Update helper to show N/A when no results, similar to Perl. Also clarified that a full account name should be passed. This aligns with the Perl behaviour. --- diff --git a/conservancy_beancount/reconcile/helper.py b/conservancy_beancount/reconcile/helper.py index f965596fd0f6c54686318f15efe9d93fbd8d9373..22e4beeebdc814f8ac02a50a471af0b0151e036d 100644 --- a/conservancy_beancount/reconcile/helper.py +++ b/conservancy_beancount/reconcile/helper.py @@ -33,18 +33,26 @@ def end_of_month(date): def format_record_for_grep(row, homedir): + """Return a line in a grep-style. + + This is so the line can be fed into Emacs grep-mode for quickly jumping to + the relevant lines in the books. + """ file = row[0].replace(homedir, '~') return [f'{file}:{row[1]}:'] + row[2:] def max_column_widths(rows): """Return the max width for each column in a table of data.""" - maxes = [0] * len(rows[0]) - for row in rows: - for i, val in enumerate(row): - length = len(str(val)) - maxes[i] = max(maxes[i], length) - return maxes + if not rows: + return [] + else: + maxes = [0] * len(rows[0]) + for row in rows: + for i, val in enumerate(row): + length = len(str(val)) + maxes[i] = max(maxes[i], length) + return maxes def tabulate(rows, headers=None): @@ -68,10 +76,11 @@ def tabulate(rows, headers=None): print('', file=output) return output.getvalue().strip() + # Parse all the arguments parser = argparse.ArgumentParser(description='Reconciliation helper') parser.add_argument('--beancount-file', required=True) -parser.add_argument('--account', help='Account regexp', 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.') @@ -156,7 +165,9 @@ print(f"START RECONCILIATION FOR {account} ENDING {lastDateInPeriod} (previous e entries, _, options = loader.load_file(beancount_file) for desc, query in QUERIES.items(): rtypes, rrows = run_query(entries, options, query, numberify=True) - if desc.startswith('04'): + if not rrows: + print(f'{desc:<55} N/A') + elif desc.startswith('04'): homedir = os.getenv('HOME') print(f'{desc}\n See {grep_output_file.name}') grep_rows = [format_record_for_grep(row, homedir) for row in rrows]