Skip to content

Commit cdb8038

Browse files
peter simpsonpeter simpson
authored andcommitted
adding some extra context to the treeviewer
1 parent e3d3cd1 commit cdb8038

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

src/clidat/commands/accounting.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def get_accounts(
3131
order_by=order_by,
3232
)
3333
accounts_json = accounts.json()
34-
viewer = ViewerDispatcher(data=accounts_json, json_flag=json)
34+
viewer = ViewerDispatcher(
35+
data=accounts_json, json_flag=json, data_type="Chart of Accounts"
36+
)
3537
viewer()
3638

3739

@@ -43,7 +45,9 @@ def get_account(ctx: click.Context, company_id: str, account: str, json: bool =
4345
client = ctx.obj
4446
account_result = client.get_account(company_id, account)
4547
account_result_json = account_result.json()
46-
viewer = ViewerDispatcher(data=account_result_json, json_flag=json)
48+
viewer = ViewerDispatcher(
49+
data=account_result_json, json_flag=json, data_type="Accounts"
50+
)
4751
viewer()
4852

4953

@@ -70,7 +74,11 @@ def get_account_transactions(
7074
order_by=order_by,
7175
)
7276
account_transactions_json = account_transactions.json()
73-
viewer = ViewerDispatcher(data=account_transactions_json, json_flag=json)
77+
viewer = ViewerDispatcher(
78+
data=account_transactions_json,
79+
json_flag=json,
80+
data_type="List of Account Transactions",
81+
)
7482
viewer()
7583

7684

@@ -90,7 +98,11 @@ def get_account_transaction(
9098
company_id, connection, account_transaction
9199
)
92100
account_transaction_result_json = account_transaction_result.json()
93-
viewer = ViewerDispatcher(data=account_transaction_result_json, json_flag=json)
101+
viewer = ViewerDispatcher(
102+
data=account_transaction_result_json,
103+
json_flag=json,
104+
data_type="Account Transaction",
105+
)
94106
viewer()
95107

96108

@@ -115,7 +127,9 @@ def get_bills(
115127
order_by=order_by,
116128
)
117129
bills_json = bills.json()
118-
viewer = ViewerDispatcher(data=bills_json, json_flag=json)
130+
viewer = ViewerDispatcher(
131+
data=bills_json, json_flag=json, data_type="List of Bills"
132+
)
119133
viewer()
120134

121135

@@ -127,7 +141,7 @@ def get_bill(ctx: click.Context, company_id: str, bill: str, json: bool = False)
127141
client = ctx.obj
128142
bill_result = client.get_bill(company_id, bill)
129143
bill_result_json = bill_result.json()
130-
viewer = ViewerDispatcher(data=bill_result_json, json_flag=json)
144+
viewer = ViewerDispatcher(data=bill_result_json, json_flag=json, data_type="Bills")
131145
viewer()
132146

133147

@@ -152,7 +166,9 @@ def get_suppliers(
152166
order_by=order_by,
153167
)
154168
suppliers_json = suppliers.json()
155-
viewer = ViewerDispatcher(data=suppliers_json, json_flag=json)
169+
viewer = ViewerDispatcher(
170+
data=suppliers_json, json_flag=json, data_type="List of Suppliers"
171+
)
156172
viewer()
157173

158174

@@ -166,7 +182,9 @@ def get_supplier(
166182
client = ctx.obj
167183
supplier_result = client.get_supplier(company_id, supplier)
168184
supplier_result_json = supplier_result.json()
169-
viewer = ViewerDispatcher(data=supplier_result_json, json_flag=json)
185+
viewer = ViewerDispatcher(
186+
data=supplier_result_json, json_flag=json, data_type="Supplier"
187+
)
170188
viewer()
171189

172190

@@ -191,7 +209,9 @@ def get_invoices(
191209
order_by=order_by,
192210
)
193211
invoices_json = invoices.json()
194-
viewer = ViewerDispatcher(data=invoices_json, json_flag=json)
212+
viewer = ViewerDispatcher(
213+
data=invoices_json, json_flag=json, data_type="List of Invoices"
214+
)
195215
viewer()
196216

197217

@@ -203,7 +223,9 @@ def get_invoice(ctx: click.Context, company_id: str, invoice: str, json: bool =
203223
client = ctx.obj
204224
invoice_result = client.get_invoice(company_id, invoice)
205225
invoice_result_json = invoice_result.json()
206-
viewer = ViewerDispatcher(data=invoice_result_json, json_flag=json)
226+
viewer = ViewerDispatcher(
227+
data=invoice_result_json, json_flag=json, data_type="Invoice"
228+
)
207229
viewer()
208230

209231

@@ -228,7 +250,9 @@ def get_payments(
228250
order_by=order_by,
229251
)
230252
payments_json = payments.json()
231-
viewer = ViewerDispatcher(data=payments_json, json_flag=json)
253+
viewer = ViewerDispatcher(
254+
data=payments_json, json_flag=json, data_type="List of Payments"
255+
)
232256
viewer()
233257

234258

@@ -240,5 +264,7 @@ def get_payment(ctx: click.Context, company_id: str, payment: str, json: bool =
240264
client = ctx.obj
241265
payment_result = client.get_payment(company_id, payment)
242266
payment_result_json = payment_result.json()
243-
viewer = ViewerDispatcher(data=payment_result_json, json_flag=json)
267+
viewer = ViewerDispatcher(
268+
data=payment_result_json, json_flag=json, data_type="Payment"
269+
)
244270
viewer()

src/clidat/commands/platform.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_company(ctx: click.Context, company_id: str, json: bool = False):
1616
client = ctx.obj
1717
company = client.get_company(company_id)
1818
company_json = company.json()
19-
viewer = ViewerDispatcher(data=company_json, json_flag=json)
19+
viewer = ViewerDispatcher(data=company_json, json_flag=json, data_type="Company")
2020
viewer()
2121

2222

@@ -36,7 +36,9 @@ def get_companies(
3636
page_size=page_size, page_number=page_number, query=query, order_by=order_by
3737
)
3838
companies_json = companies.json()
39-
viewer = ViewerDispatcher(data=companies_json, json_flag=json)
39+
viewer = ViewerDispatcher(
40+
data=companies_json, json_flag=json, data_type="List of Companies"
41+
)
4042
viewer()
4143

4244

@@ -47,7 +49,9 @@ def get_sync_settings(ctx: click.Context, company_id: str, json: bool = False):
4749
client = ctx.obj
4850
sync_settings = client.get_sync_settings(company_id)
4951
sync_settings_json = sync_settings.json()
50-
viewer = ViewerDispatcher(data=sync_settings_json, json_flag=json)
52+
viewer = ViewerDispatcher(
53+
data=sync_settings_json, json_flag=json, data_type="Sync Settings"
54+
)
5155
viewer()
5256

5357

@@ -72,7 +76,9 @@ def get_connections(
7276
order_by=order_by,
7377
)
7478
connections_json = connections.json()
75-
viewer = ViewerDispatcher(data=connections_json, json_flag=json)
79+
viewer = ViewerDispatcher(
80+
data=connections_json, json_flag=json, data_type="List of Connections"
81+
)
7682
viewer()
7783

7884

@@ -86,7 +92,9 @@ def get_connection(
8692
client = ctx.obj
8793
connection_result = client.get_connection(company_id, connection)
8894
connection_result_json = connection_result.json()
89-
viewer = ViewerDispatcher(data=connection_result_json, json_flag=json)
95+
viewer = ViewerDispatcher(
96+
data=connection_result_json, json_flag=json, data_type="Connection"
97+
)
9098
viewer()
9199

92100

@@ -111,7 +119,9 @@ def get_datasets(
111119
order_by=order_by,
112120
)
113121
datasets_json = datasets.json()
114-
viewer = ViewerDispatcher(data=datasets_json, json_flag=json)
122+
viewer = ViewerDispatcher(
123+
data=datasets_json, json_flag=json, data_type="List of Datasets"
124+
)
115125
viewer()
116126

117127

@@ -123,7 +133,9 @@ def get_dataset(ctx, company_id, dataset, json: bool = False):
123133
client = ctx.obj
124134
dataset_result = client.get_data_set(company_id, dataset)
125135
dataset_result_json = dataset_result.json()
126-
viewer = ViewerDispatcher(data=dataset_result_json, json_flag=json)
136+
viewer = ViewerDispatcher(
137+
data=dataset_result_json, json_flag=json, data_type="Dataset"
138+
)
127139
viewer()
128140

129141

@@ -134,5 +146,7 @@ def get_data_status(ctx: click.Context, company_id: str, json: bool = False):
134146
client = ctx.obj
135147
data_status = client.get_data_status(company_id)
136148
data_status_json = data_status.json()
137-
viewer = ViewerDispatcher(data=data_status_json, json_flag=json)
149+
viewer = ViewerDispatcher(
150+
data=data_status_json, json_flag=json, data_type="Data Status"
151+
)
138152
viewer()

src/clidat/tui/viewer.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99

1010

1111
class ViewerDispatcher:
12-
def __init__(self, data: str, json_flag: bool = False):
12+
def __init__(self, data: str, data_type: str, json_flag: bool = False):
1313
self.data = data
1414
self.json = json_flag
15+
self.data_type = data_type
1516

1617
def __call__(self, *args, **kwargs):
1718
if self.json:
1819
return JSONViewer(self.data).run()
1920
else:
20-
return TreeViewer(self.data).run()
21+
return TreeViewer(self.data, self.data_type).run()
2122

2223

2324
class JSONViewer(TUIApp):
@@ -38,17 +39,18 @@ class TreeViewer(TUIApp):
3839
# This has been altered very little from the Textual 'JSON tree' Example:
3940
# https: // github.com / Textualize / textual / blob / main / examples / json_tree.py
4041

41-
def __init__(self, data: str):
42+
def __init__(self, data: str, data_type: str):
4243
super().__init__()
4344
self.data = json.loads(data)
45+
self.data_type = data_type
4446

4547
def compose(self) -> ComposeResult:
4648
yield Header()
4749
yield Footer()
48-
yield Tree("Root")
50+
yield Tree("Codat API Response")
4951

5052
@classmethod
51-
def add_json(cls, node: TreeNode, json_data: object) -> None:
53+
def add_json(cls, node: TreeNode, json_data: object, data_type: str) -> None:
5254
"""Adds JSON data to a node.
5355
Args:
5456
node (TreeNode): A Tree node.
@@ -86,10 +88,10 @@ def add_node(name: str, node: TreeNode, data: object) -> None:
8688
label = Text(repr(data))
8789
node.set_label(label)
8890

89-
add_node("JSON", node, json_data)
91+
add_node(data_type, node, json_data)
9092

9193
def on_mount(self) -> None:
9294
tree = self.query_one(Tree)
9395
json_node = tree.root.add("JSON")
94-
self.add_json(json_node, self.data)
96+
self.add_json(json_node, self.data, self.data_type)
9597
tree.root.expand()

0 commit comments

Comments
 (0)