[tor-commits] [nyx/master] Move paused attribute handling out of Panel

atagar at torproject.org atagar at torproject.org
Sun Mar 20 00:17:45 UTC 2016


commit 5484afb38e2ab7475c22b5194ba3dccefbadea22
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Mar 19 16:15:10 2016 -0700

    Move paused attribute handling out of Panel
    
    This is simpler and less error prone when done by the panels. Actually, the
    graph panel was the only spot still using these helpers so easy thing to
    remove. :P
---
 nyx/panel/__init__.py | 69 ---------------------------------------------------
 nyx/panel/graph.py    | 42 ++++++++++++++++++-------------
 2 files changed, 25 insertions(+), 86 deletions(-)

diff --git a/nyx/panel/__init__.py b/nyx/panel/__init__.py
index c47ac59..77884ac 100644
--- a/nyx/panel/__init__.py
+++ b/nyx/panel/__init__.py
@@ -2,7 +2,6 @@
 Panels consisting the nyx interface.
 """
 
-import copy
 import time
 import curses
 import curses.ascii
@@ -142,13 +141,7 @@ class Panel(object):
     self.visible = False
     self.title_visible = True
 
-    # Attributes for pausing. The pause_attr contains variables our get_attr
-    # method is tracking, and the pause buffer has copies of the values from
-    # when we were last unpaused (unused unless we're paused).
-
     self.paused = False
-    self.pause_attr = []
-    self.pause_buffer = {}
     self.pause_time = -1
 
     self.top = top
@@ -207,56 +200,6 @@ class Panel(object):
 
     return self.paused
 
-  def set_pause_attr(self, attr):
-    """
-    Configures the panel to track the given attribute so that get_attr provides
-    the value when it was last unpaused (or its current value if we're
-    currently unpaused). For instance...
-
-    > self.set_pause_attr('myVar')
-    > self.myVar = 5
-    > self.myVar = 6  # self.get_attr('myVar') -> 6
-    > self.set_paused(True)
-    > self.myVar = 7  # self.get_attr('myVar') -> 6
-    > self.set_paused(False)
-    > self.myVar = 7  # self.get_attr('myVar') -> 7
-
-    Arguments:
-      attr - parameter to be tracked for get_attr
-    """
-
-    self.pause_attr.append(attr)
-    self.pause_buffer[attr] = self.copy_attr(attr)
-
-  def get_attr(self, attr):
-    """
-    Provides the value of the given attribute when we were last unpaused. If
-    we're currently unpaused then this is the current value. If untracked this
-    returns None.
-
-    Arguments:
-      attr - local variable to be returned
-    """
-
-    if attr not in self.pause_attr:
-      return None
-    elif self.paused:
-      return self.pause_buffer[attr]
-    else:
-      return self.__dict__.get(attr)
-
-  def copy_attr(self, attr):
-    """
-    Provides a duplicate of the given configuration value, suitable for the
-    pause buffer.
-
-    Arguments:
-      attr - parameter to be provided back
-    """
-
-    current_value = self.__dict__.get(attr)
-    return copy.copy(current_value)
-
   def set_paused(self, is_pause):
     """
     Toggles if the panel is paused or not. This causes the panel to be redrawn
@@ -264,8 +207,6 @@ class Panel(object):
     important when pausing since otherwise the panel's display could change
     when redrawn for other reasons.
 
-    This returns True if the panel's pause state was changed, False otherwise.
-
     Arguments:
       is_pause        - freezes the state of the pause attributes if true, makes
                         them editable otherwise
@@ -276,17 +217,7 @@ class Panel(object):
         self.pause_time = time.time()
 
       self.paused = is_pause
-
-      if is_pause:
-        # copies tracked attributes so we know what they were before pausing
-
-        for attr in self.pause_attr:
-          self.pause_buffer[attr] = self.copy_attr(attr)
-
       self.redraw(True)
