mininet-sample/core/parameter.py

57 lines
2.1 KiB
Python
Raw Normal View History

2020-06-25 12:56:47 +00:00
import logging
2020-06-24 08:54:44 +00:00
class Parameter(object):
2020-06-25 12:56:47 +00:00
"""
Generic handler for parameters stored in configuration files
Attributes:
2020-06-29 07:51:55 +00:00
parameters dictionary containing the value for configuration parameters
default_parameters dictionary containung default values for parameters
2020-06-25 12:56:47 +00:00
"""
def __init__(self, parameter_filename):
self.parameters = {}
2020-06-29 07:51:55 +00:00
self.default_parameters = {}
2020-06-25 12:56:47 +00:00
if parameter_filename is None:
logging.warning("No parameter file provided; using default parameters")
else:
2020-06-25 12:56:47 +00:00
logging.info("Extract parameters from file {}".format(parameter_filename))
self.load_parameter_file(parameter_filename)
def load_parameter_file(self, parameter_filename):
with open(parameter_filename) as f:
for line in f.readlines():
# Ignore comments
if line.startswith("#"):
continue
try:
# Also get rid of trailing characters
key, value = line.strip().split(":")
if key in self.parameters:
if not isinstance(self.parameters[key], list):
self.parameters[key] = [self.parameters[key]]
self.parameters[key].append(value)
else:
self.parameters[key] = value
except ValueError as e:
logging.warning(
"Got error '{}' for line '{}'; ignore it".format(e, line))
def get(self, key):
2020-06-25 12:56:47 +00:00
"""
2020-06-29 07:51:55 +00:00
Get the value of the parameter with key `key`.
If not defined by the configuration file, return the default value.
Raise Exception if the parameter has no default value and is absent.
2020-06-25 12:56:47 +00:00
"""
2020-06-29 07:51:55 +00:00
val = self.parameters.get(key)
if val is None:
if key in self.default_parameters:
return self.default_parameters[key]
else:
raise Exception("Parameter not found " + key)
else:
return val
def __str__(self):
2020-06-25 12:56:47 +00:00
return self.parameters.__str__()