This repository has been archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree-visualizer.py
140 lines (119 loc) · 4.16 KB
/
tree-visualizer.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
#!/usr/bin/python
import sys
import json
from math import ceil, floor
from pydbus import SessionBus
BUS = SessionBus()
LAYOUT = BUS.get(bus_name='org.way-cooler', object_path='/org/way_cooler/Layout')
try:
# python 3
import tkinter as tk
except ImportError:
# python 2
import Tkinter as tk
import ttk
class Node():
def __init__(self, name, children):
self.name = name
self.children = children
class Application(ttk.Treeview):
def __init__(self, master=None):
ttk.Treeview.__init__(self, master)
self.pack(fill=tk.BOTH, expand=True)
self.make_root()
def make_root(self):
self.insert("", 0, "root", text="Root", open=True)
self._root = Node("root", [])
def add_output(self):
next_output_num = len(self._root.children) + 1
text = "Output %d" % next_output_num
self.insert("root", next_output_num - 1, text, text=text, open=True)
output_node = Node(text, [])
self._root.children.append(output_node)
return output_node
def add_workspace(self, parent, workspace_name):
text = "Workspace %s" % workspace_name
# hacky, get the number from the parent
parent_num = int(parent.name.split()[-1])
workspace_node = Node(text, [])
parent.children.append(workspace_node)
self.insert("Output %d" % parent_num, len(parent.children), text, text=text,
open=True)
return workspace_node
def add_container(self, parent, layout_type):
text = "Container w/ layout %s" % layout_type
# hacky, get the name from the parent
parent_name = parent.name
container_node = Node(text, [])
parent.children.append(container_node)
name = self.insert(parent_name, len(parent.children), text=text,
open=True)
container_node.name = name
return container_node
def add_view(self, parent, view_name):
text = "View: %s" % view_name
parent_id = parent.name
view_node = Node(text, None)
parent.children.append(view_node)
name = self.insert(parent_id, len(parent.children), text=text)
view_node.name = name
return view_node
def node_count(self):
def count_helper(node):
if node.children is None:
return 1
return 1 + sum([count_helper(child) for child in node.children])
return count_helper(self._root)
root = tk.Tk()
app = Application(master=root)
app.master.title("Tree visualization example")
def update_app(tree):
"""Takes a json representation of the tree and lays it out
in the view"""
global app
def recurse_add_container(json, parent):
if not json:
return
if isinstance(json, list):
for child in json:
recurse_add_container(child, parent)
elif isinstance(json, dict):
for key in json.keys():
if key.startswith("Output"):
parent = app.add_output()
elif key.startswith("Workspace"):
# need to update json from rust side
parent = app.add_workspace(parent, key.split()[-1])
elif key.startswith("Container"):
# need to update json from rust side
parent = app.add_container(parent, key.split()[-1])
else:
app.add_view(parent, json[key])
continue
recurse_add_container(json[key], parent)
else:
app.add_view(parent, json)
recurse_add_container(tree, app._root)
def clear():
global app;
app.destroy()
app = Application(master=root)
running = True
tree = None
def update():
global tree
from time import sleep
while running:
sleep(.1)
buffer = json.loads(LAYOUT.Debug())
if buffer == tree:
continue
print("got: {}".format(buffer))
tree = buffer
clear()
update_app(tree['Root'])
app.configure(height=app.node_count() * 20)
from threading import Thread
update_thread = Thread(target=update).start()
app.mainloop()
running = False