2020-06-25 12:56:47 +00:00
|
|
|
import logging
|
2015-01-08 18:52:45 +00:00
|
|
|
|
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:
|
|
|
|
parameters dictionary containing the value for configuration parameters
|
|
|
|
"""
|
|
|
|
def __init__(self, parameter_filename):
|
|
|
|
self.parameters = {}
|
|
|
|
if parameter_filename is None:
|
|
|
|
logging.warning("No parameter file provided; using default parameters")
|
2020-06-24 15:18:40 +00:00
|
|
|
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))
|
2020-06-24 15:18:40 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
def get(self, key):
|
2020-06-25 12:56:47 +00:00
|
|
|
"""
|
|
|
|
Get the parameter with key `key`. If it does not exist, return None
|
|
|
|
"""
|
|
|
|
return self.parameters.get(key)
|
2020-06-24 15:18:40 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-06-25 12:56:47 +00:00
|
|
|
return self.parameters.__str__()
|