annotate configmix/py.py @ 633:9ab72688278b v0.20.4

+++++ v0.20.4
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 17 Jan 2022 09:17:30 +0100
parents f454889e41fa
children
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 -*-
208
bbe8513ea649 Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents: 207
diff changeset
2 # :-
593
f454889e41fa Adjust copyright year (the end) to 2022
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
3 # :Copyright: (c) 2015-2022, Franz Glasner. All rights reserved.
296
eed16a1ec8f3 Use SPDX license identifiers (either full or short) all over the package
Franz Glasner <fzglas.hg@dom66.de>
parents: 250
diff changeset
4 # :License: BSD-3-Clause. See LICENSE.txt for details.
208
bbe8513ea649 Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents: 207
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
250
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
12
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
13 __all__ = ["load"]
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
14
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
15
19
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 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
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 try:
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
20 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
21 except ImportError:
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
22 DictImpl = dict
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
24 from .compat import PY2, u2fs
6
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:
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
46 execfile(u2fs(filename, True), gcontext, lcontext) # noqa: F821
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
47 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
48 # "rb" mode allows Python to derive the encoding automatically
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
49 with open(filename, "rb") as vf:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
50 code = compile(vf.read(), filename, "exec")
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
51 exec(code, gcontext, lcontext)
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
52 if extract is None:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
53 if "__all__" in lcontext:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
54 extract = lcontext["__all__"]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
55 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
56 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
57 #
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
58 # 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
59 # ordered list
04505a8dbfc0 Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 6
diff changeset
60 #
6
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
61 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
62 return DictImpl(zip(extract, [lcontext[v] for v in extract]))