utils.py
changeset 7023 af9c5ac72f2c
parent 6937 99ee62eec34c
child 7024 4392d52b3536
--- a/utils.py	Tue Apr 12 13:12:27 2011 +0100
+++ b/utils.py	Tue Apr 12 10:39:17 2011 -0700
@@ -44,6 +44,38 @@
     return list
 
 
+def get_bool_from_file(file_path, bool_name, value_if_missing):
+    '''Looks for a Python boolean variable called bool_name in the
+    file specified by file_path and returns its value.
+
+    If the file or boolean variable aren't found, this function will
+    return value_if_missing.
+
+    '''
+
+    # Read in the file if it exists.
+    if os.path.exists(file_path):
+        file_in = open(file_path, "r")
+
+        # Look for the boolean variable.
+        bool_found = False
+        for line in file_in:
+            if bool_name in line:
+                # Evaluate the variable's line once it is found.  Make
+                # the split function only split it once.
+                bool = eval(line.split('=', 1)[1].strip())
+                bool_found = True
+                break
+
+        # Close the file
+        file_in.close()
+
+    if bool_found:
+        return bool
+    else:
+        return value_if_missing
+
+
 def read_config_file():
     '''Reads the NS-3 configuration file and returns a list of enabled modules.
 
@@ -53,20 +85,38 @@
 
     '''
 
+    # By default, all modules will be enabled, examples will be enabled,
+    # and tests will be disabled.
+    modules_enabled  = ['all_modules']
+    examples_enabled = True
+    tests_enabled    = False
+
     # See if the ns3 configuration file exists in the current working
     # directory and then look for it in the ~ directory.
+    config_file_exists = False
     dot_ns3rc_name = '.ns3rc'
     dot_ns3rc_path = dot_ns3rc_name
     if not os.path.exists(dot_ns3rc_path):
         dot_ns3rc_path = os.path.expanduser('~/') + dot_ns3rc_name
         if not os.path.exists(dot_ns3rc_path):
-            # Enable all modules if the .ns3rc file can't be found.
-            return ['all_modules']
+            # Return all of the default values if the .ns3rc file can't be found.
+            return (config_file_exists, modules_enabled, examples_enabled, tests_enabled)
 
-    # Read in the ns3 configuration file.
+    config_file_exists = True
+
+    # Read in the enabled modules.
     modules_enabled = get_list_from_file(dot_ns3rc_path, 'modules_enabled')
     if not modules_enabled:
         # Enable all modules if the modules_enabled line can't be found.
-        return ['all_modules']
+        modules_enabled = ['all_modules']
+
+    # Read in whether examples should be enabled or not.
+    value_if_missing = True
+    examples_enabled = get_bool_from_file(dot_ns3rc_path, 'examples_enabled', value_if_missing)
 
-    return modules_enabled
+    # Read in whether tests should be enabled or not.
+    value_if_missing = False
+    tests_enabled = get_bool_from_file(dot_ns3rc_path, 'tests_enabled', value_if_missing)
+
+    return (config_file_exists, modules_enabled, examples_enabled, tests_enabled)
+