[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [nyx/master] Make next and prev attributes
commit 576f00167e8497e3fe4910d8b3f7ea790a50ce13
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Mon Aug 22 10:32:19 2016 -0700
Make next and prev attributes
Making these attributes so it matches parent and the rest.
---
nyx/menu.py | 62 ++++++++++++++++++++++++------------------------------------
test/menu.py | 17 +++++++++++++----
2 files changed, 38 insertions(+), 41 deletions(-)
diff --git a/nyx/menu.py b/nyx/menu.py
index f137535..40b3495 100644
--- a/nyx/menu.py
+++ b/nyx/menu.py
@@ -34,6 +34,8 @@ class MenuItem(object):
:var str label: text we display
:var str suffix: text after our label
+ :var MenuItem next: menu item after this one
+ :var MenuItem prev: menu item before this one
:var Submenu parent: submenu we reside within
:var Submenu submenu: top-level submenu we reside within
"""
@@ -50,6 +52,14 @@ class MenuItem(object):
return ''
@property
+ def next(self):
+ return self._sibling(1)
+
+ @property
+ def prev(self):
+ return self._sibling(-1)
+
+ @property
def parent(self):
return self._parent
@@ -65,45 +75,23 @@ class MenuItem(object):
if self._callback:
self._callback()
- def next(self):
- """
- Provides the next option for the submenu we're in, raising a ValueError
- if we don't have a parent.
- """
-
- return self._get_sibling(1)
-
- def prev(self):
+ def _sibling(self, offset):
"""
- Provides the previous option for the submenu we're in, raising a ValueError
- if we don't have a parent.
+ Provides sibling with a given offset from us.
"""
- return self._get_sibling(-1)
+ if not self._parent:
+ return None
- def _get_sibling(self, offset):
- """
- Provides our sibling with a given index offset from us, raising a
- ValueError if we don't have a parent.
+ my_siblings = self._parent.get_children()
- Arguments:
- offset - index offset for the sibling to be returned
- """
-
- if self._parent:
- my_siblings = self._parent.get_children()
+ try:
+ my_index = my_siblings.index(self)
+ return my_siblings[(my_index + offset) % len(my_siblings)]
+ except ValueError:
+ # submenus and children should have bidirectional references
- try:
- my_index = my_siblings.index(self)
- return my_siblings[(my_index + offset) % len(my_siblings)]
- except ValueError:
- # We expect a bidirectional references between submenus and their
- # children. If we don't have this then our menu's screwed up.
-
- msg = "The '%s' submenu doesn't contain '%s' (children: '%s')" % (self, self._parent, "', '".join(my_siblings))
- raise ValueError(msg)
- else:
- raise ValueError("Menu option '%s' doesn't have a parent" % self)
+ raise ValueError("BUG: The '%s' submenu doesn't contain '%s' (children: '%s')" % (self._parent, self.label, "', '".join(my_siblings)))
class Submenu(MenuItem):
@@ -488,14 +476,14 @@ class MenuCursor:
self._selection.select()
self._is_done = True
elif key.match('up'):
- self._selection = self._selection.prev()
+ self._selection = self._selection.prev
elif key.match('down'):
- self._selection = self._selection.next()
+ self._selection = self._selection.next
elif key.match('left'):
if self._selection.parent == self._selection.submenu:
# shift to the previous main submenu
- prev_submenu = self._selection.submenu.prev()
+ prev_submenu = self._selection.submenu.prev
self._selection = prev_submenu.get_children()[0]
else:
# go up a submenu level
@@ -510,7 +498,7 @@ class MenuCursor:
else:
# shift to the next main submenu
- next_submenu = self._selection.submenu.next()
+ next_submenu = self._selection.submenu.next
self._selection = next_submenu.get_children()[0]
elif key.match('esc', 'm'):
self._is_done = True
diff --git a/test/menu.py b/test/menu.py
index f0de66f..4b03a40 100644
--- a/test/menu.py
+++ b/test/menu.py
@@ -16,6 +16,9 @@ class TestMenu(unittest.TestCase):
self.assertEqual('', menu_item.prefix)
self.assertEqual('Test Item', menu_item.label)
self.assertEqual('', menu_item.suffix)
+
+ self.assertEqual(None, menu_item.next)
+ self.assertEqual(None, menu_item.prev)
self.assertEqual(None, menu_item.parent)
self.assertEqual(menu_item, menu_item.submenu)
@@ -25,6 +28,9 @@ class TestMenu(unittest.TestCase):
self.assertEqual('', menu_item.prefix)
self.assertEqual('Test Item', menu_item.label)
self.assertEqual(' >', menu_item.suffix)
+
+ self.assertEqual(None, menu_item.next)
+ self.assertEqual(None, menu_item.prev)
self.assertEqual(None, menu_item.parent)
self.assertEqual(menu_item, menu_item.submenu)
@@ -35,6 +41,9 @@ class TestMenu(unittest.TestCase):
self.assertEqual('[X] ', menu_item.prefix)
self.assertEqual('Test Item', menu_item.label)
self.assertEqual('', menu_item.suffix)
+
+ self.assertEqual(None, menu_item.next)
+ self.assertEqual(None, menu_item.prev)
self.assertEqual(None, menu_item.parent)
self.assertEqual(menu_item, menu_item.submenu)
@@ -51,10 +60,10 @@ class TestMenu(unittest.TestCase):
self.assertEqual(middle_submenu, bottom_item.parent)
self.assertEqual(middle_submenu, bottom_item.submenu)
- self.assertEqual(bottom_item, bottom_item.next())
- self.assertEqual(bottom_item, bottom_item.prev())
+ self.assertEqual(bottom_item, bottom_item.next)
+ self.assertEqual(bottom_item, bottom_item.prev)
self.assertEqual(root_submenu, middle_submenu.parent)
self.assertEqual(middle_submenu, middle_submenu.submenu)
- self.assertEqual('Middle Item 1', middle_submenu.next().label)
- self.assertEqual('Middle Item 2', middle_submenu.prev().label)
+ self.assertEqual('Middle Item 1', middle_submenu.next.label)
+ self.assertEqual('Middle Item 2', middle_submenu.prev.label)
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits