def load_ordered_config(config_path):
"""
Loads the configuration in the same order as it's defined in yaml file,
so that, while saving it in new format, order is maintained
Args:
config_path (str): Path to the configuration file
Returns:
config(dict): Returns the configurations in the defined ordered
"""
# To load data from yaml in ordered dict format
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
def dict_representer(dumper, data):
return dumper.represent_mapping(_mapping_tag, data.iteritems())
def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))
yaml.add_representer(collections.OrderedDict, dict_representer)
yaml.add_constructor(_mapping_tag, dict_constructor)
# format the output to print a blank scalar rather than null
def represent_none(self, _):
return self.represent_scalar('tag:yaml.org,2002:null', u'')
yaml.add_representer(type(None), represent_none)
# read input from home directory for pull requests
with open(config_path, 'r') as f:
config = yaml.load(f)
return config
评论列表
文章目录