annotate configmix/py.py @ 6:caaaddb11db1

Evaluating Python configuration files
author Franz Glasner <hg@dom66.de>
date Wed, 09 Mar 2016 00:51:46 +0100
parents
children 04505a8dbfc0
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 -*-
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
2 r"""Read configuration settings from Python files
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
3
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
4 """
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
5
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
6 from __future__ import division, absolute_import, print_function
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 import locale
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 .compat import PY2
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
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
13 __all__ = ["load"]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
14
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
15
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
16 def load(filename, extract=None):
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
17 if extract is not None:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
18 if not isinstance(extract, (type([]), type(tuple()), type(set()), )):
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
19 raise TypeError("`extract' must be a sequence")
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
20 gcontext = dict()
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
21 lcontext = dict()
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
22 if PY2:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23 filename2 = filename.encode(locale.getpreferredencoding())
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
24 if PY2:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
25 execfile(filename2, gcontext, lcontext)
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
26 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
27 # "rb" mode allows Python to derive the encoding automatically
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
28 with open(filename, "rb") as vf:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
29 code = compile(vf.read(), filename, "exec")
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
30 exec(code, gcontext, lcontext)
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
31 if extract is None:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
32 if "__all__" in lcontext:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
33 extract = lcontext["__all__"]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
34 else:
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
35 extract = [k for k in lcontext if not k.startswith('_')]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
36 # don't bail on non-existing keys
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
37 extract = [v for v in extract if v in lcontext]
caaaddb11db1 Evaluating Python configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
38 return dict(zip(extract, [lcontext[v] for v in extract]))