From bd78e1649d36e998b354df3d60414135289d6b49 Mon Sep 17 00:00:00 2001 From: Christopher Moussa Date: Thu, 31 Oct 2024 13:11:07 -0700 Subject: [PATCH] formatter: add BankFormatter subclass Problem: The AccountingFormatter class has good overarching methods for printing the results of a query to the flux-accounting database, but there is some functionality specific to viewing bank information from the database that could use its own subclass. Add a new subclass called BankFormatter, which contains unique methods for viewing bank/user information in hierarchical and parsable formats with the view-bank command. Remove the helper functions previously defined in bank_subcommands.py in favor of using this new subclass. Add a --fields optional argument to allow the user to customize which columns they want to be returned when looking at a row of information for a bank. --- .../fluxacct/accounting/bank_subcommands.py | 4 ++-- .../python/fluxacct/accounting/formatter.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/bindings/python/fluxacct/accounting/bank_subcommands.py b/src/bindings/python/fluxacct/accounting/bank_subcommands.py index fa24299f..914ee13c 100644 --- a/src/bindings/python/fluxacct/accounting/bank_subcommands.py +++ b/src/bindings/python/fluxacct/accounting/bank_subcommands.py @@ -155,8 +155,8 @@ def view_bank(conn, bank, tree=False, users=False, parsable=False, cols=None): select_stmt = f"SELECT {', '.join(cols)} FROM bank_table WHERE bank=?" cur.execute(select_stmt, (bank,)) - # initialize AccountingFormatter object - formatter = fmt.BankFormatter(cur) + # initialize BankFormatter object + formatter = fmt.BankFormatter(cur, bank) if tree: if parsable: diff --git a/src/bindings/python/fluxacct/accounting/formatter.py b/src/bindings/python/fluxacct/accounting/formatter.py index 5d4c708b..a49ac27a 100644 --- a/src/bindings/python/fluxacct/accounting/formatter.py +++ b/src/bindings/python/fluxacct/accounting/formatter.py @@ -13,7 +13,7 @@ class AccountingFormatter: - def __init__(self, cursor): + def __init__(self, cursor, error_msg="no results found in query"): """ Initialize an AccountingFormatter object with a SQLite cursor. @@ -26,7 +26,7 @@ def __init__(self, cursor): if not self.rows: # the SQL query didn't fetch any results; raise an Exception - raise ValueError("no results found in query") + raise ValueError(error_msg) def get_column_names(self): """ @@ -102,6 +102,18 @@ class BankFormatter(AccountingFormatter): out banks/sub-banks in a hierarchical format and lists of users under banks. """ + def __init__(self, cursor, bank_name): + """ + Initialize a BankFormatter object with a SQLite cursor. + Args: + cursor: a SQLite Cursor object that has the results of a SQL query. + bank_name: the name of the bank. + """ + self.bank_name = bank_name + super().__init__( + cursor, error_msg=f"bank {self.bank_name} not found in bank_table" + ) + def as_tree(self): """ Format the flux-accounting bank hierarchy in tree format. The bank passed