1
0
Fork 0
mirror of synced 2024-12-23 22:43:23 -05:00

Trying out ordered YAML

This commit is contained in:
Brian Knobbs 2015-06-12 10:10:47 -04:00
parent 9c1af76e9e
commit a3bc61c97d

View file

@ -1,5 +1,24 @@
import yaml
from .util import string
from collections import OrderedDict
def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
""" Stolen from SO - http://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts
Loads a YAML file using an OrderedDict so the ordering of the values are
maintained
"""
class OrderedLoader(Loader):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_mapping)
return yaml.load(stream, OrderedLoader)
class ConfigReader(object):
def __init__(self, config_file_path):
@ -8,7 +27,7 @@ class ConfigReader(object):
def _read(self, config_file_path):
try:
with open(config_file_path) as fin:
data = yaml.load(fin)
data = ordered_load(fin)
return data
except Exception as e:
msg = string.indent_lines(str(e))