[tor-commits] [nyx/master] Help namedtuple

atagar at torproject.org atagar at torproject.org
Tue Mar 29 16:32:09 UTC 2016


commit f8efef547f18d98bb236c773e2254fc10cb6b490
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Mar 29 09:28:25 2016 -0700

    Help namedtuple
    
    Having our get_help() methods provide namedtuples rather than tuples. Not a big
    whoop, but we might combine the handle_key() and get_help() methods soon.
---
 nyx/panel/__init__.py   | 22 +++++++++++++++++-----
 nyx/panel/config.py     | 20 ++++++++++----------
 nyx/panel/connection.py | 22 +++++++++++-----------
 nyx/panel/graph.py      | 12 ++++++------
 nyx/panel/log.py        | 18 +++++++++---------
 nyx/panel/torrc.py      | 18 +++++++++---------
 nyx/popups.py           | 12 +++++-------
 7 files changed, 67 insertions(+), 57 deletions(-)

diff --git a/nyx/panel/__init__.py b/nyx/panel/__init__.py
index 70c577d..b725e20 100644
--- a/nyx/panel/__init__.py
+++ b/nyx/panel/__init__.py
@@ -2,6 +2,7 @@
 Panels consisting the nyx interface.
 """
 
+import collections
 import time
 import curses
 import curses.ascii
@@ -33,8 +34,20 @@ CONFIG = conf.config_dict('nyx', {
   'features.maxLineWrap': 8,
 }, conf_handler)
 
-# prevents curses redraws if set
-HALT_ACTIVITY = False
+HALT_ACTIVITY = False  # prevents curses redraws if set
+
+
+class Help(collections.namedtuple('Help', ['key', 'description', 'current'])):
+  """
+  Help information about keybindings the panel handles.
+
+  :var str key: key the user can press
+  :var str description: description of what it does
+  :var str current: optional current value
+  """
+
+  def __new__(self, key, description, current = None):
+    return super(Help, self).__new__(self, key, description, current)
 
 
 class BasicValidator(object):
@@ -273,11 +286,10 @@ class Panel(object):
   def get_help(self):
     """
     Provides help information for the controls this page provides. This is a
-    list of tuples of the form...
-    (control, description, status)
+    tuple of :class:`~nyx.panel.Help` instances.
     """
 
-    return []
+    return ()
 
   def draw(self, width, height):
     """
diff --git a/nyx/panel/config.py b/nyx/panel/config.py
index 4b2881d..f0eac85 100644
--- a/nyx/panel/config.py
+++ b/nyx/panel/config.py
@@ -274,16 +274,16 @@ class ConfigPanel(nyx.panel.Panel):
     return True
 
   def get_help(self):
-    return [
-      ('up arrow', 'scroll up a line', None),
-      ('down arrow', 'scroll down a line', None),
-      ('page up', 'scroll up a page', None),
-      ('page down', 'scroll down a page', None),
-      ('enter', 'edit configuration option', None),
-      ('w', 'write torrc', None),
-      ('a', 'toggle filtering', None),
-      ('s', 'sort ordering', None),
-    ]
+    return (
+      nyx.panel.Help('up arrow', 'scroll up a line'),
+      nyx.panel.Help('down arrow', 'scroll down a line'),
+      nyx.panel.Help('page up', 'scroll up a page'),
+      nyx.panel.Help('page down', 'scroll down a page'),
+      nyx.panel.Help('enter', 'edit configuration option'),
+      nyx.panel.Help('w', 'write torrc'),
+      nyx.panel.Help('a', 'toggle filtering'),
+      nyx.panel.Help('s', 'sort ordering'),
+    )
 
   def draw(self, width, height):
     contents = self._get_config_options()
diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py
index 22cda50..127cd12 100644
--- a/nyx/panel/connection.py
+++ b/nyx/panel/connection.py
@@ -420,23 +420,23 @@ class ConnectionPanel(nyx.panel.Panel, threading.Thread):
     user_traffic_allowed = tor_controller().is_user_traffic_allowed()
 
     options = [
-      ('up arrow', 'scroll up a line', None),
-      ('down arrow', 'scroll down a line', None),
-      ('page up', 'scroll up a page', None),
-      ('page down', 'scroll down a page', None),
-      ('enter', 'show connection details', None),
-      ('d', 'raw consensus descriptor', None),
-      ('s', 'sort ordering', None),
-      ('r', 'connection resolver', 'auto' if resolver is None else resolver),
+      nyx.panel.Help('up arrow', 'scroll up a line'),
+      nyx.panel.Help('down arrow', 'scroll down a line'),
+      nyx.panel.Help('page up', 'scroll up a page'),
+      nyx.panel.Help('page down', 'scroll down a page'),
+      nyx.panel.Help('enter', 'show connection details'),
+      nyx.panel.Help('d', 'raw consensus descriptor'),
+      nyx.panel.Help('s', 'sort ordering'),
+      nyx.panel.Help('r', 'connection resolver', 'auto' if resolver is None else resolver),
     ]
 
     if user_traffic_allowed.inbound:
-      options.append(('c', 'client locale usage summary', None))
+      options.append(nyx.panel.Help('c', 'client locale usage summary'))
 
     if user_traffic_allowed.outbound:
-      options.append(('e', 'exit port usage summary', None))
+      options.append(nyx.panel.Help('e', 'exit port usage summary'))
 
