[tor-commits] [ooni-probe/master] Make sure that the config file parameters are only instanced once

art at torproject.org art at torproject.org
Wed Nov 21 16:11:01 UTC 2012


commit 508d045f14f67c1bc182f1f395be253ba7d2a6c2
Author: Arturo Filastò <art at fuffa.org>
Date:   Wed Nov 21 17:07:00 2012 +0100

    Make sure that the config file parameters are only instanced once
---
 ooni/config.py |   61 ++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/ooni/config.py b/ooni/config.py
index 29255c1..0aad5cb 100644
--- a/ooni/config.py
+++ b/ooni/config.py
@@ -11,6 +11,7 @@ from twisted.internet import reactor, threads
 from ooni.utils import otime
 from ooni.utils import Storage
 
+
 def get_root_path():
     this_directory = os.path.dirname(__file__)
     root = os.path.join(this_directory, '..')
@@ -30,30 +31,42 @@ def oreport_filenames(file_name):
     pcap_filename = base_filename % (test_name, "packets", "pcap")
     return yamloo_filename, pcap_filename
 
-config_file = os.path.join(get_root_path(), 'ooniprobe.conf')
-try:
-    f = open(config_file)
-except IOError:
-    raise Exception("Unable to open config file. "\
-                "Create a config file called ooniprobe.conf")
-
-config_file_contents = '\n'.join(f.readlines())
-configuration = yaml.safe_load(config_file_contents)
-
-# Process the basic configuration options
-basic = Storage()
-for k, v in configuration['basic'].items():
-    basic[k] = v
-
-# Process the privacy configuration options
-privacy = Storage()
-for k, v in configuration['privacy'].items():
-    privacy[k] = v
-
-# Process the advanced configuration options
-advanced = Storage()
-for k, v in configuration['advanced'].items():
-    advanced[k] = v
+def loadConfigFile():
+    """
+    This is a helper function that makes sure that the configuration attributes
+    are singletons.
+    """
+    print "LOading config"
+    config_file = os.path.join(get_root_path(), 'ooniprobe.conf')
+    try:
+        f = open(config_file)
+    except IOError:
+        raise Exception("Unable to open config file. "\
+                    "Create a config file called ooniprobe.conf")
+
+    config_file_contents = '\n'.join(f.readlines())
+    configuration = yaml.safe_load(config_file_contents)
+
+    # Process the basic configuration options
+    basic = Storage()
+    for k, v in configuration['basic'].items():
+        basic[k] = v
+
+    # Process the privacy configuration options
+    privacy = Storage()
+    for k, v in configuration['privacy'].items():
+        privacy[k] = v
+
+    # Process the advanced configuration options
+    advanced = Storage()
+    for k, v in configuration['advanced'].items():
+        advanced[k] = v
+    return basic, privacy, advanced
+
+basic = None
+if not basic:
+    # Here we make sure that we instance the config file attributes only once
+    basic, privacy, advanced = loadConfigFile()
 
 # This is used to keep track of the state of the sniffer
 sniffer_running = None





More information about the tor-commits mailing list