[tor-commits] [nyx/master] nyx_interface() alias

atagar at torproject.org atagar at torproject.org
Fri Sep 16 06:18:14 UTC 2016


commit 40781c080f7a82010b5c1efed55d59d00e6d0881
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Sep 10 12:22:06 2016 -0700

    nyx_interface() alias
    
    Aliasing nyx.control.get_controller() to nyx_interface(). This will later be
    the home for thsi code but for now it's just a terminology change. I'm renaming
    this because we had two 'controllers' for both nyx and tor. With this we now
    work with nyx's 'interface' instead.
---
 nyx/__init__.py         | 11 +++++++++++
 nyx/menu.py             | 26 +++++++++++++-------------
 nyx/panel/__init__.py   |  7 +++----
 nyx/panel/connection.py |  9 ++++-----
 nyx/panel/graph.py      | 16 ++++++----------
 nyx/panel/header.py     | 12 ++++++------
 nyx/panel/log.py        |  5 ++---
 nyx/popups.py           | 10 +++++-----
 nyx/starter.py          |  8 ++++----
 test/panel/header.py    | 18 +++++++++---------
 test/popups.py          |  6 +++---
 11 files changed, 66 insertions(+), 62 deletions(-)

diff --git a/nyx/__init__.py b/nyx/__init__.py
index f2c62f4..1ffd4b0 100644
--- a/nyx/__init__.py
+++ b/nyx/__init__.py
@@ -81,6 +81,17 @@ def main():
     sys.exit(1)
 
 
+def nyx_interface():
+  """
+  Singleton controller for our interface.
+
+  :returns: :class:`~nyx.Interface` controller
+  """
+
+  import nyx.controller
+  return nyx.controller.get_controller()
+
+
 def tor_controller():
   """
   Singleton for getting our tor controller connection.
diff --git a/nyx/menu.py b/nyx/menu.py
index 8c60be9..2a5af98 100644
--- a/nyx/menu.py
+++ b/nyx/menu.py
@@ -13,7 +13,7 @@ import nyx.popups
 
 import stem
 
-from nyx import tor_controller
+from nyx import nyx_interface, tor_controller
 from nyx.curses import RED, WHITE, NORMAL, BOLD, UNDERLINE
 from stem.util import str_tools
 
@@ -211,7 +211,7 @@ def show_menu():
       selection_x = _draw_top_menubar(menu, cursor.selection)
       _draw_submenu(cursor.selection, cursor.selection.submenu, 1, selection_x)
       cursor.handle_key(nyx.curses.key_input())
-      nyx.controller.get_controller().redraw()
+      nyx_interface().redraw()
 
     nyx.controller.show_message()
 
@@ -221,26 +221,26 @@ def _make_menu():
   Constructs the base menu and all of its contents.
   """
 
-  nyx_controller = nyx.controller.get_controller()
+  interface = nyx_interface()
 
-  if not nyx_controller.is_paused():
-    pause_item = MenuItem('Pause', nyx_controller.set_paused, True)
+  if not interface.is_paused():
+    pause_item = MenuItem('Pause', interface.set_paused, True)
   else:
-    pause_item = MenuItem('Unpause', nyx_controller.set_paused, False)
+    pause_item = MenuItem('Unpause', interface.set_paused, False)
 
   root_menu = Submenu('')
 
   root_menu.add(Submenu('Actions', [
     MenuItem('Close Menu', None),
-    MenuItem('New Identity', nyx_controller.header_panel().send_newnym),
+    MenuItem('New Identity', interface.header_panel().send_newnym),
     MenuItem('Reset Tor', tor_controller().signal, stem.Signal.RELOAD),
     pause_item,
-    MenuItem('Exit', nyx_controller.quit),
+    MenuItem('Exit', interface.quit),
   ]))
 
   root_menu.add(_view_menu())
 
-  for panel in nyx_controller.get_display_panels():
+  for panel in interface.get_display_panels():
     submenu = panel.submenu()
 
     if submenu:
@@ -264,13 +264,13 @@ def _view_menu():
         Color (Submenu)
   """
 
-  nyx_controller = nyx.controller.get_controller()
+  interface = nyx_interface()
 
   view_menu = Submenu('View')
-  page_group = RadioGroup(nyx_controller.set_page, nyx_controller.get_page())
+  page_group = RadioGroup(interface.set_page, interface.get_page())
 
-  for i in range(nyx_controller.get_page_count()):
-    page_panels = nyx_controller.get_display_panels(page_number = i)
+  for i in range(interface.get_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/__init__.py b/nyx/panel/__init__.py
index 18a66ce..5714db4 100644
--- a/nyx/panel/__init__.py
+++ b/nyx/panel/__init__.py
@@ -33,6 +33,8 @@ import time
 
 import nyx.curses
 
+from nyx import nyx_interface
+
 __all__ = [
   'config',
   'connection',
@@ -195,13 +197,10 @@ class DaemonPanel(Panel, threading.Thread):
     Performs our _update() action at the given rate.
     """
 