-    return options
+    return tuple(options)
 
   def draw(self, width, height):
     controller = tor_controller()
diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py
index 510fa14..b09c8b8 100644
--- a/nyx/panel/graph.py
+++ b/nyx/panel/graph.py
@@ -538,12 +538,12 @@ class GraphPanel(nyx.panel.Panel):
     return True
 
   def get_help(self):
-    return [
-      ('r', 'resize graph', None),
-      ('s', 'graphed stats', self.displayed_stat if self.displayed_stat else 'none'),
-      ('b', 'graph bounds', self.bounds_type.replace('_', ' ')),
-      ('i', 'graph update interval', self.update_interval),
-    ]
+    return (
+      nyx.panel.Help('r', 'resize graph'),
+      nyx.panel.Help('s', 'graphed stats', self.displayed_stat if self.displayed_stat else 'none'),
+      nyx.panel.Help('b', 'graph bounds', self.bounds_type.replace('_', ' ')),
+      nyx.panel.Help('i', 'graph update interval', self.update_interval),
+    )
 
   def set_paused(self, is_pause):
     if is_pause:
diff --git a/nyx/panel/log.py b/nyx/panel/log.py
index c16eaec..8a0a9c7 100644
--- a/nyx/panel/log.py
+++ b/nyx/panel/log.py
@@ -261,15 +261,15 @@ class LogPanel(nyx.panel.Panel, threading.Thread):
     return True
 
   def get_help(self):
-    return [
-      ('up arrow', 'scroll log up a line', None),
-      ('down arrow', 'scroll log down a line', None),
-      ('a', 'save snapshot of the log', None),
-      ('e', 'change logged events', None),
-      ('f', 'log regex filter', 'enabled' if self._filter.selection() else 'disabled'),
-      ('u', 'duplicate log entries', 'visible' if self._show_duplicates else 'hidden'),
-      ('c', 'clear event log', None),
-    ]
+    return (
+      nyx.panel.Help('up arrow', 'scroll log up a line'),
+      nyx.panel.Help('down arrow', 'scroll log down a line'),
+      nyx.panel.Help('a', 'save snapshot of the log'),
+      nyx.panel.Help('e', 'change logged events'),
+      nyx.panel.Help('f', 'log regex filter', 'enabled' if self._filter.selection() else 'disabled'),
+      nyx.panel.Help('u', 'duplicate log entries', 'visible' if self._show_duplicates else 'hidden'),
+      nyx.panel.Help('c', 'clear event log'),
+    )
 
   def set_paused(self, is_pause):
     if is_pause:
diff --git a/nyx/panel/torrc.py b/nyx/panel/torrc.py
index 39318f5..560afb3 100644
--- a/nyx/panel/torrc.py
+++ b/nyx/panel/torrc.py
@@ -97,15 +97,15 @@ class TorrcPanel(panel.Panel):
     return True
 
   def get_help(self):
-    return [
-      ('up arrow', 'scroll up a line', None),
-      ('down arrow', 'scroll down a line', None),
-      ('page up', 'scroll up a page', None),
-      ('page down', 'scroll down a page', None),
-      ('s', 'comment stripping', 'off' if self._show_comments else 'on'),
-      ('l', 'line numbering', 'on' if self._show_line_numbers else 'off'),
-      ('x', 'reset tor (issue sighup)', None),
-    ]
+    return (
+      nyx.panel.Help('up arrow', 'scroll up a line'),
+      nyx.panel.Help('down arrow', 'scroll down a line'),
+      nyx.panel.Help('page up', 'scroll up a page'),
+      nyx.panel.Help('page down', 'scroll down a page'),
+      nyx.panel.Help('s', 'comment stripping', 'off' if self._show_comments else 'on'),
+      nyx.panel.Help('l', 'line numbering', 'on' if self._show_line_numbers else 'off'),
+      nyx.panel.Help('x', 'reset tor (issue sighup)'),
+    )
 
   def draw(self, width, height):
     scroll = self._scroller.location(self._last_content_height, height)
diff --git a/nyx/popups.py b/nyx/popups.py
index d6a14f1..0952a8d 100644
--- a/nyx/popups.py
+++ b/nyx/popups.py
@@ -93,7 +93,7 @@ def show_help_popup():
     subwindow.box()
     subwindow.addstr(0, 0, 'Page %i Commands:' % (control.get_page() + 1), HIGHLIGHT)
 
-    for i in range(len(help_options)):
+    for i, option in enumerate(help_options):
       if i / 2 >= subwindow.height - 2:
         break
 
@@ -102,17 +102,15 @@ def show_help_popup():
       #
       #   u: duplicate log entries (hidden)
 
-      key, description, selection = help_options[i]
-
       x = 2 if i % 2 == 0 else 41
       y = (i / 2) + 1
 
-      x = subwindow.addstr(x, y, key, BOLD)
-      x = subwindow.addstr(x, y, ': ' + description)
+      x = subwindow.addstr(x, y, option.key, BOLD)
+      x = subwindow.addstr(x, y, ': ' + option.description)
 
-      if selection:
+      if option.current:
         x = subwindow.addstr(x, y, ' (')
-        x = subwindow.addstr(x, y, selection, BOLD)
+        x = subwindow.addstr(x, y, option.current, BOLD)
         x = subwindow.addstr(x, y, ')')
 
     # tells user to press a key if the lower left is unoccupied



More information about the tor-commits mailing list