[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [nyx/master] Create data directory if it doesn't already exist



commit 68211c55bac70fcf24f6bb082e2cd1deccd323ee
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Aug 22 09:56:18 2017 -0700

    Create data directory if it doesn't already exist
    
    When fetching a data directory path ensuring the directory exists. Presently
    this doesn't do anything because this is only used for manual caching (which
    creates the directory itself), but as we cache more things this'll be handy.
---
 nyx/__init__.py     | 17 +++++++++++++++--
 nyx/panel/config.py |  6 ++----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/nyx/__init__.py b/nyx/__init__.py
index 5cd4a22..5cd0898 100644
--- a/nyx/__init__.py
+++ b/nyx/__init__.py
@@ -232,9 +232,22 @@ def init_controller(*args, **kwargs):
 
 
 @uses_settings
-def data_directory(config):
+def data_directory(config, filename):
   path = config.get('data_directory', '~/.nyx')
-  return None if path == 'disabled' else os.path.expanduser(path)
+
+  if path == 'disabled':
+    return None
+
+  data_dir = os.path.expanduser(path)
+
+  if not os.path.exists(data_dir):
+    try:
+      os.mkdir(data_dir)
+    except OSError as exc:
+      stem.util.log.log_once('nyx.data_directory_unavailable', stem.util.log.NOTICE, 'Unable to create a data directory at %s (%s). This is fine, but caching is disabled meaning performance will be diminished a bit.' % (data_dir, exc))
+      return None
+
+  return os.path.join(data_dir, filename)
 
 
 @uses_settings
diff --git a/nyx/panel/config.py b/nyx/panel/config.py
index d786981..07a04b0 100644
--- a/nyx/panel/config.py
+++ b/nyx/panel/config.py
@@ -130,11 +130,9 @@ class ConfigPanel(nyx.panel.Panel):
     self._sort_order = CONFIG['config_order']
     self._show_all = False  # show all options, or just the important ones
 
-    data_dir = data_directory()
-
-    if data_dir:
-      cached_manual_path = os.path.join(data_dir, 'manual')
+    cached_manual_path = data_directory('manual')
 
+    if cached_manual_path:
       if os.path.exists(cached_manual_path):
         manual = stem.manual.Manual.from_cache(cached_manual_path)
       else:

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits