annotate configmix/py.py @ 125:c11dd36279d9

More documentation for the configmix.py module
author Franz Glasner <f.glasner@feldmann-mg.com>
date Wed, 04 Apr 2018 10:56:54 +0200
parents 218807d7d883
children e2e8d21b4122
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):
125
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
28 """Load Python-style configuration files.
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
29
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
30 Files are loaded and **executed** with :func:`exec` using an empty
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
31 dict as global and local context.#
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
32
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
33 :param filename: the path to the configuration file
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
34 :param extract: an optional list of variable names (keys) to extract
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
35 into the resulting configuration dictionary
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
36 :returns: the configuration as dictionary
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
37 :rtype: collections.OrderedDict or ordereddict.OrderedDict or dict
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
38
c11dd36279d9 More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
39 """
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
40 if extract is not None:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
41 if not isinstance(extract, (type([]), type(tuple()), type(set()), )):
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
42 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
43 gcontext = DictImpl()
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
44 lcontext = DictImpl()
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
45 if PY2:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
46 filename2 = filename.encode(locale.getpreferredencoding())
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
47 if PY2:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
48 execfile(filename2, gcontext, lcontext)
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
49 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
50 # "rb" mode allows Python to derive the encoding automatically
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
51 with open(filename, "rb") as vf:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
52 code = compile(vf.read(), filename, "exec")
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
53 exec(code, gcontext, lcontext)
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
54 if extract is None:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
55 if "__all__" in lcontext:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
56 extract = lcontext["__all__"]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
57 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
58 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
59 #
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
60 # 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
61 # ordered list
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
62 #
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
63 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
64 return DictImpl(zip(extract, [lcontext[v] for v in extract]))