[tor-commits] [stem/master] Fetching event descriptions directly, rather than use manual.py

atagar at torproject.org atagar at torproject.org
Sun May 22 01:40:51 UTC 2016


commit c0af25275f66e36c05f721167949dd63ef4ebed3
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat May 21 18:37:33 2016 -0700

    Fetching event descriptions directly, rather than use manual.py
    
    Using the manual's _config() certainly did the trick, but kinda a hack.
    Populating our own cache of these.
    
    This now omits events from _config() so its cache is a little smaller.
---
 docs/change_log.rst |  1 +
 stem/control.py     | 24 +++++++++++++++++++++---
 stem/manual.py      |  2 +-
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index b443c69..ecfc49a 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -49,6 +49,7 @@ The following are only available within Stem's `git repository
   * :func:`~stem.connection.connect` and :func:`~stem.control.Controller.from_port` now connect to both port 9051 (relay's default) and 9151 (Tor Browser's default) (:trac:`16075`)
   * :class:`~stem.exit_policy.ExitPolicy` support for *accept6/reject6* and *\*4/6* wildcards (:trac:`16053`)
   * Added `support for NETWORK_LIVENESS events <api/response.html#stem.response.events.NetworkLivenessEvent>`_ (:spec:`44aac63`)
+  * Added :func:`~stem.control.event_description` for getting human-friendly descriptions of tor events (:trac:`19061`)
   * Added :func:`~stem.control.Controller.reconnect` to the :class:`~stem.control.Controller`
   * Added :func:`~stem.control.Controller.is_set` to the :class:`~stem.control.Controller`
   * Added :func:`~stem.control.Controller.is_user_traffic_allowed` to the :class:`~stem.control.Controller`
diff --git a/stem/control.py b/stem/control.py
index 621aaea..f1eb274 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -263,10 +263,10 @@ import stem.descriptor.reader
 import stem.descriptor.router_status_entry
 import stem.descriptor.server_descriptor
 import stem.exit_policy
-import stem.manual
 import stem.response
 import stem.response.events
 import stem.socket
+import stem.util.conf
 import stem.util.connection
 import stem.util.enum
 import stem.util.str_tools
@@ -378,12 +378,15 @@ UNCACHEABLE_GETCONF_PARAMS = (
 
 # number of sequential attempts before we decide that the Tor geoip database
 # is unavailable
+
 GEOIP_FAILURE_THRESHOLD = 5
 
 SERVER_DESCRIPTORS_UNSUPPORTED = "Tor is currently not configured to retrieve \
 server descriptors. As of Tor version 0.2.3.25 it downloads microdescriptors \
 instead unless you set 'UseMicrodescriptors 0' in your torrc."
 
+EVENT_DESCRIPTIONS = None
+
 
 class AccountingStats(collections.namedtuple('AccountingStats', ['retrieved', 'status', 'interval_end', 'time_until_reset', 'read_bytes', 'read_bytes_left', 'read_limit', 'written_bytes', 'write_bytes_left', 'write_limit'])):
   """
@@ -484,9 +487,24 @@ def event_description(event):
 
   :param str event: the event for which a description is needed
 
-  :returns: str The event description
+  :returns: str The event description or **None** if this is an event name we
+    don't have a description for
   """
-  return stem.manual._config().get('event.description.%s' % event.lower(), None)
+
+  global EVENT_DESCRIPTIONS
+
+  if EVENT_DESCRIPTIONS is None:
+    config = stem.util.conf.Config()
+    config_path = os.path.join(os.path.dirname(__file__), 'settings.cfg')
+
+    try:
+      config.load(config_path)
+      EVENT_DESCRIPTIONS = dict([(key.lower()[18:], config.get_value(key)) for key in config.keys() if key.startswith('event.description.')])
+    except Exception as exc:
+      log.warn("BUG: stem failed to load its internal manual information from '%s': %s" % (config_path, exc))
+      return None
+
+  return EVENT_DESCRIPTIONS.get(event.lower())
 
 
 class BaseController(object):
diff --git a/stem/manual.py b/stem/manual.py
index be7b224..1e2a5e1 100644
--- a/stem/manual.py
+++ b/stem/manual.py
@@ -141,7 +141,7 @@ def _config(lowercase = True):
 
   try:
     config.load(config_path)
-    config_dict = dict([(key.lower() if lowercase else key, config.get_value(key)) for key in config.keys()])
+    config_dict = dict([(key.lower() if lowercase else key, config.get_value(key)) for key in config.keys() if key.startswith('manual.summary.')])
     config_dict['manual.important'] = [name.lower() if lowercase else name for name in config.get_value('manual.important', [], multiple = True)]
     return config_dict
   except Exception as exc:





More information about the tor-commits mailing list