[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [nyx/master] Allow arguments in MenuItem constructor



commit 03aa68d2dd7622c55061a4df4caa390e41bc5d76
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Aug 24 08:26:28 2016 -0700

    Allow arguments in MenuItem constructor
    
    We use partials quite a bit when constructing MenuItems. Better to just include
    this in the constructor.
---
 nyx/menu.py  | 33 +++++++++++++++------------------
 test/menu.py | 11 +++++++----
 2 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/nyx/menu.py b/nyx/menu.py
index 71dd738..7a42fd1 100644
--- a/nyx/menu.py
+++ b/nyx/menu.py
@@ -40,13 +40,16 @@ class MenuItem(object):
   :var Submenu submenu: top-level submenu we reside within
   """
 
-  def __init__(self, label, callback):
+  def __init__(self, label, callback, *args):
     self.label = label
     self.suffix = ''
-
-    self._callback = callback
     self._parent = None
 
+    if args:
+      self._callback = functools.partial(callback, *args)
+    else:
+      self._callback = callback
+
   @property
   def prefix(self):
     return ''
@@ -196,14 +199,13 @@ def make_actions_menu():
   actions_menu = Submenu('Actions')
   actions_menu.add(MenuItem('Close Menu', None))
   actions_menu.add(MenuItem('New Identity', header_panel.send_newnym))
-  actions_menu.add(MenuItem('Reset Tor', functools.partial(controller.signal, stem.Signal.RELOAD)))
+  actions_menu.add(MenuItem('Reset Tor', controller.signal, stem.Signal.RELOAD))
 
   if control.is_paused():
-    label, arg = 'Unpause', False
+    actions_menu.add(MenuItem('Unpause', control.set_paused, False))
   else:
-    label, arg = 'Pause', True
+    actions_menu.add(MenuItem('Pause', control.set_paused, True))
 
-  actions_menu.add(MenuItem(label, functools.partial(control.set_paused, arg)))
   actions_menu.add(MenuItem('Exit', control.quit))
 
   return actions_menu
@@ -331,11 +333,9 @@ def make_log_menu(log_panel):
   log_menu.add(MenuItem('Clear', log_panel.clear))
 
   if log_panel.is_duplicates_visible():
-    label, arg = 'Hide', False
+    log_menu.add(MenuItem('Hide Duplicates', log_panel.set_duplicate_visability, False))
   else:
-    label, arg = 'Show', True
-
-  log_menu.add(MenuItem('%s Duplicates' % label, functools.partial(log_panel.set_duplicate_visability, arg)))
+    log_menu.add(MenuItem('Show Duplicates', log_panel.set_duplicate_visability, True))
 
   # filter submenu
 
@@ -418,17 +418,14 @@ def make_torrc_menu(torrc_panel):
   torrc_menu = Submenu('Torrc')
 
   if torrc_panel._show_comments:
-    label, arg = 'Hide', False
+    torrc_menu.add(MenuItem('Hide Comments', torrc_panel.set_comments_visible, False))
   else:
-    label, arg = 'Show', True
-
-  torrc_menu.add(MenuItem('%s Comments' % label, functools.partial(torrc_panel.set_comments_visible, arg)))
+    torrc_menu.add(MenuItem('Show Comments', torrc_panel.set_comments_visible, True))
 
   if torrc_panel._show_line_numbers:
-    label, arg = 'Hide', False
+    torrc_menu.add(MenuItem('Hide Line Numbers', torrc_panel.set_line_number_visible, False))
   else:
-    label, arg = 'Show', True
-  torrc_menu.add(MenuItem('%s Line Numbers' % label, functools.partial(torrc_panel.set_line_number_visible, arg)))
+    torrc_menu.add(MenuItem('Show Line Numbers', torrc_panel.set_line_number_visible, True))
 
   return torrc_menu
 
diff --git a/test/menu.py b/test/menu.py
index fdf3352..2aa0e4b 100644
--- a/test/menu.py
+++ b/test/menu.py
@@ -11,11 +11,11 @@ class Container(object):
   value = False
 
   def __nonzero__(self):
-    return self.value
+    return bool(self.value)
 
 
 def action(*args):
-  IS_CALLED.value = True
+  IS_CALLED.value = args if args else True
 
 
 NO_OP = lambda: None
@@ -40,11 +40,14 @@ class TestMenuItem(unittest.TestCase):
 
   def test_selection(self):
     menu_item = MenuItem('Test Item', action)
-
-    self.assertFalse(IS_CALLED)
     menu_item.select()
     self.assertTrue(IS_CALLED)
 
+  def test_selection_with_value(self):
+    menu_item = MenuItem('Test Item', action, 'hi')
+    menu_item.select()
+    self.assertEqual(('hi',), IS_CALLED.value)
+
   def test_menu_item_hierarchy(self):
     root_submenu = Submenu('Root Submenu')
     middle_submenu = Submenu('Middle Submenu')



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits