From 486ab1ce98ef2eaf33f18710d211b5e4fb45df1c Mon Sep 17 00:00:00 2001 From: MattCantor Date: Tue, 21 Jan 2025 10:39:54 -0500 Subject: [PATCH 1/2] add format flag to accounts command --- src/simplefin/cli/__init__.py | 46 ++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/simplefin/cli/__init__.py b/src/simplefin/cli/__init__.py index 2fbacf9..880b586 100644 --- a/src/simplefin/cli/__init__.py +++ b/src/simplefin/cli/__init__.py @@ -39,25 +39,36 @@ def setup() -> None: @cli.command() -def accounts() -> None: +@click.option( + "--format", + type=click.Choice(["json", "table"], case_sensitive=False), + default='table', + help='Specify output format' +) +def accounts(format: str) -> None: c = SimpleFINClient(access_url=os.getenv("SIMPLEFIN_ACCESS_URL")) accounts = c.get_accounts() - table = Table(title="SimpleFIN Accounts") - table.add_column("Institution") - table.add_column("Account") - table.add_column("Balance") - table.add_column("Account ID") - - for account in accounts: - table.add_row( - account["org"]["name"], - account["name"], - str(account["balance"]), - account["id"], - ) - console = Console() - console.print(table) + if format == 'json': + console = Console() + console.print(json.dumps(accounts, indent=4, cls=DateTimeEncoder)) + else: + table = Table(title="SimpleFIN Accounts") + table.add_column("Institution") + table.add_column("Account") + table.add_column("Balance") + table.add_column("Account ID") + + for account in accounts: + table.add_row( + account["org"]["name"], + account["name"], + str(account["balance"]), + account["id"], + ) + + console = Console() + console.print(table) # TODO: Add date range option @@ -92,7 +103,8 @@ def transactions(account_id: str, format: str, lookback_days: int) -> None: for txn in transactions: table.add_row( - txn["posted"].strftime("%d %b %Y"), txn["payee"], str(txn["amount"]) + txn["posted"].strftime( + "%d %b %Y"), txn["payee"], str(txn["amount"]) ) console = Console() From a25b8ccddb04400c96ab1f50c6fafcb6f741154e Mon Sep 17 00:00:00 2001 From: Chris Hasenpflug Date: Tue, 21 Jan 2025 10:38:00 -0600 Subject: [PATCH 2/2] ruff format --- src/simplefin/cli/__init__.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/simplefin/cli/__init__.py b/src/simplefin/cli/__init__.py index 880b586..972b4c3 100644 --- a/src/simplefin/cli/__init__.py +++ b/src/simplefin/cli/__init__.py @@ -42,14 +42,14 @@ def setup() -> None: @click.option( "--format", type=click.Choice(["json", "table"], case_sensitive=False), - default='table', - help='Specify output format' + default="table", + help="Specify output format", ) def accounts(format: str) -> None: c = SimpleFINClient(access_url=os.getenv("SIMPLEFIN_ACCESS_URL")) accounts = c.get_accounts() - if format == 'json': + if format == "json": console = Console() console.print(json.dumps(accounts, indent=4, cls=DateTimeEncoder)) else: @@ -103,8 +103,7 @@ def transactions(account_id: str, format: str, lookback_days: int) -> None: for txn in transactions: table.add_row( - txn["posted"].strftime( - "%d %b %Y"), txn["payee"], str(txn["amount"]) + txn["posted"].strftime("%d %b %Y"), txn["payee"], str(txn["amount"]) ) console = Console()