commit 738cdd92c249f3bb951f2e072d0ac4c0b2aca03d Author: Damian Johnson atagar@torproject.org Date: Tue Sep 13 10:01:42 2016 -0700
Move page methods to Interface
Pretty direct move. Renaming get_page_count() and dropping next_page() and prev_page() but otherwise just cut-and-paste. --- nyx/__init__.py | 40 ++++++++++++++++++++++++++++++++ nyx/controller.py | 64 ++++++++-------------------------------------------- nyx/menu.py | 2 +- nyx/panel/header.py | 2 +- test/panel/header.py | 4 ++-- 5 files changed, 53 insertions(+), 59 deletions(-)
diff --git a/nyx/__init__.py b/nyx/__init__.py index 4a031c7..c7a9b47 100644 --- a/nyx/__init__.py +++ b/nyx/__init__.py @@ -15,6 +15,10 @@ Tor curses monitoring application. msg - string from our configuration
Interface - overall nyx interface + |- get_page - page we're showing + |- set_page - sets the page we're showing + |- page_count - pages within our interface + | |- is_paused - checks if the interface is paused +- set_paused - sets paused state """ @@ -218,8 +222,44 @@ class Interface(object): """
def __init__(self): + self._page = 0 self._paused = False
+ def get_page(self): + """ + Provides the page we're showing. + + :return: **int** of the page we're showing + """ + + return self._page + + def set_page(self, page_number): + """ + Sets the selected page. + + :param int page_number: page to be shown + + :raises: **ValueError** if the page_number is invalid + """ + + if page_number < 0 or page_number >= self.page_count(): + raise ValueError('Invalid page number: %i' % page_number) + + if page_number != self._page: + self._page = page_number + self._force_redraw = True + self.header_panel().redraw() + + def page_count(self): + """ + Provides the number of pages the interface has. + + :returns: **int** number of pages in the interface + """ + + return len(self._page_panels) + def is_paused(self): """ Checks if the interface is configured to be paused. diff --git a/nyx/controller.py b/nyx/controller.py index 42553b4..8e1f6f3 100644 --- a/nyx/controller.py +++ b/nyx/controller.py @@ -103,7 +103,6 @@ class Controller(Interface): self._page_panels = [] self._header_panel = None self.quit_signal = False - self._page = 0 self._force_redraw = False self._last_drawn = 0
@@ -137,51 +136,6 @@ class Controller(Interface): self._force_redraw = False self._last_drawn = 0
- def get_page_count(self): - """ - Provides the number of pages the interface has. This may be zero if all - page panels have been disabled. - """ - - return len(self._page_panels) - - def get_page(self): - """ - Provides the number belonging to this page. Page numbers start at zero. - """ - - return self._page - - def set_page(self, page_number): - """ - Sets the selected page, raising a ValueError if the page number is invalid. - - Arguments: - page_number - page number to be selected - """ - - if page_number < 0 or page_number >= self.get_page_count(): - raise ValueError('Invalid page number: %i' % page_number) - - if page_number != self._page: - self._page = page_number - self._force_redraw = True - self.header_panel().redraw() - - def next_page(self): - """ - Increments the page number. - """ - - self.set_page((self._page + 1) % len(self._page_panels)) - - def prev_page(self): - """ - Decrements the page number. - """ - - self.set_page((self._page - 1) % len(self._page_panels)) - def header_panel(self): return self._header_panel
@@ -295,7 +249,7 @@ def start_nyx():
NYX_CONTROLLER = Controller() NYX_CONTROLLER.init() - control = get_controller() + interface = get_controller()
if not CONFIG['features.acsSupport']: nyx.curses.disable_acs() @@ -308,7 +262,7 @@ def start_nyx():
# tells daemon panels to start
- for panel_impl in control.get_daemon_panels(): + for panel_impl in interface.get_daemon_panels(): panel_impl.start()
# logs the initialization time @@ -319,15 +273,15 @@ def start_nyx():
override_key = None # uses this rather than waiting on user input
- while not control.quit_signal: - display_panels = [control.header_panel()] + control.get_display_panels() + while not interface.quit_signal: + display_panels = [interface.header_panel()] + interface.get_display_panels()
# sets panel visability
- for panel_impl in control.get_all_panels(): + for panel_impl in interface.get_all_panels(): panel_impl.set_visible(panel_impl in display_panels)
- control.redraw() + interface.redraw()
with nyx.curses.raw_screen() as stdscr: stdscr.refresh() @@ -340,11 +294,11 @@ def start_nyx(): key = nyx.curses.key_input(CONFIG['features.redrawRate'])
if key.match('right'): - control.next_page() + interface.set_page((interface.get_page() + 1) % interface.page_count()) elif key.match('left'): - control.prev_page() + interface.set_page((interface.get_page() - 1) % interface.page_count()) elif key.match('p'): - control.set_paused(not control.is_paused()) + interface.set_paused(not interface.is_paused()) elif key.match('m'): nyx.menu.show_menu() elif key.match('q'): diff --git a/nyx/menu.py b/nyx/menu.py index 2a5af98..8ebaf4d 100644 --- a/nyx/menu.py +++ b/nyx/menu.py @@ -269,7 +269,7 @@ def _view_menu(): view_menu = Submenu('View') page_group = RadioGroup(interface.set_page, interface.get_page())
- for i in range(interface.get_page_count()): + for i in range(interface.page_count()): page_panels = interface.get_display_panels(page_number = i) label = ' / '.join([type(panel).__name__.replace('Panel', '') for panel in page_panels]) view_menu.add(RadioMenuItem(label, page_group, i)) diff --git a/nyx/panel/header.py b/nyx/panel/header.py index 8b270b3..06ffb90 100644 --- a/nyx/panel/header.py +++ b/nyx/panel/header.py @@ -507,6 +507,6 @@ def _draw_status(subwindow, x, y, is_paused, message, *attr): subwindow.addstr(x, y, message, *attr) elif not is_paused: interface = nyx_interface() - subwindow.addstr(x, y, 'page %i / %i - m: menu, p: pause, h: page help, q: quit' % (interface.get_page() + 1, interface.get_page_count())) + subwindow.addstr(x, y, 'page %i / %i - m: menu, p: pause, h: page help, q: quit' % (interface.get_page() + 1, interface.page_count())) else: subwindow.addstr(x, y, 'Paused', HIGHLIGHT) diff --git a/test/panel/header.py b/test/panel/header.py index 416e99d..6a9ba3f 100644 --- a/test/panel/header.py +++ b/test/panel/header.py @@ -75,7 +75,7 @@ class TestHeaderPanel(unittest.TestCase): def test_rendering_panel(self, sampling_mock, tor_controller_mock, nyx_interface_mock): nyx_interface_mock().is_paused.return_value = False nyx_interface_mock().get_page.return_value = 1 - nyx_interface_mock().get_page_count.return_value = 4 + nyx_interface_mock().page_count.return_value = 4 sampling_mock.return_value = test_sampling()
panel = nyx.panel.header.HeaderPanel() @@ -344,7 +344,7 @@ class TestHeaderPanel(unittest.TestCase): @patch('nyx.panel.header.nyx_interface') def test_draw_status(self, nyx_interface_mock): nyx_interface_mock().get_page.return_value = 1 - nyx_interface_mock().get_page_count.return_value = 4 + nyx_interface_mock().page_count.return_value = 4
self.assertEqual('page 2 / 4 - m: menu, p: pause, h: page help, q: quit', test.render(nyx.panel.header._draw_status, 0, 0, False, None).content) self.assertEqual('Paused', test.render(nyx.panel.header._draw_status, 0, 0, True, None).content)