Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/config.py @ 738:20e3c2d956d9
More tests for nested filters
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 29 Oct 2023 11:36:25 +0100 |
| parents | 75d37575472b |
| children | cabd1046d95f |
| rev | line source |
|---|---|
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
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:
554
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:
251
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 """The unified configuration dictionary with attribute support or |
|
54
aa8345dae995
Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents:
40
diff
changeset
|
7 variable substitution. |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
8 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
9 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
10 |
|
482
8b8ffb74d452
Remove unneeded __future__ import because we are on Python 2.6+
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
481
diff
changeset
|
11 from __future__ import division, absolute_import, print_function |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
12 |
|
250
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
13 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
14 __all__ = ["Configuration"] |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
15 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
16 |
|
39
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
17 import warnings |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
18 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
19 from collections import OrderedDict as ConfigurationBase |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
20 except ImportError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
21 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
22 from ordereddict import OrderedDict as ConfigurationBase |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
23 except ImportError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
24 ConfigurationBase = dict |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
25 try: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
26 from urllib.parse import urlsplit |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
27 except ImportError: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
28 from urlparse import urlsplit |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
29 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
30 from .variables import lookup_varns, lookup_filter |
|
487
d7f6f2afcee2
Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
485
diff
changeset
|
31 from .compat import u, uchr, n, str_and_u, PY2 |
|
637
4499e9b4855d
The ``{{::DEL::}}`` is not subject to interpolation any more.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
610
diff
changeset
|
32 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER, DEL_VALUE |
|
542
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
33 try: |
|
550
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
34 from ._speedups import (fast_unquote, fast_quote, fast_pathstr2path, |
|
554
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
35 _fast_split_ns, _fast_split_filters, |
|
610
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
36 _fast_getvarl, _fast_getvarl_s, |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
37 _fast_getvar, _fast_getvar_s, |
|
554
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
38 _fast_interpolate_variables, |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
39 _sync_MISSING, _sync_MARKER) |
|
542
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
40 except ImportError: |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
41 fast_unquote = None |
|
550
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
42 fast_quote = None |
|
542
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
43 fast_pathstr2path = None |
|
543
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
44 _fast_split_ns = None |
|
552
39e5d07d8dbc
Provide a C implementation of configmix.config._split_filters.
Franz Glasner <fzglas.hg@dom66.de>
parents:
551
diff
changeset
|
45 _fast_split_filters = None |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
46 _fast_getvarl = None |
|
610
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
47 _fast_getvarl_s = None |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
48 _fast_getvar = None |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
49 _fast_getvar_s = None |
|
554
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
50 _fast_interpolate_variables = None |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
51 _sync_MISSING = None |
|
713
1832c5d1bd00
Handle _sync_MARKER exactly as _sync_MISSING.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
712
diff
changeset
|
52 _sync_MARKER = None |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
53 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
54 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
55 _MARKER = object() |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
56 _MISSING = object() |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
57 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
58 |
|
17
94b5e94fae44
Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
16
diff
changeset
|
59 class _AttributeDict(ConfigurationBase): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
60 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
61 def __getattr__(self, name): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
62 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
63 v = self[name] |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
64 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
65 raise AttributeError("%s has no attribute %r" % (type(self), name)) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
66 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
67 # Wrap a dict into another dict with attribute access support |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
68 if isinstance(v, dict): |
|
29
17af7a78710c
FIX: Renaming a class was not really complete
Franz Glasner <hg@dom66.de>
parents:
25
diff
changeset
|
69 return _AttributeDict(v) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
70 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
71 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
72 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
73 |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
74 class CoercingMethodsMixin(object): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
75 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
76 """Mixin to provide some common implementations for retrieval |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
77 methods that convert return values to a fixed type (int, bool, |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
78 float). |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
79 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
80 Both :class:`~.Configuration` and :class:`~._JailedConfiguration` use |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
81 this mixin. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
82 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
83 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
84 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
85 def getintvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
86 """Get a (possibly substituted) variable and coerce text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
87 number. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
88 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
89 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
90 s = self.getvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
91 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
92 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
93 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
94 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
95 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
96 def getfirstintvarl_s(self, *paths, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
97 """Get a (possibly substituted) variable and coerce text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
98 number. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
99 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
100 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
101 s = self.getfirstvarl_s(*paths, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
102 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
103 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
104 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
105 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
106 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
107 def getintvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
108 """Get a (possibly substituted) variable and coerce text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
109 number. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
110 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
111 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
112 s = self.getvar_s(varname, default=default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
113 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
114 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
115 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
116 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
117 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
118 def getfirstintvar_s(self, *varnames, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
119 """A variant of :meth:`~.getintvar_s` that returns the first found |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
120 variable in the list of given variables in `varnames`. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
121 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
122 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
123 s = self.getfirstvar_s(*varnames, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
124 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
125 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
126 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
127 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
128 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
129 def getboolvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
130 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
131 boolean |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
132 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
133 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
134 s = self.getvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
135 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
136 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
137 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
138 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
139 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
140 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
141 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
142 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
143 def getfirstboolvarl_s(self, *paths, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
144 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
145 boolean |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
146 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
147 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
148 s = self.getfirstvarl_s(*paths, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
149 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
150 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
151 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
152 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
153 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
154 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
155 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
156 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
157 def getboolvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
158 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
159 boolean |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
160 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
161 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
162 s = self.getvar_s(varname, default=default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
163 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
164 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
165 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
166 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
167 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
168 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
169 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
170 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
171 def getfirstboolvar_s(self, *varnames, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
172 """A variant of :meth:`~.getboolvar_s` that returns the first found |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
173 variable in the list of given variables in `varnames`. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
174 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
175 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
176 s = self.getfirstvar_s(*varnames, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
177 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
178 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
179 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
180 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
181 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
182 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
183 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
184 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
185 # Conversion of booleans |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
186 _BOOL_CVT = { |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
187 u('1'): True, u('yes'): True, u('true'): True, u('on'): True, |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
188 u('0'): False, u('no'): False, u('false'): False, u('off'): False |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
189 } |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
190 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
191 def getfloatvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
192 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
193 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
194 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
195 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
196 s = self.getvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
197 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
198 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
199 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
200 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
201 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
202 def getfirstfloatvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
203 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
204 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
205 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
206 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
207 s = self.getfirstvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
208 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
209 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
210 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
211 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
212 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
213 def getfloatvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
214 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
215 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
216 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
217 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
218 s = self.getvar_s(varname, default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
219 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
220 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
221 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
222 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
223 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
224 def getfirstfloatvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
225 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
226 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
227 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
228 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
229 s = self.getfirstvar_s(varname, default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
230 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
231 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
232 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
233 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
234 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
235 |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
236 # Speed |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
237 _EMPTY_STR = u("") |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
238 _TEXTTYPE = type(_EMPTY_STR) |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
239 _STARTTOK = u(b"{{") |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
240 _ENDTOK = u(b"}}") |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
241 _HIER_SEPARATOR = u(b'.') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
242 _NS_SEPARATOR = u(b':') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
243 _FILTER_SEPARATOR = u(b'|') |
|
707
10fbc23b4dba
Rename internal variable FILTER_SEPARATOR_REV -> FILTER_SEPARATOR_2
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
705
diff
changeset
|
244 _FILTER_SEPARATOR_2 = u(b',') |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
245 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
246 _ENDTOK_REF = _ENDTOK |
|
703
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
247 _ENDTOK_FILTER = _FILTER_SEPARATOR + _ENDTOK |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
248 _DOT = u(b'.') |
|
656
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
249 _TILDE = u(b'~') |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
250 _QUOTE = u(b'%') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
251 _QUOTE_x = u(b'x') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
252 _QUOTE_u = u(b'u') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
253 _QUOTE_U = u(b'U') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
254 _COMMENT = u(b'#') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
255 _QUOTE_MAP = { |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
256 0x25: u(b'%x25'), # _QUOTE |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
257 0x2e: u(b'%x2e'), # _DOT |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
258 0x3a: u(b'%x3a'), # _NS_SEPARATOR |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
259 0x23: u(b'%x23'), # _COMMENT / anchor |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
260 0x7c: u(b'%x7c'), # _FILTER_SEPARATOR |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
261 0x22: u(b'%x22'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
262 0x27: u(b'%x27'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
263 0x7b: u(b'%x7b'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
264 0x7d: u(b'%x7d'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
265 0x5b: u(b'%x5b'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
266 0x5d: u(b'%x5d'), |
|
656
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
267 0x7e: u(b'%x7e'), # tilde `~` |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
268 } |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
269 _QUOTE_SAFE = u(b'abcdefghijklmnopqrstuvwxyz' |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
270 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
271 b'0123456789' |
|
656
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
272 b'-_@!$&/\\()=?*+;,<>^') |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
273 """Mostly used configuration key characters that do not need any quoting |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
274 |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
275 """ |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
276 |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
277 |
|
550
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
278 def py_quote(s): |
|
502
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
279 """Replace important special characters in string `s` by replacing |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
280 them with ``%xNN`` where `NN` are the two hexadecimal digits of the |
| 702 | 281 character's unicode codepoint value. |
|
502
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
282 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
283 Handled are the important special chars: ``%``, ``.``, ``:``, |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
284 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``. |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
285 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
286 See also the :ref:`quoting` section. |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
287 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
288 """ |
|
656
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
289 try: |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
290 # Quick check whether all of the chars are in _QUOTE_SAFE |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
291 if not s.lstrip(_QUOTE_SAFE): |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
292 return s |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
293 except AttributeError: |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
294 # |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
295 # Check whether s is an index (int) and return the special tag if |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
296 # it is so |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
297 # |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
298 if isinstance(s, int): |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
299 return "~%d~" % (s, ) |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
300 else: |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
301 raise |
|
502
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
302 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
303 # Slow path |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
304 re_encode = False |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
305 if PY2: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
306 # Use the Unicode translation variant in PY2 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
307 if isinstance(s, str): |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
308 s = s.decode("latin1") |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
309 re_encode = True |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
310 s = s.translate(_QUOTE_MAP) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
311 if re_encode: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
312 return s.encode("latin1") |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
313 else: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
314 return s |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
315 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
316 |
|
550
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
317 if fast_quote: |
|
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
318 quote = fast_quote |
|
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
319 else: |
|
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
320 quote = py_quote |
|
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
321 |
|
79db28e879f8
Provide a C-implementation of configmix.config.quote() also: fast_quote
Franz Glasner <fzglas.hg@dom66.de>
parents:
548
diff
changeset
|
322 |
|
542
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
323 def py_unquote(s): |
|
502
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
324 """Unquote the content of `s`: handle all patterns ``%xNN``, |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
325 ``%uNNNN`` or ``%UNNNNNNNN``. |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
326 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
327 This is the inverse of :func:`.quote`. |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
328 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
329 """ |
|
656
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
330 s_len = len(s) |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
331 if s_len > 2 and s[0] == _TILDE and s[-1] == _TILDE: |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
332 try: |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
333 v = int(s[1:-1], 10) |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
334 if v // 10 > 3275: # be compatible to the fast C implementation |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
335 raise OverflowError("index too large") |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
336 return v |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
337 except (ValueError, OverflowError): |
|
2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
Franz Glasner <fzglas.hg@dom66.de>
parents:
655
diff
changeset
|
338 pass |
|
502
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
339 if _QUOTE not in s: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
340 return s |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
341 parts = s.split(_QUOTE) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
342 res = [parts[0]] |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
343 res_append = res.append |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
344 for p in parts[1:]: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
345 try: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
346 qc = p[0] |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
347 except IndexError: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
348 raise ValueError("unknown quote syntax string: {}".format(s)) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
349 if qc == _QUOTE_x: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
350 if len(p) < 3: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
351 raise ValueError("quote syntax: length too small") |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
352 res_append(uchr(int(p[1:3], 16))) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
353 res_append(p[3:]) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
354 elif qc == _QUOTE_u: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
355 if len(p) < 5: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
356 raise ValueError("quote syntax: length too small") |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
357 res_append(uchr(int(p[1:5], 16))) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
358 res_append(p[5:]) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
359 elif qc == _QUOTE_U: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
360 if len(p) < 9: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
361 raise ValueError("quote syntax: length too small") |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
362 res_append(uchr(int(p[1:9], 16))) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
363 res_append(p[9:]) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
364 else: |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
365 raise ValueError("unknown quote syntax string: {}".format(s)) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
366 return _EMPTY_STR.join(res) |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
367 |
|
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
368 |
|
542
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
369 if fast_unquote: |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
370 unquote = fast_unquote |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
371 else: |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
372 unquote = py_unquote |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
373 |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
374 |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
375 def py_pathstr2path(varname): |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
376 """Parse a dot-separated path string `varname` into a tuple of |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
377 unquoted path items |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
378 |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
379 :param str varname: The quoted and dot-separated path string |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
380 :return: The unquoted and parsed path items |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
381 :rtype: tuple |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
382 |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
383 Used e.g. by :meth:`~.Configuration.getvar`, |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
384 :meth:`~.Configuration.getvar_s` and :meth:`~.Configuration.jailed`. |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
385 |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
386 The returned value is suitable as input for |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
387 :meth:`~.Configuration.getvarl`, :meth:`~.Configuration.getvarl_s` and |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
388 friends. |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
389 |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
390 An empty `varname` returns an empty tuple. |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
391 |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
392 """ |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
393 # |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
394 # Because str.split yields a non-empty list for an empty string handle |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
395 # the empty string separately. |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
396 # |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
397 if varname: |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
398 return tuple([unquote(p) for p in varname.split(_HIER_SEPARATOR)]) |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
399 else: |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
400 return tuple() |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
401 |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
402 |
|
542
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
403 if fast_pathstr2path: |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
404 pathstr2path = fast_pathstr2path |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
405 else: |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
406 pathstr2path = py_pathstr2path |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
407 |
|
f71d34dda19f
Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path.
Franz Glasner <fzglas.hg@dom66.de>
parents:
541
diff
changeset
|
408 |
|
543
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
409 def _py_split_ns(varname): |
|
541
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
410 """Split the variable name string `varname` into the namespace and |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
411 the namespace-specific name |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
412 |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
413 :type varname: str |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
414 :return: A tuple containing the namespace (or `None`) and the |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
415 namespace-specific (variable-)name |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
416 :rtype: tuple(str or None, str) |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
417 |
|
543
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
418 .. note:: The returned namespace may be an empty string if the namespace |
|
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
419 separator is found. |
|
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
420 |
|
541
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
421 """ |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
422 ns, sep, rest = varname.partition(_NS_SEPARATOR) |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
423 if sep: |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
424 return (unquote(ns), rest) |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
425 else: |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
426 return (None, ns) |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
427 |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
428 |
|
543
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
429 if _fast_split_ns: |
|
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
430 _split_ns = _fast_split_ns |
|
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
431 else: |
|
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
432 _split_ns = _py_split_ns |
|
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
433 |
|
491413368c7c
Added also a fast C-implementation of configmix.config._split_ns
Franz Glasner <fzglas.hg@dom66.de>
parents:
542
diff
changeset
|
434 |
|
705
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
435 def _py_split_filters(varname, direction): |
|
541
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
436 """Split off the filter part from the `varname` string |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
437 |
|
705
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
438 :param str varname: The string where to search for filters |
|
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
439 :param int direction: +1 means to do a forward search, -1 a backward search |
|
541
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
440 :return: The tuple of the variable name without the filters and a list |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
441 of filters |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
442 :rtype: tuple(str, list) |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
443 |
|
25b61f0a1958
Docs for _split_ns() and _split_filters()
Franz Glasner <fzglas.hg@dom66.de>
parents:
540
diff
changeset
|
444 """ |
|
705
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
445 if direction == 1: |
|
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
446 name, sep, filters = varname.partition(_FILTER_SEPARATOR) |
|
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
447 elif direction == -1: |
|
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
448 name, sep, filters = varname.rpartition(_FILTER_SEPARATOR) |
|
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
449 else: |
|
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
450 raise ValueError("`direction' must be -1 or +1") |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
451 if sep: |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
452 filters = filters.strip() |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
453 if filters: |
|
705
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
454 if direction == 1: |
|
708
e692216f8756
Allow also "," characters to act as a separator within a filter-chain.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
707
diff
changeset
|
455 if _FILTER_SEPARATOR_2 in filters: |
|
e692216f8756
Allow also "," characters to act as a separator within a filter-chain.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
707
diff
changeset
|
456 return (name, filters.split(_FILTER_SEPARATOR_2)) |
|
e692216f8756
Allow also "," characters to act as a separator within a filter-chain.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
707
diff
changeset
|
457 else: |
|
e692216f8756
Allow also "," characters to act as a separator within a filter-chain.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
707
diff
changeset
|
458 return (name, filters.split(_FILTER_SEPARATOR)) |
|
705
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
459 else: |
|
707
10fbc23b4dba
Rename internal variable FILTER_SEPARATOR_REV -> FILTER_SEPARATOR_2
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
705
diff
changeset
|
460 return (name, filters.split(_FILTER_SEPARATOR_2)) |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
461 else: |
|
553
9d2bd411f5c5
Do not rstrip() the remaining variable name when parsing out filters from variable names
Franz Glasner <fzglas.hg@dom66.de>
parents:
552
diff
changeset
|
462 return (name, []) |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
463 else: |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
464 return (name, []) |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
465 |
|
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
466 |
|
552
39e5d07d8dbc
Provide a C implementation of configmix.config._split_filters.
Franz Glasner <fzglas.hg@dom66.de>
parents:
551
diff
changeset
|
467 if _fast_split_filters: |
|
39e5d07d8dbc
Provide a C implementation of configmix.config._split_filters.
Franz Glasner <fzglas.hg@dom66.de>
parents:
551
diff
changeset
|
468 _split_filters = _fast_split_filters |
|
39e5d07d8dbc
Provide a C implementation of configmix.config._split_filters.
Franz Glasner <fzglas.hg@dom66.de>
parents:
551
diff
changeset
|
469 else: |
|
39e5d07d8dbc
Provide a C implementation of configmix.config._split_filters.
Franz Glasner <fzglas.hg@dom66.de>
parents:
551
diff
changeset
|
470 _split_filters = _py_split_filters |
|
39e5d07d8dbc
Provide a C implementation of configmix.config._split_filters.
Franz Glasner <fzglas.hg@dom66.de>
parents:
551
diff
changeset
|
471 |
|
39e5d07d8dbc
Provide a C implementation of configmix.config._split_filters.
Franz Glasner <fzglas.hg@dom66.de>
parents:
551
diff
changeset
|
472 |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
473 class Configuration(CoercingMethodsMixin, _AttributeDict): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
474 |
| 712 | 475 """The configuration dictionary with attribute support and |
| 476 variable interpolation/substitution. | |
| 18 | 477 |
|
711
6557cf9ecea5
FIX: When using attribute access for configurations the values are interpolated since long ago: fix the docu and add a test for that behaviour
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
708
diff
changeset
|
478 .. note:: When retrieving by attribute names variables will be |
|
6557cf9ecea5
FIX: When using attribute access for configurations the values are interpolated since long ago: fix the docu and add a test for that behaviour
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
708
diff
changeset
|
479 interpolated. |
| 18 | 480 |
| 481 """ | |
| 482 | |
|
404
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
483 is_jail = False |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
484 """Flag to show that this is not a jail for another configuration""" |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
485 |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
486 def __init__(self, *args, **kwds): |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
487 # |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
488 # PY2.7 compat: must be set before calling the superclass' __init__ |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
489 # |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
490 self.enable_cache() |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
491 super(Configuration, self).__init__(*args, **kwds) |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
492 |
|
506
dffa692751b1
Implement clear_cache() for the configuration
Franz Glasner <fzglas.hg@dom66.de>
parents:
504
diff
changeset
|
493 def clear_cache(self): |
| 529 | 494 """Clear the internal lookup cache and the interpolation cache""" |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
495 if self.__lookup_cache is not None: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
496 self.__lookup_cache.clear() |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
497 if self.__interpolation_cache is not None: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
498 self.__interpolation_cache.clear() |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
499 |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
500 def disable_cache(self): |
|
731
75d37575472b
Docs: Add does to some methods that are yet missing docs
Franz Glasner <fzglas.hg@dom66.de>
parents:
723
diff
changeset
|
501 """Disable the internal lookup cache and the interpolation cache""" |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
502 self.__lookup_cache = None |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
503 self.__interpolation_cache = None |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
504 |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
505 def enable_cache(self): |
|
731
75d37575472b
Docs: Add does to some methods that are yet missing docs
Franz Glasner <fzglas.hg@dom66.de>
parents:
723
diff
changeset
|
506 """Enable the internal lookup cache and the interpolation cache. |
|
75d37575472b
Docs: Add does to some methods that are yet missing docs
Franz Glasner <fzglas.hg@dom66.de>
parents:
723
diff
changeset
|
507 |
|
75d37575472b
Docs: Add does to some methods that are yet missing docs
Franz Glasner <fzglas.hg@dom66.de>
parents:
723
diff
changeset
|
508 The caches are empty after enabling. |
|
75d37575472b
Docs: Add does to some methods that are yet missing docs
Franz Glasner <fzglas.hg@dom66.de>
parents:
723
diff
changeset
|
509 |
|
75d37575472b
Docs: Add does to some methods that are yet missing docs
Franz Glasner <fzglas.hg@dom66.de>
parents:
723
diff
changeset
|
510 """ |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
511 self.__lookup_cache = {} |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
512 self.__interpolation_cache = {} |
|
506
dffa692751b1
Implement clear_cache() for the configuration
Franz Glasner <fzglas.hg@dom66.de>
parents:
504
diff
changeset
|
513 |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
514 def __getitem__(self, key): |
| 469 | 515 """Mapping and list interface that forwards to :meth:`~.getvarl_s` |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
516 |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
517 """ |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
518 if isinstance(key, (tuple, list)): |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
519 return self.getvarl_s(*key) |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
520 else: |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
521 return self.getvarl_s(key) |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
522 |
|
442
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
523 def get(self, key, default=None): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
524 """Mapping interface that forwards to :meth:`~.getvarl_s` |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
525 |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
526 """ |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
527 if isinstance(key, (tuple, list)): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
528 return self.getvarl_s(*key, default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
529 else: |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
530 return self.getvarl_s(key, default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
531 |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
532 def __contains__(self, key): |
| 469 | 533 """Containment test""" |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
534 if isinstance(key, (tuple, list)): |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
535 # No namespace and quoting support here |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
536 try: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
537 self._lookupvar(*key) |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
538 except KeyError: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
539 return False |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
540 else: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
541 return True |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
542 else: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
543 return super(Configuration, self).__contains__(key) |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
544 |
|
443
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
545 def getitem_ns(self, key): |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
546 """Just forward to the *original* :meth:`dict.__getitem__`. |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
547 |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
548 No variable interpolation and key path access. |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
549 |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
550 """ |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
551 return super(Configuration, self).__getitem__(key) |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
552 |
|
517
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
553 def items(self): |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
554 """Items without interpolation""" |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
555 for k in self: |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
556 yield (k, self.getitem_ns(k)) |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
557 |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
558 def values(self): |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
559 """Values without interpolation""" |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
560 for k in self: |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
561 yield self.getitem_ns(k) |
|
328af767f5f8
Implement Configuration.items() and Configuration.values() without interpolating the values
Franz Glasner <fzglas.hg@dom66.de>
parents:
516
diff
changeset
|
562 |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
563 def py_getvarl(self, *path, **kwds): |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
564 """Get a variable where the hierarchy is given in `path` as sequence |
|
380
bb4a90fb58e0
Docu: "namespace" in getvarl() is a keyword argument
Franz Glasner <fzglas.hg@dom66.de>
parents:
374
diff
changeset
|
565 and the namespace is given in the `namespace` keyword argument. |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
566 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
567 No variable interpolation is done and no filters are applied. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
568 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
569 Quoting of `path` and `namespace` is *not* needed and wrong. |
| 333 | 570 |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
571 """ |
|
321
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
572 default = kwds.pop("default", _MARKER) |
|
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
573 namespace = kwds.pop("namespace", None) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
574 try: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
575 if not namespace: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
576 lookupfn = self._lookupvar |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
577 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
578 if namespace == REF_NAMESPACE: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
579 lookupfn = self._lookupref |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
580 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
581 lookupfn = lookup_varns(namespace) |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
582 varvalue = lookupfn(*path) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
583 except KeyError: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
584 if default is _MARKER: |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
585 raise KeyError("Variable %r not found" % (path,)) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
586 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
587 return default |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
588 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
589 return varvalue |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
590 |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
591 if _fast_getvarl: |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
592 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
593 def fast_getvarl(self, *args, **kwds): |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
594 return _fast_getvarl(self, args, **kwds) |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
595 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
596 getvarl = fast_getvarl |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
597 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
598 else: |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
599 getvarl = py_getvarl |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
600 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
601 def getkeysl(self, *path, **kwds): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
602 """Yield the keys of a variable value. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
603 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
604 :rtype: A generator |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
605 :raise KeyError: |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
606 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
607 .. note:: Dictionary keys are not subject to interpolation. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
608 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
609 """ |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
610 if "default" in kwds: |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
611 raise TypeError("got unexpected keyword argument: default") |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
612 for k in self.getvarl(*path, **kwds).keys(): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
613 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
614 |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
615 def getfirstvarl(self, *paths, **kwds): |
|
385
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
616 """A variant of :meth:`~.getvarl` that returns the first found |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
617 variable in the `paths` list. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
618 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
619 Every item in `paths` is either a `tuple` or `list` or a `dict`. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
620 If the path item is a `dict` then it must have two keys "namespace" |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
621 and "path". |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
622 If the path item is a `list` or `tuple` then the namespace is assumed |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
623 to be `None`. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
624 |
| 389 | 625 Note that a caller that wants to use variables from a non-default |
| 626 namespace must use a sequence of dicts. | |
| 627 | |
|
385
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
628 No variable interpolation is done and no filters are applied. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
629 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
630 Quoting of anything in `paths` is *not* needed and wrong. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
631 |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
632 """ |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
633 default = kwds.pop("default", _MARKER) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
634 for path in paths: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
635 if isinstance(path, (list, tuple)): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
636 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
637 varvalue = self.getvarl(*path) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
638 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
639 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
640 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
641 return varvalue |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
642 elif isinstance(path, dict): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
643 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
644 namespace = path["namespace"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
645 p = path["path"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
646 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
647 raise TypeError("a paths dict item must have a `path'" |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
648 " and a `namespace' key") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
649 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
650 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
651 varvalue = self.getvarl(*p, namespace=namespace) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
652 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
653 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
654 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
655 return varvalue |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
656 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
657 raise TypeError("a paths item must be a dict, list or tuple") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
658 if default is _MARKER: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
659 raise KeyError( |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
660 "none of the given variables found: %r" % (paths,)) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
661 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
662 return default |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
663 |
|
610
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
664 def py_getvar(self, varname, default=_MARKER): |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
665 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
666 variables from other namespaces. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
667 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
668 No variable interpolation is done and no filters are applied. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
669 |
| 333 | 670 Special characters (e.g. ``:`` and ``.``) must be quoted when using |
| 671 the default namespace. | |
| 672 | |
|
502
4f90e1eb7af8
Make quote() and unquote() module globals and also export from configmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
500
diff
changeset
|
673 See also :func:`.quote`. |
| 333 | 674 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
675 """ |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
676 varns, varname = _split_ns(varname) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
677 if not varns: |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
678 return self.getvarl(*pathstr2path(varname), default=default) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
679 else: |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
680 return self.getvarl(varname, namespace=varns, default=default) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
681 |
|
610
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
682 if _fast_getvar: |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
683 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
684 def fast_getvar(self, varname, default=_MARKER): |
|
640
d35f41e15404
Style: remove trailing semicolon in Python script
Franz Glasner <fzglas.hg@dom66.de>
parents:
637
diff
changeset
|
685 return _fast_getvar(self, varname, default) |
|
610
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
686 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
687 getvar = fast_getvar |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
688 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
689 else: |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
690 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
691 getvar = py_getvar |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
692 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
693 def getkeys(self, varname): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
694 """Yield all the keys of a variable value. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
695 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
696 :rtype: A generator |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
697 :raise KeyError: |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
698 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
699 .. note:: Dictionary keys are not subject to interpolation. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
700 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
701 """ |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
702 for k in self.getvar(varname).keys(): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
703 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
704 |
|
368
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
705 def getfirstvar(self, *varnames, **kwds): |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
706 """A variant of :meth:`~.getvar` that returns the first found variable |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
707 in the list of given variables in `varnames`. |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
708 |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
709 """ |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
710 default = kwds.pop("default", _MARKER) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
711 for varname in varnames: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
712 try: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
713 varvalue = self.getvar(varname) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
714 except KeyError: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
715 pass |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
716 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
717 return varvalue |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
718 if default is _MARKER: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
719 raise KeyError( |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
720 "none of the given variables found: %r" % (varnames,)) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
721 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
722 return default |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
723 |
|
610
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
724 def py_getvarl_s(self, *path, **kwds): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
725 """Get a variable - including variables from other namespaces. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
726 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
727 `path` and `namespace` are interpreted as in |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
728 :meth:`.getvarl`. But variables will be interpolated |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
729 recursively within the variable values and filters are |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
730 applied. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
731 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
732 For more details see chapter :ref:`variable-interpolation`. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
733 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
734 """ |
|
321
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
735 default = kwds.pop("default", _MARKER) |
|
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
736 namespace = kwds.pop("namespace", None) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
737 try: |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
738 obj = self.getvarl(*path, namespace=namespace) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
739 return self.substitute_variables_in_obj(obj) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
740 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
741 if default is _MARKER: |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
742 raise |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
743 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
744 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
745 |
|
610
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
746 if _fast_getvarl_s: |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
747 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
748 def fast_getvarl_s(self, *path, **kwds): |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
749 return _fast_getvarl_s(self, path, **kwds) |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
750 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
751 getvarl_s = fast_getvarl_s |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
752 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
753 else: |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
754 |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
755 getvarl_s = py_getvarl_s |
|
764d4185c76a
C-implementations of Configuration.getvarl_s() and Configuration.getvar()
Franz Glasner <fzglas.hg@dom66.de>
parents:
603
diff
changeset
|
756 |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
757 def getfirstvarl_s(self, *paths, **kwds): |
|
388
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
758 """A variant of :meth:`~.getfirstvarl` that does variable |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
759 interpolation. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
760 |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
761 `paths` and `kwds` are interpreted as in :meth:`.getfirstvarl`. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
762 But variables will be interpolated |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
763 recursively within the variable values and filters are |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
764 applied. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
765 |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
766 For more details see chapter :ref:`variable-interpolation`. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
767 |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
768 """ |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
769 default = kwds.pop("default", _MARKER) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
770 for path in paths: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
771 if isinstance(path, (list, tuple)): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
772 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
773 obj = self.getvarl(*path) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
774 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
775 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
776 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
777 return self.substitute_variables_in_obj(obj) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
778 |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
779 elif isinstance(path, dict): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
780 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
781 namespace = path["namespace"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
782 p = path["path"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
783 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
784 raise TypeError("a paths dict item must have a `path'" |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
785 " and a `namespace' key") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
786 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
787 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
788 obj = self.getvarl(*p, namespace=namespace) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
789 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
790 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
791 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
792 return self.substitute_variables_in_obj(obj) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
793 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
794 raise TypeError("a paths item must be a dict, list or tuple") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
795 if default is _MARKER: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
796 raise KeyError( |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
797 "none of the given variables found: %r" % (paths,)) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
798 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
799 return default |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
800 |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
801 def py_getvar_s(self, varname, default=_MARKER): |
|
24
fa65adab0b71
FIX: Typo in comment
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
23
diff
changeset
|
802 """Get a variable - including variables from other namespaces. |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
803 |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
804 `varname` is interpreted as in :meth:`.getvar`. But variables |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
805 will be interpolated recursively within the variable values |
|
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
806 and filters are applied. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
807 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
808 For more details see chapter :ref:`variable-interpolation`. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
809 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
810 """ |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
811 varns, varname = _split_ns(varname) |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
812 try: |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
813 if not varns: |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
814 return self.substitute_variables_in_obj( |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
815 self.getvarl(*pathstr2path(varname))) |
|
524
09b8e28b7a44
Implement Configuration.getvar_s() by using Configuration.getvarl() instead of Configuration.getvar().
Franz Glasner <fzglas.hg@dom66.de>
parents:
523
diff
changeset
|
816 else: |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
817 return self.substitute_variables_in_obj( |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
818 self.getvarl(varname, namespace=varns)) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
819 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
820 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
821 raise |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
822 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
823 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
824 |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
825 if _fast_getvar_s: |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
826 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
827 def fast_getvar_s(self, varname, default=_MARKER): |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
828 return _fast_getvar_s(self, varname, default) |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
829 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
830 getvar_s = fast_getvar_s |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
831 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
832 else: |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
833 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
834 getvar_s = py_getvar_s |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
835 |
|
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
836 def _py_getvar_s_with_cache_info(self, varname): |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
837 """Internal variant of :meth:`~.getvar_s` that returns some information |
|
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
838 whether caching of interpolated values is allowed |
|
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
839 |
| 722 | 840 Caching is currently not allowed when namespaces are used -- |
| 841 as their implementations may not be deeterministic (or | |
| 842 ``immutable`` in the sense of PostgreSQL). | |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
843 |
|
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
844 Currently used by :meth:`~.interpolate_variables`. |
|
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
845 |
|
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
846 """ |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
847 varns, varname = _split_ns(varname) |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
848 if not varns: |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
849 # no namespace -> cacheable |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
850 return ( |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
851 self.substitute_variables_in_obj( |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
852 self.getvarl(*pathstr2path(varname))), |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
853 True |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
854 ) |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
855 else: |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
856 # results from namespaced lookups are currently not cacheable |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
857 return ( |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
858 self.substitute_variables_in_obj( |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
859 self.getvarl(varname, namespace=varns)), |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
860 False |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
861 ) |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
862 |
|
368
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
863 def getfirstvar_s(self, *varnames, **kwds): |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
864 """A variant of :meth:`~.getvar_s` that returns the first found |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
865 variable in the list of given variables in `varnames`. |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
866 |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
867 """ |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
868 default = kwds.pop("default", _MARKER) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
869 for varname in varnames: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
870 try: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
871 obj = self.getvar(varname) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
872 except KeyError: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
873 pass |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
874 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
875 return self.substitute_variables_in_obj(obj) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
876 if default is _MARKER: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
877 raise KeyError( |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
878 "none of the given variables found: %r" % (varnames,)) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
879 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
880 return default |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
881 |
|
489
b49164db1273
Remove unused keyword params for ._lookupvar().
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
488
diff
changeset
|
882 def _lookupvar(self, *path): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
883 """Lookup a variable within a hierarchy. |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
884 |
|
489
b49164db1273
Remove unused keyword params for ._lookupvar().
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
488
diff
changeset
|
885 :raise KeyError: An unexisting `path` raises a `KeyError` |
|
b49164db1273
Remove unused keyword params for ._lookupvar().
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
488
diff
changeset
|
886 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
887 """ |
|
419
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
888 if not path: |
|
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
889 return self |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
890 use_cache = self.__lookup_cache is not None |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
891 if use_cache: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
892 v = self.__lookup_cache.get(path, _MARKER) |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
893 if v is not _MARKER: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
894 if v is _MISSING: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
895 raise KeyError( |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
896 "Configuration variable %r not found" |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
897 " (negative internal cache value)" % (path,)) |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
898 else: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
899 return v |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
900 eiref = self.expand_if_reference |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
901 try: |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
902 v = eiref(super(Configuration, self).__getitem__(path[0])) |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
903 for p in path[1:]: |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
904 v = eiref(v[p]) |
|
104
d9f9a06e0c49
Make a better error message for "TypeError" exceptions when looking up variables.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
905 except TypeError: |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
906 if use_cache: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
907 self.__lookup_cache[path] = _MISSING |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
908 raise KeyError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
909 "Configuration variable %r not found" |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
910 "(missing intermediate keys?)" % (path,)) |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
911 except KeyError: |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
912 if use_cache: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
913 self.__lookup_cache[path] = _MISSING |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
914 raise |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
915 if use_cache: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
916 self.__lookup_cache[path] = v |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
917 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
918 |
|
518
55b8aed70f7d
Remove unneeded "default" keyword argument for Configuration._lookupref() and Configuration.expand_ref_uri()
Franz Glasner <fzglas.hg@dom66.de>
parents:
517
diff
changeset
|
919 def _lookupref(self, key): |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
920 """ |
| 311 | 921 |
| 922 `key` must be a configuration reference URI without any | |
| 923 (namespace) prefixes and suffixes | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
924 |
| 519 | 925 :raise KeyError: If the reference is not found |
| 926 | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
927 """ |
|
518
55b8aed70f7d
Remove unneeded "default" keyword argument for Configuration._lookupref() and Configuration.expand_ref_uri()
Franz Glasner <fzglas.hg@dom66.de>
parents:
517
diff
changeset
|
928 return self.expand_ref_uri(key) |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
929 |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
930 def expand_if_reference(self, v): |
| 311 | 931 """Check whether `v` is a configuration reference and -- if true -- |
| 932 then expand it. | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
933 |
| 310 | 934 `v` must match the pattern ``{{ref:<REFERENCE>}}`` |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
935 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
936 All non-matching texttypes and all non-texttypes are returned |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
937 unchanged. |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
938 |
| 678 | 939 :raise KeyError: If the reference cannot found |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
940 |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
941 """ |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
942 if not isinstance(v, _TEXTTYPE): |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
943 return v |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
944 if v.startswith(_STARTTOK_REF) and v.endswith(_ENDTOK_REF): |
|
488
298510ec8171
Simplify logic by not using many negations
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
487
diff
changeset
|
945 return self.expand_ref_uri( |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
946 v[len(_STARTTOK_REF):-len(_ENDTOK_REF)]) |
|
488
298510ec8171
Simplify logic by not using many negations
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
487
diff
changeset
|
947 else: |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
948 return v |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
949 |
|
518
55b8aed70f7d
Remove unneeded "default" keyword argument for Configuration._lookupref() and Configuration.expand_ref_uri()
Franz Glasner <fzglas.hg@dom66.de>
parents:
517
diff
changeset
|
950 def expand_ref_uri(self, uri): |
| 519 | 951 """ |
| 952 | |
| 953 :raises KeyError: If the reference URI is not found | |
| 954 | |
| 955 """ | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
956 pu = urlsplit(uri) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
957 if pu.scheme or pu.netloc or pu.path or pu.query: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
958 raise ValueError("only fragment-only URIs are supported") |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
959 if not pu.fragment: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
960 return self |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
961 if pu.fragment.startswith(_DOT): |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
962 raise ValueError("relative refs not supported") |
|
518
55b8aed70f7d
Remove unneeded "default" keyword argument for Configuration._lookupref() and Configuration.expand_ref_uri()
Franz Glasner <fzglas.hg@dom66.de>
parents:
517
diff
changeset
|
963 return self.getvar(pu.fragment) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
964 |
|
679
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
965 def try_get_reference_uri(self, v): |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
966 """Check whether `v` is a configuration reference and -- if true -- |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
967 return the configuration path where the reference points to. |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
968 |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
969 If `v` is not a text type or not a reference return `None`. |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
970 |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
971 Does not check whether the referenced configuration object exists. |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
972 |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
973 :rtype: None or str |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
974 |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
975 """ |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
976 if not isinstance(v, _TEXTTYPE): |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
977 return None |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
978 if v.startswith(_STARTTOK_REF) and v.endswith(_ENDTOK_REF): |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
979 uri = v[len(_STARTTOK_REF):-len(_ENDTOK_REF)] |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
980 pu = urlsplit(uri) |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
981 if pu.scheme or pu.netloc or pu.path or pu.query: |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
982 raise ValueError("only fragment-only URIs are supported") |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
983 if not pu.fragment: |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
984 return _EMPTY_STR |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
985 if pu.fragment.startswith(_DOT): |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
986 raise ValueError("relative refs not supported") |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
987 return pu.fragment |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
988 return None |
|
aa39c1856de4
Begin "ref:" support for jails.
Franz Glasner <fzglas.hg@dom66.de>
parents:
678
diff
changeset
|
989 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
990 def substitute_variables_in_obj(self, obj): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
991 """Recursively expand variables in the object tree `obj`.""" |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
992 ty = type(obj) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
993 if issubclass(ty, _TEXTTYPE): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
994 # a string - really replace the value |
|
525
be6ef72c55d5
Change Configuration.expand_variable() to Configuration.interpolate_variables()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
524
diff
changeset
|
995 return self.interpolate_variables(obj) |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
996 elif issubclass(ty, dict): |
|
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
997 newdict = ty() |
|
497
8e516f17cf95
Optimize .substitute_variables_in_obj: use dict.items() to avoid one dict lookup
Franz Glasner <fzglas.hg@dom66.de>
parents:
495
diff
changeset
|
998 for k, v in obj.items(): |
|
8e516f17cf95
Optimize .substitute_variables_in_obj: use dict.items() to avoid one dict lookup
Franz Glasner <fzglas.hg@dom66.de>
parents:
495
diff
changeset
|
999 newdict[k] = self.substitute_variables_in_obj(v) |
|
490
ea4b7fac02d6
Reorder type comparisons by usage
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
489
diff
changeset
|
1000 return newdict |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
1001 elif issubclass(ty, list): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1002 return [self.substitute_variables_in_obj(i) for i in obj] |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
1003 elif issubclass(ty, tuple): |
|
512
bf8a1b1498db
Remove a temporary variable usage
Franz Glasner <fzglas.hg@dom66.de>
parents:
511
diff
changeset
|
1004 return ty([self.substitute_variables_in_obj(i) for i in obj]) |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
1005 elif issubclass(ty, set): |
|
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
1006 newset = ty() |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1007 for i in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1008 newset.add(self.substitute_variables_in_obj(i)) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1009 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1010 return obj |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1011 |
|
554
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1012 def py_interpolate_variables(self, s): |
|
525
be6ef72c55d5
Change Configuration.expand_variable() to Configuration.interpolate_variables()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
524
diff
changeset
|
1013 """Expand all variables in the single string `s`""" |
|
551
4c968c5cfce6
Try to interpolate only if the length of the source string exceeds a minimum length
Franz Glasner <fzglas.hg@dom66.de>
parents:
550
diff
changeset
|
1014 len_s = len(s) |
|
4c968c5cfce6
Try to interpolate only if the length of the source string exceeds a minimum length
Franz Glasner <fzglas.hg@dom66.de>
parents:
550
diff
changeset
|
1015 if len_s < 4: |
|
4c968c5cfce6
Try to interpolate only if the length of the source string exceeds a minimum length
Franz Glasner <fzglas.hg@dom66.de>
parents:
550
diff
changeset
|
1016 return s |
|
637
4499e9b4855d
The ``{{::DEL::}}`` is not subject to interpolation any more.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
610
diff
changeset
|
1017 if s == DEL_VALUE: |
|
4499e9b4855d
The ``{{::DEL::}}`` is not subject to interpolation any more.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
610
diff
changeset
|
1018 return s |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
1019 start = s.find(_STARTTOK, 0) |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1020 if start < 0: |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1021 return s |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1022 use_cache = self.__interpolation_cache is not None |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1023 if use_cache: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1024 res = self.__interpolation_cache.get(s, _MARKER) |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1025 if res is not _MARKER: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1026 if res is _MISSING: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1027 warnings.warn("Cannot interpolate variables in string " |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1028 "%r (cached)" % (s, ), |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1029 UserWarning, |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1030 stacklevel=1) |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1031 raise KeyError("Cannot interpolate variables in string " |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1032 "%r (cached)" % (s, )) |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1033 else: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1034 return res |
|
711
6557cf9ecea5
FIX: When using attribute access for configurations the values are interpolated since long ago: fix the docu and add a test for that behaviour
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
708
diff
changeset
|
1035 |
|
703
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1036 if ((len_s >= 6) |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1037 and (s[2] == _FILTER_SEPARATOR) |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1038 and (start == 0)): |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1039 if s.find(_ENDTOK_FILTER, 3) != (len_s - 3): |
|
723
c17a4e30ebbf
Docs for nested filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
722
diff
changeset
|
1040 raise ValueError("`{{|' nested filter interpolation must end with `|}}'") |
|
705
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
1041 new_s, filters = _split_filters(s[3:-3], -1) |
|
703
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1042 try: |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1043 varvalue = self.py_interpolate_variables(new_s) |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1044 except KeyError: |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1045 if NONE_FILTER in filters: |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1046 varvalue = None |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1047 elif EMPTY_FILTER in filters: |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1048 varvalue = _EMPTY_STR |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1049 else: |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1050 raise |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1051 varvalue = self._apply_filters(filters, varvalue) |
|
193a616e0b3c
Begin implementation of filter-only expansions (recursive with respect to expansion)
Franz Glasner <fzglas.hg@dom66.de>
parents:
702
diff
changeset
|
1052 return varvalue |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1053 res = [] |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1054 res_append = res.append |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1055 rest = 0 |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1056 cacheable = True |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1057 while start != -1: |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1058 res_append(s[rest:start]) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
1059 end = s.find(_ENDTOK, start) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1060 if end < 0: |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1061 rest = start |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1062 break |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
1063 varname, filters = _split_filters( |
|
705
0485a033c95d
FIX: Parsing a filter chain for the new filter-only expansions: parse them backwards and use "," as filter-chain separator here.
Franz Glasner <fzglas.hg@dom66.de>
parents:
703
diff
changeset
|
1064 s[start+2:end], 1) # noqa: E226 |
|
39
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
1065 try: |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
1066 varvalue, cacheable = self._py_getvar_s_with_cache_info(varname) |
|
528
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1067 except KeyError: |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
1068 cacheable = True |
|
528
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1069 if NONE_FILTER in filters: |
|
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1070 varvalue = None |
|
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1071 elif EMPTY_FILTER in filters: |
|
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1072 varvalue = _EMPTY_STR |
|
349
83f76a41cf7c
Implement a special filter named "None" that suppresses "KeyErrors" from interpolation lookups and returns a Python "None" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
334
diff
changeset
|
1073 else: |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1074 if use_cache and cacheable: |
|
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1075 self.__interpolation_cache[s] = _MISSING |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
1076 warnings.warn("Cannot interpolate variable %r in string " |
|
528
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1077 "%r" % (varname, s, ), |
|
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1078 UserWarning, |
|
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1079 stacklevel=1) |
|
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1080 raise |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
1081 if not cacheable: |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1082 cacheable = False |
|
528
54a8d020f5d5
Apply None and Empty interpolation filters only on KeyErrors
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
527
diff
changeset
|
1083 varvalue = self._apply_filters(filters, varvalue) |
|
515
3387a9d5fb12
Performance: compute the length of the constant input strinc only once and compute the new rest earlier and use the computed value also in comparisons
Franz Glasner <fzglas.hg@dom66.de>
parents:
513
diff
changeset
|
1084 rest = end + 2 |
|
251
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
1085 # |
|
350
9d729c479dc2
FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
349
diff
changeset
|
1086 # Dont apply and type conversions to the variable value if |
|
9d729c479dc2
FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
349
diff
changeset
|
1087 # the whole `s` is just one expansion |
|
251
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
1088 # |
|
515
3387a9d5fb12
Performance: compute the length of the constant input strinc only once and compute the new rest earlier and use the computed value also in comparisons
Franz Glasner <fzglas.hg@dom66.de>
parents:
513
diff
changeset
|
1089 if (start == 0) and (rest == len_s): |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1090 if use_cache and cacheable: |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
1091 self.__interpolation_cache[s] = varvalue |
|
251
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
1092 return varvalue |
|
516
ad1e630ba736
FIX: Do not filter-out all False values then interpolating (zero int, boolean False)
Franz Glasner <fzglas.hg@dom66.de>
parents:
515
diff
changeset
|
1093 if varvalue is None: |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1094 pass |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1095 else: |
|
487
d7f6f2afcee2
Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
485
diff
changeset
|
1096 res_append(str_and_u(varvalue)) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1097 # don't re-evaluate because `self.getvar_s()` expands already |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
1098 start = s.find(_STARTTOK, rest) |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
1099 res_append(s[rest:]) |
|
527
08924499db3c
Implement a simple interpolation cache for text types
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
526
diff
changeset
|
1100 res = _EMPTY_STR.join(res) |
|
647
df58983f28a2
Allow to disable the internal caching in configmix.
Franz Glasner <fzglas.hg@dom66.de>
parents:
640
diff
changeset
|
1101 if use_cache and cacheable: |
|
530
28191d61b042
FIX: Handle non-cacheable interpolations properly.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
529
diff
changeset
|
1102 self.__interpolation_cache[s] = res |
|
527
08924499db3c
Implement a simple interpolation cache for text types
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
526
diff
changeset
|
1103 return res |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1104 |
|
554
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1105 if _fast_interpolate_variables: |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1106 |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1107 def fast_interpolate_variables(self, s): |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1108 return _fast_interpolate_variables( |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1109 self, s, self.__interpolation_cache) |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1110 |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1111 interpolate_variables = fast_interpolate_variables |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1112 |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1113 else: |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1114 |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1115 interpolate_variables = py_interpolate_variables |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1116 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1117 def _apply_filters(self, filters, value): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1118 for name in filters: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1119 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1120 filterfn = lookup_filter(name) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1121 except KeyError: |
|
40
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
1122 # |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
1123 # Convert to NameError because we find a missing filters |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
1124 # a very serious error. |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
1125 # |
|
521
1001701be682
Formatting of NameErrors in ._apply_filters(): put "name" into a tuple explicitely
Franz Glasner <fzglas.hg@dom66.de>
parents:
519
diff
changeset
|
1126 raise NameError("Filter %r not found" % (name, )) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1127 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1128 value = filterfn(self, value) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1129 return value |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
1130 |
|
685
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1131 def jailed(self, rootpath=None, root=None, bind_root=True): |
| 399 | 1132 """Return a "jailed" configuration of the current configuration. |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1133 |
|
655
b74f20e19c01
Docstring: be more accurate now in ".jailed()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
648
diff
changeset
|
1134 :param rootpath: a sequence of strings (or objects) that shall |
|
b74f20e19c01
Docstring: be more accurate now in ".jailed()"
Franz Glasner <fzglas.hg@dom66.de>
parents:
648
diff
changeset
|
1135 emcompass the chroot-like jail of the returned |
| 399 | 1136 configuration |
| 1137 :type rootpath: list or tuple | |
| 1138 :param str root: a string path expression that shall encompass | |
| 1139 the chroot-like jail of the returned configuration | |
|
411
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1140 :param bool bind_root: if you do a :meth:`~.rebind` just after |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1141 creation of a jailed config you can set |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1142 `bind_root` to `False`; otherwise use |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1143 the default |
| 397 | 1144 :return: a jailed (aka restricted) configuration |
| 1145 :rtype: _JailedConfiguration | |
| 1146 | |
| 399 | 1147 Exactly one of `rootpath` or `root` must be given. |
| 1148 | |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1149 """ |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1150 if rootpath is not None and root is not None: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1151 raise ValueError("only one of `rootpath' or `root' can be given") |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1152 if rootpath is None and root is None: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1153 raise ValueError("one of `rootpath' or `root' must be given") |
|
416
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
1154 if rootpath is not None and not isinstance(rootpath, (list, tuple)): |
|
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
1155 raise TypeError("`rootpath' must be a list or a tuple") |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1156 if root is not None: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1157 # convert to path |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
1158 varns, varname = _split_ns(root) |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1159 if varns: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1160 raise ValueError( |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1161 "jailed configurations do not support namespaces") |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
1162 rootpath = pathstr2path(root) |
|
685
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1163 # if resolve_ref: |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1164 # while True: |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1165 # target = self.getvarl(*rootpath) |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1166 # target_uri = self.try_get_reference_uri(target) |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1167 # if target_uri is None: |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1168 # break |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1169 # rootpath = pathstr2path(target_uri) |
|
411
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1170 jc = _JailedConfiguration(*rootpath) |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1171 if bind_root: |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1172 jc.rebind(self) |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1173 return jc |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1174 |
|
666
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1175 def iter_jailed(self, rootpath=None, root=None): |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1176 """Iterator that yields properly jailed configurations. |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1177 |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1178 `rootpath` or `root` must refer to a `list` or `dict` container. |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1179 |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1180 """ |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1181 return self.jailed(rootpath=rootpath, root=root).iter_jailed() |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1182 |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1183 |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
1184 class _JailedConfiguration(CoercingMethodsMixin): |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1185 |
| 397 | 1186 """A jailed and restricted variant of :class:`Configuration`. |
| 1187 | |
| 399 | 1188 Restriction is two-fold: |
| 1189 | |
| 1190 - The access to configuration variables in `config` is restricted | |
| 1191 to the configuration sub-tree that is configured in `path`. | |
| 1192 | |
| 1193 - Not all access-methods of :class:`Configuration` are implemented | |
| 1194 yet. | |
| 1195 | |
| 400 | 1196 .. seealso:: :ref:`jailed-configuration` |
| 1197 | |
| 399 | 1198 .. note:: There is no namespace support. |
| 1199 | |
| 1200 .. note:: Do not call the constructor directly. Instantiate a jailed | |
| 1201 configuration from the parent configuration's | |
| 1202 :meth:`~.Configuration.jailed` factory method. | |
| 397 | 1203 |
| 1204 """ | |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1205 |
|
503
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1206 __slots__ = ("_base", "_path", "_path_string") |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1207 |
|
404
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
1208 is_jail = True |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
1209 """Flag to show that this is a jail for another configuration""" |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
1210 |
|
411
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
1211 def __init__(self, *path): |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1212 super(_JailedConfiguration, self).__init__() |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1213 self._path = path |
|
503
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1214 self._path_string = None |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1215 |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1216 @property |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1217 def _pathstr(self): |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1218 v = self._path_string |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1219 if v is None: |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1220 if self._path: |
|
513
599e8ade567c
str.join(): use a list comprehension instead of a generator expression for performance reasons
Franz Glasner <fzglas.hg@dom66.de>
parents:
512
diff
changeset
|
1221 v = _HIER_SEPARATOR.join([quote(p) for p in self._path]) \ |
|
503
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1222 + _HIER_SEPARATOR |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1223 else: |
|
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1224 v = _EMPTY_STR |
|
548
325008573bc6
FIX: Assign to _JailedConfiguration._path_string only if not yet done
Franz Glasner <fzglas.hg@dom66.de>
parents:
543
diff
changeset
|
1225 self._path_string = v |
|
503
a56f1d97a3f3
Use generator comprehensions where possible instead of list comprehensions that are converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
502
diff
changeset
|
1226 return v |
|
405
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1227 |
|
406
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
1228 @property |
|
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
1229 def base(self): |
|
426
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
1230 """Ask for the base (aka parent) configuration". |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
1231 |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
1232 This configuration is always unjailed. |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
1233 |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
1234 """ |
|
406
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
1235 return self._base |
|
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
1236 |
|
405
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1237 def rebind(self, new_base): |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1238 """Bind the jail to a new unjailed configuration `new_base`. |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1239 |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1240 The new configuration base also must have an existing path to |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1241 the root. |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1242 |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1243 :param Configuration new_base: the new base |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1244 |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1245 """ |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1246 if new_base.is_jail: |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1247 raise TypeError("can only bind to an unjailed configuration") |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
1248 self._base = new_base |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1249 # |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1250 # Early error out if the chroot does not exist but allow |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1251 # degenerated case if `self._path` is empty. |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1252 # |
|
449
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
1253 if self._path and self._path not in new_base: |
|
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
1254 raise KeyError( |
|
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
1255 "base key path %r not available in the new base" |
|
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
1256 % (self._path, )) |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1257 |
|
448
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1258 def __getattr__(self, name): |
| 469 | 1259 """Attribute-style access. |
| 1260 | |
| 1261 Result values are interpolated (i.e. forwarded to | |
| 1262 :meth:`~.getvarl_s`) | |
| 1263 | |
| 1264 """ | |
|
448
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1265 try: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1266 v = self._base.getvarl_s(*(self._path + (name, ))) |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1267 except KeyError: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1268 raise AttributeError("%s has no attribute %r" % (type(self), name)) |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1269 else: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1270 # Wrap a dict into another dict with attribute access support |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1271 if isinstance(v, dict): |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1272 return _AttributeDict(v) |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1273 else: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1274 return v |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
1275 |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
1276 def __getitem__(self, key): |
| 469 | 1277 """Mapping and list interface that forwards to :meth:`~.getvarl_s` |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
1278 |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
1279 """ |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
1280 if isinstance(key, tuple): |
|
440
f297c23f78f0
Optimize __getitem__() in jailed configurations: call base directly
Franz Glasner <fzglas.hg@dom66.de>
parents:
439
diff
changeset
|
1281 return self._base.getvarl_s(*(self._path + key)) |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
1282 elif isinstance(key, list): |
|
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
1283 return self._base.getvarl_s(*(self._path + tuple(key))) |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
1284 else: |
|
468
95df1a10259a
Index list-access for jailed configurations: be more generic by calling also __getitem__ in the base
Franz Glasner <fzglas.hg@dom66.de>
parents:
467
diff
changeset
|
1285 return self._base.getvarl_s(*self._path)[key] |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
1286 |
|
442
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1287 def get(self, key, default=None): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1288 if isinstance(key, tuple): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1289 return self._base.get(self._path + key, default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1290 elif isinstance(key, list): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1291 return self._base.get(self._path + tuple(key), default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1292 else: |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1293 return self._base.get(self._path + (key, ), default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
1294 |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
1295 def __contains__(self, key): |
| 469 | 1296 """Containment support for containers""" |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
1297 if isinstance(key, tuple): |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
1298 return (self._path + key) in self._base |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
1299 elif isinstance(key, list): |
|
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
1300 return (self._path + tuple(key)) in self._base |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
1301 else: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
1302 return (self._path + (key, )) in self._base |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
1303 |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1304 def getvarl(self, *path, **kwds): |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1305 return self._base.getvarl(*(self._path + path), **kwds) |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1306 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1307 def getkeysl(self, *path, **kwds): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1308 for k in self._base.getkeysl(*(self._path + path), **kwds): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1309 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1310 |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1311 def getfirstvarl(self, *paths, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1312 real_paths = [] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1313 for path in paths: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1314 if isinstance(path, (list, tuple)): |
|
509
4c2517e18af0
FIX: Convert a list ito a tuple before appending it to another tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
507
diff
changeset
|
1315 real_paths.append(self._path + tuple(path)) |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1316 elif isinstance(path, dict): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1317 raise TypeError( |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1318 "a `dict' is not supported in a jailed configuration") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1319 else: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1320 raise TypeError("a paths item must be a list or tuple") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1321 return self._base.getfirstvarl(*real_paths, **kwds) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1322 |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1323 def getvarl_s(self, *path, **kwds): |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1324 return self._base.getvarl_s(*(self._path + path), **kwds) |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1325 |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1326 def getfirstvarl_s(self, *paths, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1327 real_paths = [] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1328 for path in paths: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1329 if isinstance(path, (list, tuple)): |
|
510
acf1e8696d68
FIX: Some other lists that need to converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
509
diff
changeset
|
1330 real_paths.append(self._path + tuple(path)) |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1331 elif isinstance(path, dict): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1332 raise TypeError( |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1333 "a `dict' is not supported in a jailed configuration") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1334 else: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1335 raise TypeError("a paths item must be a list or tuple") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1336 return self._base.getfirstvarl_s(*real_paths, **kwds) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1337 |
|
428
090a25f36a3d
FIX: Allow jailed configurations to use correctly use base configurations that use a different "default" marker object.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
426
diff
changeset
|
1338 def getvar(self, varname, **kwds): |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
1339 return self._base.getvarl(*(self._path + pathstr2path(varname)), |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
1340 **kwds) |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1341 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1342 def getkeys(self, varname): |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
1343 for k in self._base.getkeysl(*(self._path + pathstr2path(varname))): |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1344 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1345 |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1346 def getfirstvar(self, *varnames, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1347 real_varnames = [self._pathstr + vn for vn in varnames] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1348 return self._base.getfirstvar(*real_varnames, **kwds) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1349 |
|
428
090a25f36a3d
FIX: Allow jailed configurations to use correctly use base configurations that use a different "default" marker object.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
426
diff
changeset
|
1350 def getvar_s(self, varname, **kwds): |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
1351 return self._base.getvarl_s(*(self._path + pathstr2path(varname)), |
|
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
1352 **kwds) |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1353 |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1354 def getfirstvar_s(self, *varnames, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1355 real_varnames = [self._pathstr + vn for vn in varnames] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1356 return self._base.getfirstvar_s(*real_varnames, **kwds) |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1357 |
|
459
9dc9cef1b9cd
Implement proper iteration support for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
455
diff
changeset
|
1358 def __iter__(self): |
| 469 | 1359 """Iteration support for containers""" |
|
459
9dc9cef1b9cd
Implement proper iteration support for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
455
diff
changeset
|
1360 return iter(self._base.getvarl_s(*self._path)) |
|
9dc9cef1b9cd
Implement proper iteration support for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
455
diff
changeset
|
1361 |
|
467
9fcdc42a0457
len-support for jailed configurations: implement a proper __len__() method
Franz Glasner <fzglas.hg@dom66.de>
parents:
460
diff
changeset
|
1362 def __len__(self): |
| 469 | 1363 """Length support for containers""" |
|
648
e8f3e970e411
__len__() for jails also now internally expands: this is for consistency with __bool__ when applyint to non-container jails (which are possible)
Franz Glasner <fzglas.hg@dom66.de>
parents:
647
diff
changeset
|
1364 return len(self._base.getvarl_s(*self._path)) |
|
467
9fcdc42a0457
len-support for jailed configurations: implement a proper __len__() method
Franz Glasner <fzglas.hg@dom66.de>
parents:
460
diff
changeset
|
1365 |
|
666
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1366 def iter_jailed(self): |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1367 """Iteration support for containers which yields properly jailed |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1368 sub-jails. |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1369 |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1370 Only supported for type `list` or type `dict` jails. |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1371 |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1372 """ |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1373 container = self._base.getvarl(*self._path) |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1374 if isinstance(container, dict): |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1375 for k in container: |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1376 yield self.jailed(rootpath=(k, )) |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1377 elif isinstance(container, list): |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1378 len_container = len(container) |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1379 idx = 0 |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1380 while idx < len_container: |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1381 yield self.jailed(rootpath=(idx, )) |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1382 idx += 1 |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1383 else: |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1384 raise TypeError( |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1385 "jailed iterators only supported for lists and dicts") |
|
0eff8441c4b9
Implement iteration support that yields properly jailed configurations for each container item
Franz Glasner <fzglas.hg@dom66.de>
parents:
656
diff
changeset
|
1386 |
|
460
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1387 if PY2: |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1388 |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1389 def __nonzero__(self): |
| 469 | 1390 """Map- and list-style evaluation in boolean context""" |
|
460
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1391 return bool(self._base.getvarl_s(*self._path)) |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1392 |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1393 else: |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1394 |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1395 def __bool__(self): |
| 469 | 1396 """Map- and list-style evaluation in boolean context""" |
|
460
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1397 return bool(self._base.getvarl_s(*self._path)) |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1398 |
|
685
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1399 def jailed(self, rootpath=None, root=None, bind_root=True): |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1400 """Return a "jailed" configuration that effectively is a |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1401 subjail of the current jail |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1402 |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1403 For a more complete description see :meth:`.Configuration.jailed`. |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1404 |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1405 """ |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1406 if rootpath is not None and root is not None: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1407 raise ValueError("only one of `rootpath' or `root' can be given") |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1408 if rootpath is None and root is None: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1409 raise ValueError("one of `rootpath' or `root' must be given") |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1410 if rootpath is not None and not isinstance(rootpath, (list, tuple)): |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1411 raise TypeError("`rootpath' must be a list or a tuple") |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1412 if root is not None: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1413 # convert to path |
|
540
33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
Franz Glasner <fzglas.hg@dom66.de>
parents:
539
diff
changeset
|
1414 varns, varname = _split_ns(root) |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1415 if varns: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1416 raise ValueError( |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1417 "sub-jails do not support namespaces") |
|
539
9546d38cd3f8
Refactor: the parsing of the quoted and dot-separated path string is put into a function that handles also empty inputs properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
531
diff
changeset
|
1418 rootpath = pathstr2path(varname) |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1419 if self._path: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1420 new_rootpath = self._path + tuple(rootpath) |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1421 else: |
|
510
acf1e8696d68
FIX: Some other lists that need to converted to tuples
Franz Glasner <fzglas.hg@dom66.de>
parents:
509
diff
changeset
|
1422 new_rootpath = tuple(rootpath) |
|
685
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1423 # if resolve_ref: |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1424 # while True: |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1425 # target = self._base.getvarl(*new_rootpath) |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1426 # target_uri = self._base.try_get_reference_uri(target) |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1427 # if target_uri is None: |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1428 # break |
|
2e7920b0b4d9
Because Configuration.getvarl() expands references internally already extra handling of references in .jailed() is not needed
Franz Glasner <fzglas.hg@dom66.de>
parents:
684
diff
changeset
|
1429 # new_rootpath = pathstr2path(target_uri) |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1430 sjc = _JailedConfiguration(*new_rootpath) |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1431 if bind_root: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1432 sjc.rebind(self._base) |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1433 return sjc |
|
432
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1434 |
|
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1435 def __repr__(self): |
|
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1436 r = "_JailedConfiguration(rootpath=%s)" % n(repr(self._path)) |
|
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1437 return r |
|
554
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1438 |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1439 |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1440 if _sync_MISSING: |
|
36d7aa000435
Implement a C-version of Configuration.interpolate_variables
Franz Glasner <fzglas.hg@dom66.de>
parents:
553
diff
changeset
|
1441 _sync_MISSING(_MISSING) |
|
713
1832c5d1bd00
Handle _sync_MARKER exactly as _sync_MISSING.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
712
diff
changeset
|
1442 if _sync_MARKER: |
|
603
e55a42144ba9
C-implementations for Configuration.getvarl() and Configuration.getvar_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
593
diff
changeset
|
1443 _sync_MARKER(_MARKER) |
