I want to read a config file and get tag value pairs in dictionary. Code need to stip whitespace, new line and only do tag value pairs as dictionary key value. Also, need to ignore comments (starting with #)
Please note I don't have much experience working with Python so happy to be pointed out best way to achieve it or improve the whole code.
File Example:
Parser.field_msgtypes.currency = GBP,EUR
Listener.mode = //blp/ref
Parser.field_mappings.ID_SEC = SECID
Engine.session.UATD.connection.transacted = 0
InternalMessage.formatType = STRB
InternalMessage.defaultVersionId =
Here is my code:
class ConfigFileReader:
file_prefix = 'import';
file_suffix = 'properties';
pfd = '/user/myusername/dev/';
pf = '/user/myusername/';
def setConfigFileList(self):
genFile = [arg for arg in self.args[1:] if arg]; #ignore filename
custom_file_middle = None;
for arg in genFile:
if custom_file_middle is None:
custom_file_middle = arg;
else:
custom_file_middle = ".".join([custom_file_middle,arg]);
custom_file = ".".join([self.file_prefix,custom_file_middle,self.file_suffix]);
custom_file_path_pfd = "".join([self.pfd,custom_file]);
custom_file_path_pf = "".join([self.pf,custom_file]);
if os.path.exists(custom_file_path_pfd):
print ("Loading custom file...... %s" % custom_file_path_pfd);
self.dict_file_pfd[custom_file] = custom_file_path_pfd;
elif os.path.exists(custom_file_path_pf):
print ("Loading custom file...... %s" % custom_file_path_pf);
self.dict_file_pf[custom_file] = custom_file_path_pf;
else:
print ("File not found......[%s] [%s]" % (custom_file,arg));
def loadConfigFiles(self):
self.loadConfigKeys(self.dict_file_pfd);
self.loadConfigKeys(self.dict_file_pf);
def loadConfigKeys(self,dict_file):
for key in dict_file:
file_path = dict_file[key];
filename = key;
list_tvp = [];
with open(file_path,'r') as f:
list_tvp = [line.strip() for line in f if line.strip() and not line.startswith('#')];
self.dict_config.update(dict((each.split("=")[0].strip(),each.split("=")[1].strip()) for each in list_tvp if each.split("=")[0].strip() not in self.dict_config));
if __name__ == '__main__':
args = sys.argv;
config_reader = ConfigFileReader(args);
config_reader.setConfigFileList();
config_reader.loadConfigFiles();
for key,value in config_reader.dict_config.items():
print key,"=",value;
I want to change this as I feel there must be a efficient and readable version of this.
self.dict_config.update(dict((each.split("=")[0].strip(),each.split("=")[1].strip()) for each in list_tvp if each.split("=")[0].strip() not in self.dict_config));