-    import nyx.controller
-
     last_ran = -1
-    nyx_controller = nyx.controller.get_controller()
 
     while not self._halt:
-      if nyx_controller.is_paused() or (time.time() - last_ran) < self._update_rate:
+      if nyx_interface().is_paused() or (time.time() - last_ran) < self._update_rate:
         with self._pause_condition:
           if not self._halt:
             self._pause_condition.wait(0.2)
diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py
index 16175d9..f8c3a7d 100644
--- a/nyx/panel/connection.py
+++ b/nyx/panel/connection.py
@@ -11,15 +11,14 @@ import collections
 import curses
 import itertools
 
-import nyx.controller
 import nyx.curses
 import nyx.panel
 import nyx.popups
 import nyx.tracker
 
+from nyx import nyx_interface, tor_controller
 from nyx.curses import WHITE, NORMAL, BOLD, HIGHLIGHT
 from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup
-from nyx import tor_controller
 
 from stem.control import Listener
 from stem.util import datetime_to_unix, conf, connection, enum, str_tools
@@ -418,7 +417,7 @@ class ConnectionPanel(nyx.panel.DaemonPanel):
 
   def _draw(self, subwindow):
     controller = tor_controller()
-    nyx_controller = nyx.controller.get_controller()
+    interface = nyx_interface()
     entries = self._entries
 
     lines = list(itertools.chain.from_iterable([entry.get_lines() for entry in entries]))
@@ -426,8 +425,8 @@ class ConnectionPanel(nyx.panel.DaemonPanel):
     details_offset = DETAILS_HEIGHT + 1 if is_showing_details else 0
     selected, scroll = self._scroller.selection(lines, subwindow.height - details_offset - 1)
 
-    if nyx_controller.is_paused():
-      current_time = nyx_controller.get_pause_time()
+    if interface.is_paused():
+      current_time = interface.get_pause_time()
     elif not controller.is_alive():
       current_time = controller.connection_time()
     else:
diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py
index 70b8b60..76d4bfa 100644
--- a/nyx/panel/graph.py
+++ b/nyx/panel/graph.py
@@ -24,7 +24,7 @@ import nyx.panel
 import nyx.popups
 import nyx.tracker
 
-from nyx import join, msg, tor_controller
+from nyx import join, msg, nyx_interface, tor_controller
 from nyx.curses import RED, GREEN, CYAN, BOLD, HIGHLIGHT
 from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup
 from stem.control import EventType, Listener
@@ -452,9 +452,8 @@ class GraphPanel(nyx.panel.Panel):
     if not self._displayed_stat:
       return 0
 
-    nyx_controller = nyx.controller.get_controller()
     height = DEFAULT_CONTENT_HEIGHT + self._graph_height
-    accounting_stats = self._accounting_stats if nyx_controller.is_paused() else self._accounting_stats_paused
+    accounting_stats = self._accounting_stats if nyx_interface().is_paused() else self._accounting_stats_paused
 
     if self._displayed_stat == GraphStat.BANDWIDTH and accounting_stats:
       height += 3
@@ -495,7 +494,7 @@ class GraphPanel(nyx.panel.Panel):
           elif key.is_selection():
             break
 
-          nyx.controller.get_controller().redraw()
+          nyx_interface().redraw()
       finally:
         nyx.controller.show_message()
 
@@ -556,9 +555,7 @@ class GraphPanel(nyx.panel.Panel):
     if not self._displayed_stat:
       return
 
-    nyx_controller = nyx.controller.get_controller()
-
-    if not nyx_controller.is_paused():
+    if not nyx_interface().is_paused():
       stat = self._stats[self._displayed_stat]
       accounting_stats = self._accounting_stats
     else:
@@ -582,16 +579,15 @@ class GraphPanel(nyx.panel.Panel):
     if not CONFIG['features.graph.bw.accounting.show']:
       self._accounting_stats = None
     elif not self._accounting_stats or time.time() - self._accounting_stats.retrieved >= ACCOUNTING_RATE:
-      nyx_controller = nyx.controller.get_controller()
       old_accounting_stats = self._accounting_stats
       self._accounting_stats = tor_controller().get_accounting_stats(None)
 