-      return True
-    else:
-      return False
 
   def get_pause_time(self):
     """
diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py
index 20a6110..b325dd1 100644
--- a/nyx/panel/graph.py
+++ b/nyx/panel/graph.py
@@ -390,21 +390,21 @@ class GraphPanel(nyx.panel.Panel):
     self._graph_height = CONFIG['features.graph.height']
 
     self._accounting_stats = None
+    self._accounting_stats_paused = None
 
     self._stats = {
       GraphStat.BANDWIDTH: BandwidthStats(),
       GraphStat.SYSTEM_RESOURCES: ResourceStats(),
     }
 
+    self._stats_paused = None
+
     if CONFIG['features.panels.show.connection']:
       self._stats[GraphStat.CONNECTIONS] = ConnectionStats()
     elif self._displayed_stat == GraphStat.CONNECTIONS:
       log.warn("The connection graph is unavailble when you set 'features.panels.show.connection false'.")
       self._displayed_stat = GraphStat.BANDWIDTH
 
-    self.set_pause_attr('_stats')
-    self.set_pause_attr('_accounting_stats')
-
     controller = tor_controller()
     controller.add_event_listener(self._update_accounting, EventType.BW)
     controller.add_event_listener(self._update_stats, EventType.BW)
@@ -455,8 +455,9 @@ class GraphPanel(nyx.panel.Panel):
       return 0
 
     height = DEFAULT_CONTENT_HEIGHT + self._graph_height
+    accounting_stats = self._accounting_stats if self.is_paused() else self._accounting_stats_paused
 
-    if self.displayed_stat == GraphStat.BANDWIDTH and self._accounting_stats:
+    if self.displayed_stat == GraphStat.BANDWIDTH and accounting_stats:
       height += 3
 
     return height
@@ -544,11 +545,23 @@ class GraphPanel(nyx.panel.Panel):
       ('i', 'graph update interval', self.update_interval),
     ]
 
+  def set_paused(self, is_pause):
+    if is_pause:
+      self._accounting_stats_paused = copy.copy(self._accounting_stats)
+      self._stats_paused = dict([(key, type(self._stats[key])(self._stats[key])) for key in self._stats])
+
+    nyx.panel.Panel.set_paused(self, is_pause)
+
   def draw(self, width, height):
     if not self.displayed_stat:
       return
 
-    stat = self.get_attr('_stats')[self.displayed_stat]
+    if not self.is_paused():
+      stat = self._stats[self.displayed_stat]
+      accounting_stats = self._accounting_stats
+    else:
+      stat = self._stats_paused[self.displayed_stat]
+      accounting_stats = self._accounting_stats_paused
 
     attr = DrawAttributes(
       stat = type(stat)(stat),  # clone the GraphCategory
@@ -556,7 +569,7 @@ class GraphPanel(nyx.panel.Panel):
       subgraph_width = min(width / 2, CONFIG['features.graph.max_width']),
       interval = self.update_interval,
       bounds_type = self.bounds_type,
-      accounting = self.get_attr('_accounting_stats'),
+      accounting = accounting_stats,
       right_to_left = CONFIG['features.graph.right_to_left'],
     )
 
@@ -723,12 +736,6 @@ class GraphPanel(nyx.panel.Panel):
       self.addstr(y, 0, 'Accounting:', BOLD)
       self.addstr(y, 12, 'Connection Closed...')
 
-  def copy_attr(self, attr):
-    if attr == '_stats':
-      return dict([(key, type(self._stats[key])(self._stats[key])) for key in self._stats])
-    else:
-      return nyx.panel.Panel.copy_attr(self, attr)
-
   def _update_accounting(self, event):
     if not CONFIG['features.graph.bw.accounting.show']:
       self._accounting_stats = None
@@ -736,18 +743,19 @@ class GraphPanel(nyx.panel.Panel):
       old_accounting_stats = self._accounting_stats
       self._accounting_stats = tor_controller().get_accounting_stats(None)
 
-      # if we either added or removed accounting info then redraw the whole
-      # screen to account for resizing
+      if not self.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()
+        if bool(old_accounting_stats) != bool(self._accounting_stats):
+          nyx.controller.get_controller().redraw()
 
   def _update_stats(self, event):
     for stat in self._stats.values():
       stat.bandwidth_event(event)
 
     if self.displayed_stat:
-      param = self.get_attr('_stats')[self.displayed_stat]
+      param = self._stats[self.displayed_stat]
       update_rate = INTERVAL_SECONDS[self.update_interval]
 
       if param.primary.tick % update_rate == 0:





More information about the tor-commits mailing list