utils.py
changeset 7023 af9c5ac72f2c
parent 6937 99ee62eec34c
child 7024 4392d52b3536
equal deleted inserted replaced
7022:497d6fc1aac8 7023:af9c5ac72f2c
    42         file_in.close()
    42         file_in.close()
    43 
    43 
    44     return list
    44     return list
    45 
    45 
    46 
    46 
       
    47 def get_bool_from_file(file_path, bool_name, value_if_missing):
       
    48     '''Looks for a Python boolean variable called bool_name in the
       
    49     file specified by file_path and returns its value.
       
    50 
       
    51     If the file or boolean variable aren't found, this function will
       
    52     return value_if_missing.
       
    53 
       
    54     '''
       
    55 
       
    56     # Read in the file if it exists.
       
    57     if os.path.exists(file_path):
       
    58         file_in = open(file_path, "r")
       
    59 
       
    60         # Look for the boolean variable.
       
    61         bool_found = False
       
    62         for line in file_in:
       
    63             if bool_name in line:
       
    64                 # Evaluate the variable's line once it is found.  Make
       
    65                 # the split function only split it once.
       
    66                 bool = eval(line.split('=', 1)[1].strip())
       
    67                 bool_found = True
       
    68                 break
       
    69 
       
    70         # Close the file
       
    71         file_in.close()
       
    72 
       
    73     if bool_found:
       
    74         return bool
       
    75     else:
       
    76         return value_if_missing
       
    77 
       
    78 
    47 def read_config_file():
    79 def read_config_file():
    48     '''Reads the NS-3 configuration file and returns a list of enabled modules.
    80     '''Reads the NS-3 configuration file and returns a list of enabled modules.
    49 
    81 
    50     This function first looks for the ns3 configuration file (.ns3rc)
    82     This function first looks for the ns3 configuration file (.ns3rc)
    51     in the current working directory and then looks in the ~
    83     in the current working directory and then looks in the ~
    52     directory.
    84     directory.
    53 
    85 
    54     '''
    86     '''
    55 
    87 
       
    88     # By default, all modules will be enabled, examples will be enabled,
       
    89     # and tests will be disabled.
       
    90     modules_enabled  = ['all_modules']
       
    91     examples_enabled = True
       
    92     tests_enabled    = False
       
    93 
    56     # See if the ns3 configuration file exists in the current working
    94     # See if the ns3 configuration file exists in the current working
    57     # directory and then look for it in the ~ directory.
    95     # directory and then look for it in the ~ directory.
       
    96     config_file_exists = False
    58     dot_ns3rc_name = '.ns3rc'
    97     dot_ns3rc_name = '.ns3rc'
    59     dot_ns3rc_path = dot_ns3rc_name
    98     dot_ns3rc_path = dot_ns3rc_name
    60     if not os.path.exists(dot_ns3rc_path):
    99     if not os.path.exists(dot_ns3rc_path):
    61         dot_ns3rc_path = os.path.expanduser('~/') + dot_ns3rc_name
   100         dot_ns3rc_path = os.path.expanduser('~/') + dot_ns3rc_name
    62         if not os.path.exists(dot_ns3rc_path):
   101         if not os.path.exists(dot_ns3rc_path):
    63             # Enable all modules if the .ns3rc file can't be found.
   102             # Return all of the default values if the .ns3rc file can't be found.
    64             return ['all_modules']
   103             return (config_file_exists, modules_enabled, examples_enabled, tests_enabled)
    65 
   104 
    66     # Read in the ns3 configuration file.
   105     config_file_exists = True
       
   106 
       
   107     # Read in the enabled modules.
    67     modules_enabled = get_list_from_file(dot_ns3rc_path, 'modules_enabled')
   108     modules_enabled = get_list_from_file(dot_ns3rc_path, 'modules_enabled')
    68     if not modules_enabled:
   109     if not modules_enabled:
    69         # Enable all modules if the modules_enabled line can't be found.
   110         # Enable all modules if the modules_enabled line can't be found.
    70         return ['all_modules']
   111         modules_enabled = ['all_modules']
    71 
   112 
    72     return modules_enabled
   113     # Read in whether examples should be enabled or not.
       
   114     value_if_missing = True
       
   115     examples_enabled = get_bool_from_file(dot_ns3rc_path, 'examples_enabled', value_if_missing)
       
   116 
       
   117     # Read in whether tests should be enabled or not.
       
   118     value_if_missing = False
       
   119     tests_enabled = get_bool_from_file(dot_ns3rc_path, 'tests_enabled', value_if_missing)
       
   120 
       
   121     return (config_file_exists, modules_enabled, examples_enabled, tests_enabled)
       
   122