# HG changeset patch # User Franz Glasner # Date 1457604677 -3600 # Node ID 04505a8dbfc071a1b28ac840772cf04cff4ba31f # Parent a04fa81e10ae8659442bf6f9839bd2a021eb0476 Use ordered dictionaries (if available) when reading Python configuration files diff -r a04fa81e10ae -r 04505a8dbfc0 configmix/py.py --- a/configmix/py.py Thu Mar 10 10:45:09 2016 +0100 +++ b/configmix/py.py Thu Mar 10 11:11:17 2016 +0100 @@ -6,6 +6,13 @@ from __future__ import division, absolute_import, print_function import locale +try: + from collections import OrderedDict as DictImpl +except ImportError: + try: + from ordereddict import OrderedDict as DictImpl + except ImportError: + DictImpl = dict from .compat import PY2 @@ -17,8 +24,8 @@ if extract is not None: if not isinstance(extract, (type([]), type(tuple()), type(set()), )): raise TypeError("`extract' must be a sequence") - gcontext = dict() - lcontext = dict() + gcontext = DictImpl() + lcontext = DictImpl() if PY2: filename2 = filename.encode(locale.getpreferredencoding()) if PY2: @@ -33,6 +40,9 @@ extract = lcontext["__all__"] else: extract = [k for k in lcontext if not k.startswith('_')] - # don't bail on non-existing keys + # + # Don't bail on non-existing keys and (implicitly) convert to an + # ordered list + # extract = [v for v in extract if v in lcontext] - return dict(zip(extract, [lcontext[v] for v in extract])) + return DictImpl(zip(extract, [lcontext[v] for v in extract]))