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.
This commit is contained in:
Ben Sturmfels 2022-01-28 10:08:01 +11:00
parent 970fea86fd
commit dbe8d02b78
Signed by: bsturmfels
GPG key ID: 023C05E2C9C068F0

View file

@ -33,18 +33,26 @@ def end_of_month(date):
def format_record_for_grep(row, homedir): 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, '~') file = row[0].replace(homedir, '~')
return [f'{file}:{row[1]}:'] + row[2:] return [f'{file}:{row[1]}:'] + row[2:]
def max_column_widths(rows): def max_column_widths(rows):
"""Return the max width for each column in a table of data.""" """Return the max width for each column in a table of data."""
maxes = [0] * len(rows[0]) if not rows:
for row in rows: return []
for i, val in enumerate(row): else:
length = len(str(val)) maxes = [0] * len(rows[0])
maxes[i] = max(maxes[i], length) for row in rows:
return maxes for i, val in enumerate(row):
length = len(str(val))
maxes[i] = max(maxes[i], length)
return maxes
def tabulate(rows, headers=None): def tabulate(rows, headers=None):
@ -68,10 +76,11 @@ def tabulate(rows, headers=None):
print('', file=output) print('', file=output)
return output.getvalue().strip() return output.getvalue().strip()
# Parse all the arguments # Parse all the arguments
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='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('--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.')
@ -156,7 +165,9 @@ print(f"START RECONCILIATION FOR {account} ENDING {lastDateInPeriod} (previous e
entries, _, options = loader.load_file(beancount_file) entries, _, options = loader.load_file(beancount_file)
for desc, query in QUERIES.items(): for desc, query in QUERIES.items():
rtypes, rrows = run_query(entries, options, query, numberify=True) 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') homedir = os.getenv('HOME')
print(f'{desc}\n See {grep_output_file.name}') print(f'{desc}\n See {grep_output_file.name}')
grep_rows = [format_record_for_grep(row, homedir) for row in rrows] grep_rows = [format_record_for_grep(row, homedir) for row in rrows]