Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/py.py @ 237:13711ba8e81e
Adjust copyright year to 2020
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 13 May 2020 09:33:34 +0200 |
| parents | bbe8513ea649 |
| children | ff964825a75a |
| 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 # :- |
|
237
13711ba8e81e
Adjust copyright year to 2020
Franz Glasner <fzglas.hg@dom66.de>
parents:
208
diff
changeset
|
3 # :Copyright: (c) 2015-2020, Franz Glasner. All rights reserved. |
|
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
|
4 # :License: 3-clause BSD. 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 |
|
19
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
12 try: |
|
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
13 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
|
14 except ImportError: |
|
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
15 try: |
|
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
16 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
|
17 except ImportError: |
|
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
18 DictImpl = dict |
|
6
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
19 |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
20 from .compat import PY2, u2fs |
|
6
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
21 |
|
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 __all__ = ["load"] |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
24 |
|
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 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
|
27 """Load Python-style configuration files. |
|
c11dd36279d9
More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
28 |
|
c11dd36279d9
More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
29 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
|
30 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
|
31 |
|
c11dd36279d9
More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
32 :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
|
33 :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
|
34 into the resulting configuration dictionary |
|
c11dd36279d9
More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
35 :returns: the configuration as dictionary |
|
c11dd36279d9
More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
36 :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
|
37 |
|
c11dd36279d9
More documentation for the configmix.py module
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
38 """ |
|
6
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
39 if extract is not None: |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
40 if not isinstance(extract, (type([]), type(tuple()), type(set()), )): |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
41 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
|
42 gcontext = DictImpl() |
|
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
43 lcontext = DictImpl() |
|
6
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
44 if PY2: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
45 execfile(u2fs(filename, True), gcontext, lcontext) # noqa: F821 |
|
6
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
46 else: |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
47 # "rb" mode allows Python to derive the encoding automatically |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
48 with open(filename, "rb") as vf: |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
49 code = compile(vf.read(), filename, "exec") |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
50 exec(code, gcontext, lcontext) |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
51 if extract is None: |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
52 if "__all__" in lcontext: |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
53 extract = lcontext["__all__"] |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
54 else: |
|
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
55 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
|
56 # |
|
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
57 # 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
|
58 # ordered list |
|
04505a8dbfc0
Use ordered dictionaries (if available) when reading Python configuration files
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
6
diff
changeset
|
59 # |
|
6
caaaddb11db1
Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
60 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
|
61 return DictImpl(zip(extract, [lcontext[v] for v in extract])) |