-      if not nyx_controller.is_paused():
+      if not nyx_interface().is_paused():
         # if we either added or removed accounting info then redraw the whole
         # screen to account for resizing
 
         if bool(old_accounting_stats) != bool(self._accounting_stats):
-          nyx.controller.get_controller().redraw()
+          nyx_interface().redraw()
 
   def _update_stats(self, event):
     for stat in self._stats.values():
diff --git a/nyx/panel/header.py b/nyx/panel/header.py
index da13ff3..2165c3a 100644
--- a/nyx/panel/header.py
+++ b/nyx/panel/header.py
@@ -23,7 +23,7 @@ import nyx.popups
 import nyx.tracker
 
 from stem.util import conf, log
-from nyx import msg, tor_controller
+from nyx import msg, nyx_interface, tor_controller
 
 from nyx.curses import RED, GREEN, YELLOW, CYAN, WHITE, BOLD, HIGHLIGHT
 
@@ -150,10 +150,10 @@ class HeaderPanel(nyx.panel.DaemonPanel):
 
     # space available for content
 
-    nyx_controller = nyx.controller.get_controller()
+    interface = nyx_interface()
     left_width = max(subwindow.width / 2, 77) if is_wide else subwindow.width
     right_width = subwindow.width - left_width
-    pause_time = nyx_controller.get_pause_time() if nyx_controller.is_paused() else None
+    pause_time = interface.get_pause_time() if interface.is_paused() else None
 
     _draw_platform_section(subwindow, 0, 0, left_width, vals)
 
@@ -178,7 +178,7 @@ class HeaderPanel(nyx.panel.DaemonPanel):
         _draw_fingerprint_and_fd_usage(subwindow, 0, 3, left_width, vals)
         _draw_flags(subwindow, 0, 4, vals.flags)
 
-    _draw_status(subwindow, 0, self.get_height() - 1, nyx_controller.is_paused(), self._message, *self._message_attr)
+    _draw_status(subwindow, 0, self.get_height() - 1, interface.is_paused(), self._message, *self._message_attr)
 
   def _reset_listener(self, controller, event_type, _):
     self._update()
@@ -502,7 +502,7 @@ def _draw_status(subwindow, x, y, is_paused, message, *attr):
   if message:
     subwindow.addstr(x, y, message, *attr)
   elif not is_paused:
-    controller = nyx.controller.get_controller()
-    subwindow.addstr(x, y, 'page %i / %i - m: menu, p: pause, h: page help, q: quit' % (controller.get_page() + 1, controller.get_page_count()))
+    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()))
   else:
     subwindow.addstr(x, y, 'Paused', HIGHLIGHT)
diff --git a/nyx/panel/log.py b/nyx/panel/log.py
index af793aa..ca331ab 100644
--- a/nyx/panel/log.py
+++ b/nyx/panel/log.py
@@ -20,7 +20,7 @@ import nyx.panel
 import nyx.popups
 import nyx.log
 
-from nyx import join, tor_controller
+from nyx import join, nyx_interface, tor_controller
 from nyx.curses import GREEN, YELLOW, WHITE, NORMAL, BOLD, HIGHLIGHT
 from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup
 from stem.util import conf, log
@@ -263,13 +263,12 @@ class LogPanel(nyx.panel.DaemonPanel):
   def _draw(self, subwindow):
     scroll = self._scroller.location(self._last_content_height, subwindow.height - 1)
 
-    nyx_controller = nyx.controller.get_controller()
     event_filter = self._filter.clone()
     event_types = list(self._event_types)
     last_content_height = self._last_content_height
     show_duplicates = self._show_duplicates
 
-    event_log = self._event_log_paused if nyx_controller.is_paused() else self._event_log
+    event_log = self._event_log_paused if nyx_interface().is_paused() else self._event_log
     event_log = list(filter(lambda entry: event_filter.match(entry.display_message), event_log))
     event_log = list(filter(lambda entry: not entry.is_duplicate or show_duplicates, event_log))
 
diff --git a/nyx/popups.py b/nyx/popups.py
index ee4910c..5dd7c05 100644
--- a/nyx/popups.py
+++ b/nyx/popups.py
@@ -26,11 +26,11 @@ import operator
 
 import nyx
 import nyx.arguments
-import nyx.controller
 import nyx.curses
 import nyx.log
 import nyx.panel
 
+from nyx import nyx_interface
 from nyx.curses import RED, GREEN, YELLOW, CYAN, WHITE, NORMAL, BOLD, HIGHLIGHT
 
 import stem.control
