utils.py
changeset 6886 690b7a78bbd0
child 6937 99ee62eec34c
equal deleted inserted replaced
6884:b7518424f22d 6886:690b7a78bbd0
       
     1 #! /usr/bin/env python
       
     2 
       
     3 import os
       
     4 import sys
       
     5 
       
     6 def get_list_from_file(file_path, list_name):
       
     7     '''Looks for a Python list called list_name in the file specified
       
     8     by file_path and returns it.
       
     9 
       
    10     If the file or list name aren't found, this function will return
       
    11     an empty list.
       
    12 
       
    13     '''
       
    14 
       
    15     list = []
       
    16 
       
    17     # Read in the file if it exists.
       
    18     if os.path.exists(file_path):
       
    19         file_in = open(file_path, "r")
       
    20 
       
    21         # Look for the list.
       
    22         list_string = ""
       
    23         parsing_multiline_list = False
       
    24         for line in file_in:
       
    25             if list_name in line or parsing_multiline_list:
       
    26                 list_string += line
       
    27 
       
    28                 # Handle multiline lists.
       
    29                 if ']' not in list_string:
       
    30                     parsing_multiline_list = True
       
    31                 else:
       
    32                     # Evaluate the list once its end is reached.
       
    33                     # Make the split function only split it once.
       
    34                     list = eval(list_string.split('=', 1)[1].strip())
       
    35                     break
       
    36 
       
    37         # Close the file
       
    38         file_in.close()
       
    39 
       
    40     return list
       
    41 
       
    42 
       
    43 def read_config_file():
       
    44     '''Reads the NS-3 configuration file and returns a list of enabled modules.
       
    45 
       
    46     This function first looks for the ns3 configuration file (.ns3rc)
       
    47     in the current working directory and then looks in the ~
       
    48     directory.
       
    49 
       
    50     '''
       
    51 
       
    52     # See if the ns3 configuration file exists in the current working
       
    53     # directory and then look for it in the ~ directory.
       
    54     dot_ns3rc_name = '.ns3rc'
       
    55     dot_ns3rc_path = dot_ns3rc_name
       
    56     if not os.path.exists(dot_ns3rc_path):
       
    57         dot_ns3rc_path = os.path.expanduser('~/') + dot_ns3rc_name
       
    58         if not os.path.exists(dot_ns3rc_path):
       
    59             # Enable all modules if the .ns3rc file can't be found.
       
    60             return ['all_modules']
       
    61 
       
    62     # Read in the ns3 configuration file.
       
    63     modules_enabled = get_list_from_file(dot_ns3rc_path, 'modules_enabled')
       
    64     if not modules_enabled:
       
    65         # Enable all modules if the modules_enabled line can't be found.
       
    66         return ['all_modules']
       
    67 
       
    68     return modules_enabled