[nyx/master] Allow children through the Submenu constructor

commit d15e2f4cfef5fd60a82824eb0c10b8cdb1433dd3 Author: Damian Johnson <atagar@torproject.org> Date: Tue Aug 23 13:37:27 2016 -0700 Allow children through the Submenu constructor Usually we want to use add() but in a couple particularly simple cases it's nicer to just pass them into the constructor. --- nyx/menu.py | 22 +++++++++++++--------- test/menu.py | 9 +++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/nyx/menu.py b/nyx/menu.py index f086ae7..71dd738 100644 --- a/nyx/menu.py +++ b/nyx/menu.py @@ -101,11 +101,15 @@ class Submenu(MenuItem): :var list children: menu items this contains """ - def __init__(self, label): + def __init__(self, label, children = None): MenuItem.__init__(self, label, None) self.suffix = ' >' self.children = [] + if children: + for child in children: + self.add(child) + def add(self, menu_item): """ Adds the given menu item to our listing. @@ -247,10 +251,10 @@ def make_help_menu(): About """ - help_menu = Submenu('Help') - help_menu.add(MenuItem('Hotkeys', nyx.popups.show_help)) - help_menu.add(MenuItem('About', nyx.popups.show_about)) - return help_menu + return Submenu('Help', [ + MenuItem('Hotkeys', nyx.popups.show_help), + MenuItem('About', nyx.popups.show_about), + ]) def make_graph_menu(graph_panel): @@ -394,10 +398,10 @@ def make_configuration_menu(config_panel): config_panel - instance of the configuration panel """ - config_menu = Submenu('Configuration') - config_menu.add(MenuItem('Save Config...', config_panel.show_write_dialog)) - config_menu.add(MenuItem('Sorting...', config_panel.show_sort_dialog)) - return config_menu + return Submenu('Configuration', [ + MenuItem('Save Config...', config_panel.show_write_dialog), + MenuItem('Sorting...', config_panel.show_sort_dialog), + ]) def make_torrc_menu(torrc_panel): diff --git a/test/menu.py b/test/menu.py index f3c0143..fdf3352 100644 --- a/test/menu.py +++ b/test/menu.py @@ -80,6 +80,15 @@ class TestSubmenu(unittest.TestCase): self.assertEqual(None, menu_item.parent) self.assertEqual(menu_item, menu_item.submenu) + self.assertEqual([], menu_item.children) + + menu_item = Submenu('Test Item', [ + MenuItem('Test Item 1', NO_OP), + MenuItem('Test Item 2', NO_OP), + ]) + + self.assertEqual(2, len(menu_item.children)) + def test_add(self): submenu = Submenu('Menu') item_1 = MenuItem('Test Item 1', NO_OP)
participants (1)
-
atagar@torproject.org