Mercurial > hgrepos > Python > libs > ConfigMix
changeset 19:04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Thu, 10 Mar 2016 11:11:17 +0100 |
| parents | a04fa81e10ae |
| children | 9bdc4e421415 |
| files | configmix/py.py |
| diffstat | 1 files changed, 14 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- 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]))
