annotate configmix/py.py @ 107:2ee042791197

Don't read the first configuration file a second time. Because of merging nothing bad happend with the old implementation.
author Franz Glasner <hg@dom66.de>
date Sat, 24 Mar 2018 15:34:52 +0100
parents 218807d7d883
children c11dd36279d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
79
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
2 #-
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
3 # :Copyright: (c) 2015-2018, Franz Glasner. All rights reserved.
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
4 # :License: 3-clause BSD. See LICENSE.txt for details.
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
5 #-
82
218807d7d883 Remove header markup from the Python files and put them into the doc .rst files
Franz Glasner <hg@dom66.de>
parents: 79
diff changeset
6 """Read configuration settings from Python files.
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
7
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
8 """
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
9
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
10 from __future__ import division, absolute_import, print_function
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
11
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
12 import locale
19
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
13 try:
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
14 from collections import OrderedDict as DictImpl
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
15 except ImportError:
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
16 try:
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
17 from ordereddict import OrderedDict as DictImpl
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
18 except ImportError:
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
19 DictImpl = dict
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
20
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
21 from .compat import PY2
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
22
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
24 __all__ = ["load"]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
25
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
26
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
27 def load(filename, extract=None):
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
28 if extract is not None:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
29 if not isinstance(extract, (type([]), type(tuple()), type(set()), )):
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
30 raise TypeError("`extract' must be a sequence")
19
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
31 gcontext = DictImpl()
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
32 lcontext = DictImpl()
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
33 if PY2:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
34 filename2 = filename.encode(locale.getpreferredencoding())
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
35 if PY2:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
36 execfile(filename2, gcontext, lcontext)
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
37 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
38 # "rb" mode allows Python to derive the encoding automatically
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
39 with open(filename, "rb") as vf:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
40 code = compile(vf.read(), filename, "exec")
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
41 exec(code, gcontext, lcontext)
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
42 if extract is None:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
43 if "__all__" in lcontext:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
44 extract = lcontext["__all__"]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
45 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
46 extract = [k for k in lcontext if not k.startswith('_')]
19
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
47 #
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
48 # Don't bail on non-existing keys and (implicitly) convert to an
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
49 # ordered list
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
50 #
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
51 extract = [v for v in extract if v in lcontext]
19
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
52 return DictImpl(zip(extract, [lcontext[v] for v in extract]))