The goal of my current piece of work is to read from a global config file all the settings and then from the local file only those settings that a user has chosen.
Firstly I wrote a function but then I needed a class. It's my first working class in Python, and I ask if you can kindly note its defects. Although it works, I think it can for sure be written more correctly and concisely.
Here's all the beginning:
# files to be read from
cmd_parameter = "config_1.cfg"
cmd_parameter_global = "global.cfg"
if len(sys.argv) == 2:
cmd_parameter = sys.argv[1]
else:
print("Error: there should be one argument after program name - config file name")
exit(-1)
config_file = []
config_file_global = []
# reading user's config file
try:
with open(cmd_parameter, 'r') as config_file_name:
config_file = config_file_name.readlines()
except FileNotFoundError:
print("File '", sys.argv[1], "' not exists or not available")
exit(-1)
# reading global config file
try:
with open(cmd_parameter_global, 'r') as config_file_global_name:
config_file_global = config_file_global_name.readlines()
except FileNotFoundError:
print("Global configuration file '", cmd_parameter_global, "' not exists or not available")
exit(-1)
# parsing config file, reading values from it to list and matching parameters with a given argument
# parameters in config file should start with <conf_key_split1>, value should be separated from parameter by <conf_key_split2>
config_param_user = []
config_value_user = []
config_param_global = []
config_value_global = []
config = ""
conf_key_split1 = '.'
conf_key_split2 = '='
conf_key_split3 = ','
# reading user's config file
for line in config_file:
if line.startswith(conf_key_split1):
line = line.rstrip().split(conf_key_split1)
line = line[1]
line = line.rstrip().split(conf_key_split2)
config_param_user.append(line[0])
config_value_user.append(line[1])
# reading global config file
for line in config_file_global:
if line.startswith(conf_key_split1):
line = line.rstrip().split(conf_key_split1)
line = line[1]
line = line.rstrip().split(conf_key_split2)
config_param_global.append(line[0])
config_value_global.append(line[1])
And here is the class itself:
# class for reading config from user's and global config file the list of params names list and the list of values
class configFromFile():
def config_from_file_userfile(self, param):
index = config_param_user.index(param)
if config_value_user[index]:
config_value = config_value_user[index]
else:
print('The value of the parameter "', param, '" was not found in', cmd_parameter)
exit(-1)
return config_value
def config_from_file_global(self, param):
index = config_param_global.index(param)
if config_value_global[index]:
config_value = config_value_global[index]
else:
print('The value of the parameter "', param, '" was not found in', cmd_parameter_global)
exit(-1)
return config_value
def main(self, param):
if param in config_param_global:
config = self.config_from_file_global(param)
elif param in config_param_user:
config = self.config_from_file_userfile(param)
else:
print('The parameter "', param, '" was not found in', cmd_parameter, 'and in', cmd_parameter_global)
exit(-1)
return config
Usage:
names = configFromFile().main('names')