Trying out ordered YAML
This commit is contained in:
parent
9c1af76e9e
commit
a3bc61c97d
1 changed files with 20 additions and 1 deletions
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue