annotate configmix/py.py @ 58:95034b28f285

Include the LICENSE into the Sphinx documentation
author Franz Glasner <hg@dom66.de>
date Wed, 28 Feb 2018 01:19:37 +0100
parents aa8345dae995
children a43749f751e0
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 -*-
54
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 19
diff changeset
2 r"""
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 19
diff changeset
3 configmix.py
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 19
diff changeset
4 ^^^^^^^^^^^^
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 19
diff changeset
5
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 19
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]))