-
Notifications
You must be signed in to change notification settings - Fork 1
/
miller_demo.py
408 lines (312 loc) · 12.7 KB
/
miller_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from collections import namedtuple
from itertools import chain
from org_graph import (
count_descendant_accounts,
iter_descendant_accounts,
read_graphml,
get_root,
)
from typing import Dict, Iterable, List, Optional, Tuple, cast
import tkinter as tk
import networkx as nx # type: ignore[import]
from tkinter import ttk
AccountRow = namedtuple(
"AccountRow", ["Id", "Name", "Email", "Arn", "Status", "JoinedMethod"]
)
def main() -> None:
app = BrowserApp()
app.mainloop()
class PathSelection:
_root: str
_org: nx.DiGraph
_components: List[str]
def __init__(self, org: nx.DiGraph) -> None:
self._org = org
self._root = get_root(org)
self._components = [self._root]
@property
def components(self) -> List[str]:
return self._components
def get_children(self, parent: Optional[str]) -> List[str]:
if parent is None:
return [self._root]
return list(self._org[parent])
def update_selection(self, depth: int, node: str) -> None:
if depth > len(self._components):
raise ValueError(f"Can't set {depth=} when {len(self._components)=}")
if depth < len(self._components) and self._components[depth] == node:
return
previous_component = self._components[depth - 1]
children_of_previous_component = self.get_children(previous_component)
if node not in children_of_previous_component:
raise ValueError(f"{node} is not a child of {previous_component}")
self._components[depth:] = [node]
NavIterable = Iterable[Tuple[int, Optional[str], Optional[str], List[str]]]
def iter_navegable_nodes(self) -> NavIterable:
selected_ancestors = chain([None], self._components)
selected_descendants = chain(self._components, [None])
nav_iterator = enumerate(zip(selected_ancestors, selected_descendants))
for depth, (parent, selected_child) in nav_iterator:
children = self.get_children(parent)
yield depth, parent, selected_child, children
class BrowserController:
def __init__(
self,
path: PathSelection,
org: nx.DiGraph,
miller_view: "MillerView",
account_table_view: "AccountTableView",
resource_detail_view: "ResourceDetailView",
):
self.org = org
self.path = path
self.miller_view = miller_view
self.account_table_view = account_table_view
self.resource_detail_view = resource_detail_view
def update_selection(self, depth: int, node: str) -> None:
self.path.update_selection(depth, node)
self.show_path_in_view()
self.show_descendant_accounts_in_table()
def show_path_in_view(self) -> None:
self.miller_view.clear_all_columns()
# TODO: Remove this if it doesn't look good.
# self.miller_view.hide_all_columns()
nav_iterator = self.path.iter_navegable_nodes()
for depth, parent, selected_child, children in nav_iterator:
# TODO: Don't test the node value to format it correctly.
# iter_navegable_nodes should return all the node data. Then here it
# should test the node type to generate an appropriate item for each
# node. Call clear_column and append_column here instead of
# fill_column. Allow controller to call MillerColumn methods
# directly.
def format_count(node: str) -> str:
if node.startswith("ou-") or node.startswith("r-"):
return str(count_descendant_accounts(self.org, node))
return ""
descendants_per_child = [format_count(p) for p in children]
names_per_child = [self.org.nodes[p]["Name"] for p in children]
self.miller_view.show_column(depth)
self.miller_view.fill_column(
depth,
children,
names_per_child,
descendants_per_child
)
# TODO: Remove this definitely if I stick with the account count
# column.
# if parent is None:
# self.miller_view.set_column_name(depth, "")
# else:
# self.miller_view.set_column_name(depth, parent)
if selected_child is None:
self.miller_view.clear_column_selection(depth)
else:
self.miller_view.set_column_selection(depth, selected_child)
def show_descendant_accounts_in_table(self) -> None:
account_ids = iter_descendant_accounts(self.org, self.path.components[-1])
account_datas = (self.org.nodes[id] for id in account_ids)
rows = (
AccountRow(**{k: v for k, v in data.items() if k in AccountRow._fields})
for data in account_datas
)
self.account_table_view.fill(rows)
def update_resource_detail_view(self, selection: str) -> None:
self.resource_detail_view.clear()
if selection is None:
return
detail = cast(Dict[str, str], self.org.nodes[selection])
for key, value in detail.items():
self.resource_detail_view.add_detail(key, value)
class MillerColumn(ttk.Treeview):
def __init__(self, parent: "MillerView") -> None:
super().__init__(
parent,
columns=["Name", "Accounts"],
displaycolumns="#all",
show=["headings"]
)
self.heading("Name", text="Name")
self.heading("Accounts", text="#")
self.column("Name", width=160, stretch=True)
self.column("Accounts", width=40, stretch=False)
# TODO: Replace ButtonRelease with TreeviewSelect. Fix the infinite loop
# that it causes. The call sequence that might trigger it:
#
# * MillerView.on_click_column
# * BrowserController.update_selection
# * BrowserController.show_path_in_view
# * MillerView.clear_column_selection
self.bind("<<TreeviewSelect>>", parent.on_click_column)
def clear(self) -> None:
for c in self.get_children():
self.delete(c)
def fill(
self,
items: List[str],
names: List[str],
descendants_per_child: List[str],
) -> None:
self.clear()
for item, name, descendants in zip(items, names, descendants_per_child):
self.insert(
parent="",
index="end",
iid=item,
text=name,
values=(name, descendants),
)
def first_selection(self) -> Optional[str]:
selection = self.selection()
if not selection:
return None
return selection[0]
class MillerView(ttk.Frame):
controller: BrowserController
columns: List[MillerColumn]
def __init__(self, parent: tk.Misc, max_depth: int):
super().__init__(parent)
self.grid()
self.grid_rowconfigure(0, weight=1)
self.columns = []
for i in range(max_depth):
self.add_column()
self.show_column(i)
self.grid_columnconfigure(i, weight=1)
def set_controller(self, controller: BrowserController) -> None:
self.controller = controller
def add_column(self) -> None:
self.columns.append(MillerColumn(self))
def set_column_name(self, depth: int, name: str) -> None:
self.columns[depth].heading("Name", text=name)
def clear_column_selection(self, depth: int) -> None:
self.columns[depth].selection_clear()
def set_column_selection(self, depth: int, iid: str) -> None:
self.columns[depth].selection_set(iid)
def fill_column(
self,
depth: int,
children: List[str],
names: List[str],
descendants_per_child: List[str]
) -> None:
self.columns[depth].fill(children, names, descendants_per_child)
def show_column(self, depth: int) -> None:
self.columns[depth].grid(column=depth, row=0, sticky="NSEW")
self.grid_columnconfigure(depth, weight=1)
def hide_column(self, depth: int) -> None:
self.columns[depth].grid_forget()
def max_depth(self) -> int:
return len(self.columns)
def on_click_column(self, event: "tk.Event[MillerColumn]") -> None:
if not self.controller:
return
column = event.widget
depth = self.columns.index(column)
selection = column.first_selection()
if selection is not None:
self.controller.update_selection(depth, selection)
def clear_all_columns(self) -> None:
for column in self.columns:
column.clear()
def hide_all_columns(self) -> None:
for index in range(len(self.columns)):
self.hide_column(index)
class AccountTableView(ttk.Frame):
controller: BrowserController
table: ttk.Treeview
def __init__(self, parent: tk.Misc) -> None:
super().__init__(parent)
columns = AccountRow._fields
self.table = ttk.Treeview(
self,
columns=columns,
displaycolumns="#all",
show=["headings"],
)
for c in AccountRow._fields:
self.table.heading(c, text=c)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.table.grid(column=0, row=0, sticky="NSEW")
self.table.bind("<<TreeviewSelect>>", self.on_click) # type: ignore[arg-type]
def set_controller(self, controller: BrowserController) -> None:
self.controller = controller
def clear(self) -> None:
for r in self.table.get_children():
self.table.delete(r)
def fill(self, accounts: Iterable[AccountRow]) -> None:
self.clear()
for account in accounts:
self.table.insert(
parent="",
index="end",
iid=account.Id,
text=account.Id,
values=account,
)
def first_selection(self) -> Optional[str]:
selection = self.table.selection()
if not selection:
return None
return selection[0]
def on_click(self, event: "tk.Event[AccountTableView]") -> None:
if not self.controller:
return
selection = self.first_selection()
self.controller.update_resource_detail_view(selection) # type: ignore[arg-type]
class ResourceDetailView(ttk.Frame):
resource_name: ttk.Label
tag_table: ttk.Treeview
def __init__(self, parent: tk.Misc) -> None:
super().__init__(parent)
self.resource_name = ttk.Label(self, text="Account 111111111111")
columns = ["Key", "Value"]
self.tag_table = ttk.Treeview(
self,
columns=columns,
displaycolumns="#all",
show=["headings"],
)
for c in columns:
self.tag_table.heading(c, text=c)
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(1, weight=1)
self.resource_name.grid(column=0, row=0, sticky="NSEW")
self.tag_table.grid(column=0, row=1, sticky="NSEW")
def clear(self) -> None:
for r in self.tag_table.get_children():
self.tag_table.delete(r)
def add_detail(self, key: str, value: str) -> None:
self.tag_table.insert(parent="", index="end", iid=key, values=(key, value))
class BrowserApp(tk.Tk):
def __init__(self) -> None:
super().__init__()
self.bind_quit_events()
self.grid()
self.grid_rowconfigure(0, weight=1)
self.miller_view = MillerView(self, 7)
self.miller_view.grid(column=0, row=0, sticky="NSEW")
self.grid_rowconfigure(1, weight=1)
self.table_view = AccountTableView(self)
self.table_view.grid(column=0, row=1, sticky="NSEW")
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1, minsize=300)
self.tag_table = ResourceDetailView(self)
self.tag_table.grid(column=1, row=0, rowspan=2, sticky="NSEW")
self.org_tree = read_graphml("/home/isme/tmp/int_org.graphml")
self.path = PathSelection(org=self.org_tree)
self.controller = BrowserController(
self.path, self.org_tree, self.miller_view, self.table_view, self.tag_table
)
self.miller_view.set_controller(self.controller)
self.table_view.set_controller(self.controller)
self.controller.show_path_in_view()
self.controller.show_descendant_accounts_in_table()
def bind_quit_events(self) -> None:
self.bind_all("<Alt-KeyPress-F4>", self.on_quit)
self.bind_all("<Alt-KeyPress-q>", self.on_quit)
def on_quit(self, event: "tk.Event[tk.Misc]") -> None:
self.destroy()
if __name__ == "__main__":
main()