[tor-commits] [nyx/master] Cache manual when read from 'man tor'

atagar at torproject.org atagar at torproject.org
Fri Jan 22 17:30:43 UTC 2016


commit 46cead2bc4439e227d65c410b461769c36948f4d
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jan 22 09:23:38 2016 -0800

    Cache manual when read from 'man tor'
    
    When available we run 'man tor' to get manual information about the user's
    specific version. This is a neat trick, but it takes over twice as long as
    reading our pre-parsed manual information.
    
    After parsing a manual caching the result to disk so running nyx in the future
    is a lot quicker.
---
 nyx/arguments.py     |    4 ++--
 nyx/config_panel.py  |   23 +++++++++++++++++------
 nyx/util/__init__.py |    1 +
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/nyx/arguments.py b/nyx/arguments.py
index 4351974..7510057 100644
--- a/nyx/arguments.py
+++ b/nyx/arguments.py
@@ -10,7 +10,7 @@ import nyx
 
 import stem.util.connection
 
-from nyx.util import tor_controller, msg
+from nyx.util import DATA_DIR, tor_controller, msg
 
 DEFAULT_ARGS = {
   'control_address': '127.0.0.1',
@@ -18,7 +18,7 @@ DEFAULT_ARGS = {
   'user_provided_port': False,
   'control_socket': '/var/run/tor/control',
   'user_provided_socket': False,
-  'config': os.path.expanduser('~/.nyx/nyxrc'),
+  'config': os.path.join(DATA_DIR, 'nyxrc'),
   'debug_path': None,
   'logged_events': 'N3',
   'print_version': False,
diff --git a/nyx/config_panel.py b/nyx/config_panel.py
index 8f71861..2352fe7 100644
--- a/nyx/config_panel.py
+++ b/nyx/config_panel.py
@@ -4,6 +4,7 @@ and the resulting configuration files saved.
 """
 
 import curses
+import os
 
 import nyx.controller
 import nyx.popups
@@ -11,7 +12,7 @@ import nyx.popups
 import stem.control
 import stem.manual
 
-from nyx.util import panel, tor_controller, ui_tools
+from nyx.util import DATA_DIR, panel, tor_controller, ui_tools
 
 from stem.util import conf, enum, log, str_tools
 
@@ -122,11 +123,21 @@ class ConfigPanel(panel.Panel):
     self._sort_order = CONFIG['features.config.order']
     self._show_all = False  # show all options, or just the important ones
 
-    try:
-      manual = stem.manual.Manual.from_man()
-    except IOError as exc:
-      log.debug("Unable to use 'man tor' to get information about config options (%s), using bundled information instead" % exc)
-      manual = stem.manual.Manual.from_cache()
+    cached_manual_path = os.path.join(DATA_DIR, 'manual')
+
+    if os.path.exists(cached_manual_path):
+      manual = stem.manual.Manual.from_cache(cached_manual_path)
+    else:
+      try:
+        manual = stem.manual.Manual.from_man()
+
+        try:
+          manual.save(cached_manual_path)
+        except IOError as exc:
+          log.debug("Unable to cache manual information to '%s'. This is fine, but means starting Nyx takes a little longer than usual: " % (cached_manual_path, exc))
+      except IOError as exc:
+        log.debug("Unable to use 'man tor' to get information about config options (%s), using bundled information instead" % exc)
+        manual = stem.manual.Manual.from_cache()
 
     try:
       for line in tor_controller().get_info('config/names').splitlines():
diff --git a/nyx/util/__init__.py b/nyx/util/__init__.py
index c19b9c7..12b5ace 100644
--- a/nyx/util/__init__.py
+++ b/nyx/util/__init__.py
@@ -23,6 +23,7 @@ __all__ = [
 
 TOR_CONTROLLER = None
 BASE_DIR = os.path.sep.join(__file__.split(os.path.sep)[:-2])
+DATA_DIR = os.path.expanduser('~/.nyx')
 TESTING = False
 
 try:



More information about the tor-commits mailing list