-
Notifications
You must be signed in to change notification settings - Fork 194
/
utils.py
116 lines (89 loc) · 3.96 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/env python3
# These methods are used by test.py to look for and read the
# .ns3rc configuration file, which is used to specify the modules that
# should be enabled
import os
def get_list_from_file(file_path, list_name):
"""Looks for a Python list called list_name in the file specified
by file_path and returns it.
If the file or list name aren't found, this function will return
an empty list.
"""
# Read in the file if it exists.
if not os.path.exists(file_path):
return []
with open(file_path, "r", encoding="utf-8") as file_in:
# Look for the list.
list_string = ""
parsing_multiline_list = False
for line in file_in:
# Remove any comments.
if "#" in line:
(line, comment) = line.split("#", 1)
# Parse the line.
if list_name in line or parsing_multiline_list:
list_string += line
# Handle multiline lists.
if "]" not in list_string:
parsing_multiline_list = True
else:
# Evaluate the list once its end is reached.
# Make the split function only split it once.
return eval(list_string.split("=", 1)[1].strip())
# List name was not found
return []
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 not os.path.exists(file_path):
return value_if_missing
with open(file_path, "r", encoding="utf-8") as file_in:
# Look for the boolean variable.
for line in file_in:
# Remove any comments.
if "#" in line:
(line, comment) = line.split("#", 1)
# Parse the line.
if bool_name in line:
# Evaluate the variable's line once it is found. Make
# the split function only split it once.
return eval(line.split("=", 1)[1].strip())
# Boolean variable was not found
return value_if_missing
# Reads the NS-3 configuration file and returns a list of enabled modules.
#
# This function first looks for the ns3 configuration file (.ns3rc) in
# the current working directory and then looks in the ~ directory.
def read_config_file():
# By default, all modules will be enabled, examples will be disabled,
# and tests will be disabled.
modules_enabled = ["all_modules"]
examples_enabled = False
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):
# Return all of the default values if the .ns3rc file can't be found.
return (config_file_exists, modules_enabled, examples_enabled, tests_enabled)
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.
modules_enabled = ["all_modules"]
# Read in whether examples should be enabled or not.
value_if_missing = False
examples_enabled = get_bool_from_file(dot_ns3rc_path, "examples_enabled", value_if_missing)
# 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)