@@ -56,15 +56,15 @@ def show_help():
     if it's one panels should act upon, **None** otherwise
   """
 
-  control = nyx.controller.get_controller()
+  interface = nyx_interface()
   handlers = []
 
-  for panel in reversed(control.get_display_panels()):
+  for panel in reversed(interface.get_display_panels()):
     handlers += [handler for handler in panel.key_handlers() if handler.description]
 
   def _render(subwindow):
     subwindow.box()
-    subwindow.addstr(0, 0, 'Page %i Commands:' % (control.get_page() + 1), HIGHLIGHT)
+    subwindow.addstr(0, 0, 'Page %i Commands:' % (interface.get_page() + 1), HIGHLIGHT)
 
     for i, option in enumerate(handlers):
       if i // 2 >= subwindow.height - 2:
@@ -543,4 +543,4 @@ def confirm_save_torrc(torrc):
 
 
 def _top():
-  return nyx.controller.get_controller().header_panel().get_height()
+  return nyx_interface().header_panel().get_height()
diff --git a/nyx/starter.py b/nyx/starter.py
index 5f89526..0126c3c 100644
--- a/nyx/starter.py
+++ b/nyx/starter.py
@@ -24,7 +24,7 @@ import stem
 import stem.util.log
 import stem.util.system
 
-from nyx import log, init_controller, msg, uses_settings
+from nyx import log, init_controller, msg, uses_settings, nyx_interface
 
 
 @uses_settings
@@ -254,10 +254,10 @@ def _shutdown_daemons(controller):
   """
 
   halt_threads = [nyx.tracker.stop_trackers()]
-  curses_controller = nyx.controller.get_controller()
+  interface = nyx_interface()
 
-  if curses_controller:
-    halt_threads.append(curses_controller.halt())
+  if interface:
+    halt_threads.append(interface.halt())
 
   for thread in halt_threads:
     thread.join()
diff --git a/test/panel/header.py b/test/panel/header.py
index 2951747..416e99d 100644
--- a/test/panel/header.py
+++ b/test/panel/header.py
@@ -69,13 +69,13 @@ def test_sampling():
 
 class TestHeaderPanel(unittest.TestCase):
   @require_curses
-  @patch('nyx.controller.get_controller')
+  @patch('nyx.panel.header.nyx_interface')
   @patch('nyx.panel.header.tor_controller')
   @patch('nyx.panel.header.Sampling.create')
-  def test_rendering_panel(self, sampling_mock, tor_controller_mock, nyx_controller_mock):
-    nyx_controller_mock().is_paused.return_value = False
-    nyx_controller_mock().get_page.return_value = 1
-    nyx_controller_mock().get_page_count.return_value = 4
+  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
     sampling_mock.return_value = test_sampling()
 
     panel = nyx.panel.header.HeaderPanel()
@@ -341,10 +341,10 @@ class TestHeaderPanel(unittest.TestCase):
     self.assertEqual('building circuits, available again in 5 seconds', test.render(nyx.panel.header._draw_newnym_option, 0, 0, 5).content)
 
   @require_curses
-  @patch('nyx.controller.get_controller')
-  def test_draw_status(self, nyx_controller_mock):
-    nyx_controller_mock().get_page.return_value = 1
-    nyx_controller_mock().get_page_count.return_value = 4
+  @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
 
     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)
diff --git a/test/popups.py b/test/popups.py
index 12ce3ef..4b12f33 100644
--- a/test/popups.py
+++ b/test/popups.py
@@ -246,8 +246,8 @@ NCGI042p6+7UgCVT1x3WcLnq3ScV//s1wXHrUXa7vi0=
 class TestPopups(unittest.TestCase):
   @require_curses
   @patch('nyx.popups._top', Mock(return_value = 0))
-  @patch('nyx.controller.get_controller')
-  def test_help(self, get_controller_mock):
+  @patch('nyx.popups.nyx_interface')
+  def test_help(self, nyx_interface_mock):
     header_panel = Mock()
 
     header_panel.key_handlers.return_value = (
@@ -275,7 +275,7 @@ class TestPopups(unittest.TestCase):
       nyx.panel.KeyHandler('c', 'clear event log'),
     )
 
-    get_controller_mock().get_display_panels.return_value = [header_panel, graph_panel, log_panel]
+    nyx_interface_mock().get_display_panels.return_value = [header_panel, graph_panel, log_panel]
 
     rendered = test.render(nyx.popups.show_help)
     self.assertEqual(EXPECTED_HELP_POPUP, rendered.content)





More information about the tor-commits mailing list