Page MenuHomec4science

replace_in_config.py
No OneTemporary

File Metadata

Created
Wed, Nov 6, 07:27

replace_in_config.py

import logging
import yaml
import re
def replace_in_config(paths):
"""
Replaces the YAML configuration file and return a dictionary with all the configuration parameters.
@author: Balazs Laurenczy
@date: 2017-2018
"""
# do the "root" parts replacing in the paths
logging.debug("Replacing '__XXXX_ROOT__' tags ...")
is_match = True
# repeat until no more roots have been replaced
while is_match:
is_match = False
# loop through each key/value pair in the "path" dictionary
for key, value in paths.items():
# replace the "__XXX_ROOT__" tags in the path with their corresponding path entry
# for example: "__DATA_ROOT__" should be replaced with paths['data_root']
if re.match('__\w+_ROOT__', value):
# a match was found, make sure to loop again
is_match = True
# extract the different parts of the path
re_match = re.match('(.*)__(\w+)_ROOT__(.*)', value)
# get the root's name, like "DATA" or "REF"
root_name = re_match.groups()[1]
# get the lower case version ("data" or "ref")
root_name_lower = root_name.lower()
# replace the path: for example "__DATA_ROOT__" with paths['data_root']
paths[key] = value.replace("__" + root_name + "_ROOT__", paths[root_name_lower + '_root'])
# remove the beginning './' from all the paths
logging.debug("Replacing './' ...")
is_match = True
# repeat until no more replacements have been done
while is_match:
is_match = False
# loop through each key/value pair in the "path" dictionary
for key, value in paths.items():
if re.match('^\./', value):
# a match was found, make sure to loop again
is_match = True
# remove the beginning of the path (the './')
paths[key] = value[2:]
return paths

Event Timeline