[tor-commits] [arm/master] Changing prepopulate_from_state() to raise exceptions

atagar at torproject.org atagar at torproject.org
Wed Sep 10 16:37:20 UTC 2014


commit d1f4fbb15ce82b4075550d9acd495d9504d8c3da
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Sep 10 08:26:44 2014 -0700

    Changing prepopulate_from_state() to raise exceptions
    
    Having prepopulate_from_state() raise upon issues, and return the seconds
    missing rather than a boolean indicating success. Both makes the function
    nicer, and lets us move the logging to the caller.
---
 arm/controller.py               |   15 +++++++++++----
 arm/graphing/bandwidth_stats.py |   27 +++++++--------------------
 2 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/arm/controller.py b/arm/controller.py
index 4a864b9..457da4e 100644
--- a/arm/controller.py
+++ b/arm/controller.py
@@ -26,9 +26,9 @@ import stem
 
 from stem.control import State
 
-from arm.util import panel, tor_config, tor_controller, ui_tools
+from arm.util import msg, panel, tor_config, tor_controller, ui_tools
 
-from stem.util import conf, enum, log, system
+from stem.util import conf, enum, log, str_tools, system
 
 ARM_CONTROLLER = None
 
@@ -198,10 +198,17 @@ def init_controller(stdscr, start_time):
     # prepopulates bandwidth values from state file
 
     if CONFIG["features.graph.bw.prepopulate"] and tor_controller().is_alive():
-      is_successful = bw_stats.prepopulate_from_state()
+      try:
+        missing_seconds = bw_stats.prepopulate_from_state()
+
+        if missing_sec:
+          log.notice(msg('panel.graphing.prepopulation_successful', duration = str_tools.time_label(missing_seconds, 0, True)))
+        else:
+          log.notice(msg('panel.graphing.prepopulation_all_successful'))
 
-      if is_successful:
         graph_panel.update_interval = 4
+      except ValueError as exc:
+        log.info(msg('panel.graphing.prepopulation_failure', error = str(exc)))
 
 
 class LabelPanel(panel.Panel):
diff --git a/arm/graphing/bandwidth_stats.py b/arm/graphing/bandwidth_stats.py
index 384497e..d2a0b33 100644
--- a/arm/graphing/bandwidth_stats.py
+++ b/arm/graphing/bandwidth_stats.py
@@ -138,8 +138,7 @@ class BandwidthStats(graph_panel.GraphStats):
     controller = tor_controller()
 
     if not controller.is_localhost():
-      log.info(msg('panel.graphing.prepopulation_failure', error = "we can only prepopulate bandwidth information for a local tor instance"))
-      return False
+      raise ValueError('we can only prepopulate bandwidth information for a local tor instance')
 
     start_time = system.start_time(controller.get_pid(None))
     uptime = time.time() - start_time if start_time else None
@@ -149,19 +148,16 @@ class BandwidthStats(graph_panel.GraphStats):
     # want to prepopulate with information from a prior tor instance.
 
     if not uptime:
-      log.info(msg('panel.graphing.prepopulation_failure', error = "unable to determine tor's uptime"))
-      return False
+      raise ValueError("unable to determine tor's uptime")
     elif uptime < (24 * 60 * 60):
-      log.info(msg('panel.graphing.prepopulation_failure', error = "insufficient uptime, tor must've been running for at least a day"))
-      return False
+      raise ValueError("insufficient uptime, tor must've been running for at least a day")
 
     # get the user's data directory (usually '~/.tor')
 
     data_dir = controller.get_conf('DataDirectory', None)
 
     if not data_dir:
-      log.info(msg('panel.graphing.prepopulation_failure', error = "unable to determine tor's data directory"))
-      return False
+      raise ValueError("unable to determine tor's data directory")
 
     # attempt to open the state file
 
@@ -171,8 +167,7 @@ class BandwidthStats(graph_panel.GraphStats):
       with open(state_file_path) as state_file:
         state_file_content = state_file.readlines()
     except IOError as exc:
-      log.info(msg('panel.graphing.prepopulation_failure', error = 'unable to read the state file at %s, %s' % (state_file_path, exc)))
-      return False
+      raise ValueError('unable to read the state file at %s, %s' % (state_file_path, exc))
 
     # get the BWHistory entries (ordered oldest to newest) and number of
     # intervals since last recorded
@@ -211,8 +206,7 @@ class BandwidthStats(graph_panel.GraphStats):
         missing_write_entries = int((time.time() - last_write_time) / 900)
 
     if not bw_read_entries or not bw_write_entries or not last_read_time or not last_write_time:
-      log.info(msg('panel.graphing.prepopulation_failure', error = 'bandwidth stats missing from state file'))
-      return False
+      raise ValueError('bandwidth stats missing from state file')
 
     # fills missing entries with the last value
 
@@ -255,14 +249,7 @@ class BandwidthStats(graph_panel.GraphStats):
     del self.primary_counts[interval_index][self.max_column + 1:]
     del self.secondary_counts[interval_index][self.max_column + 1:]
 
-    missing_sec = time.time() - min(last_read_time, last_write_time)
-
-    if missing_sec:
-      log.notice(msg('panel.graphing.prepopulation_successful', duration = str_tools.time_label(missing_sec, 0, True)))
-    else:
-      log.notice(msg('panel.graphing.prepopulation_all_successful'))
-
-    return True
+    return time.time() - min(last_read_time, last_write_time)
 
   def bandwidth_event(self, event):
     if self.is_accounting and self.is_next_tick_redraw():





More information about the tor-commits mailing list