Last active
August 29, 2015 14:23
-
-
Save johanfforsberg/41d74b8c8abdcb62337c to your computer and use it in GitHub Desktop.
Non-working example of removing a branch in an urwidtree widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import urwid | |
from urwidtrees.tree import Tree | |
from urwidtrees.widgets import TreeBox | |
from urwidtrees.decoration import CollapsibleIndentedTree | |
# define selectable urwid.Text widgets to display paths | |
class FocusableText(urwid.WidgetWrap): | |
"""Widget to display paths lines""" | |
def __init__(self, path): | |
txt = path[-1] | |
t = urwid.Text(txt, wrap="clip") | |
w = urwid.AttrMap(t, 'body', 'focus') | |
urwid.WidgetWrap.__init__(self, w) | |
def selectable(self): | |
return True | |
def keypress(self, size, key): | |
return key | |
def get_path(path, dictlike): | |
for step in path: | |
dictlike = dictlike[step] | |
return dictlike | |
def del_path(path, dictlike): | |
for step in path[:-1]: | |
dictlike = dictlike[step] | |
del dictlike[path[-1]] | |
class MyTree(Tree): | |
def __init__(self, data): | |
self.data = data | |
self.root = ("root",) | |
def __getitem__(self, pos): | |
return FocusableText(pos) | |
# helpers | |
def _get_children(self, path): | |
node = get_path(path, self.data) | |
if node and isinstance(node, dict): | |
children = node.keys() | |
return [path + (child,) for child in sorted(children)] | |
return [] | |
def _get_siblings(self, path): | |
parent = self.parent_position(path) | |
if parent is not None: | |
return self._get_children(parent) | |
return [path] | |
# UrwidTrees Tree API | |
def parent_position(self, path): | |
print >>sys.stderr, "parent_position", path | |
if path != self.root: | |
return path[:-1] | |
def first_child_position(self, path): | |
try: | |
children = self._get_children(path) | |
except KeyError: | |
return path | |
if children: | |
return children[0] | |
def last_child_position(self, path): | |
try: | |
children = self._get_children(path) | |
except KeyError: | |
return path | |
if children: | |
return children[-1] | |
def next_sibling_position(self, path): | |
siblings = self._get_siblings(path) | |
if path in siblings: | |
myindex = siblings.index(path) | |
if myindex + 1 < len(siblings): # path is not the last entry | |
return siblings[myindex + 1] | |
def prev_sibling_position(self, path): | |
siblings = self._get_siblings(path) | |
if path in siblings: | |
myindex = siblings.index(path) | |
if myindex > 0: # path is not the first entry | |
return siblings[myindex - 1] | |
class MyTreeBox(TreeBox): | |
def keypress(self, size, key): | |
"Spicing up the keybindings!" | |
if key == "delete": | |
_, path = self.get_focus() | |
self.set_focus(path[:-1]) | |
del_path(path, treedata) | |
self.refresh() | |
else: | |
return TreeBox.keypress(self, size, key) | |
palette = [ | |
('body', 'black', 'light gray'), | |
('focus', 'light gray', 'dark blue', 'standout'), | |
('bars', 'dark blue', 'light gray', ''), | |
('arrowtip', 'light blue', 'light gray', ''), | |
('connectors', 'light red', 'light gray', ''), | |
] | |
treedata = {"root": {"a": {"1": None, "2": None}, "b": None, "c": None}} | |
if __name__ == "__main__": | |
tree = CollapsibleIndentedTree(MyTree(treedata)) | |
box = MyTreeBox(tree) | |
loop = urwid.MainLoop(box, palette) | |
loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
your bug is in line 32: you forgot to return
dictlike
, so the global variable will not be amended.It is true that the trace is not very telling here, but that's urwids fault i believe :)