Skip to content

Commit

Permalink
formatter: add BankFormatter subclass
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmoussa1 committed Dec 30, 2024
1 parent 7504966 commit bd78e16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/bindings/python/fluxacct/accounting/bank_subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions src/bindings/python/fluxacct/accounting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bd78e16

Please sign in to comment.