annotate configmix/__init__.py @ 787:615b7e1b80d2

Suppress section numbering for "Breaking Changes"
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 17 May 2026 13:11:40 +0200
parents 49d43c125781
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
53ea2bc254e7 Begin a package to abstract some of the important configuration handling stuff.
Franz Glasner <hg@dom66.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
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
2 """A library for helping with configuration files.
4
f76d85ccc5b9 Switch to the "New BSD License"
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 0
diff changeset
3
73
6216c561532a Put a Copyright and License notice into the package's __init__.py
Franz Glasner <hg@dom66.de>
parents: 69
diff changeset
4 :Author: Franz Glasner
673
77d4a3e7c9e2 Extent all important copyright years to 2023
Franz Glasner <fzglas.hg@dom66.de>
parents: 669
diff changeset
5 :Copyright: (c) 2015–2023, Franz Glasner.
73
6216c561532a Put a Copyright and License notice into the package's __init__.py
Franz Glasner <hg@dom66.de>
parents: 69
diff changeset
6 All rights reserved.
296
eed16a1ec8f3 Use SPDX license identifiers (either full or short) all over the package
Franz Glasner <fzglas.hg@dom66.de>
parents: 293
diff changeset
7 :License: BSD 3-Clause "New" or "Revised" License.
73
6216c561532a Put a Copyright and License notice into the package's __init__.py
Franz Glasner <hg@dom66.de>
parents: 69
diff changeset
8 See LICENSE.txt for details.
174
e2505f524ab9 Use the "@(#)" sigil in the package documentation header
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 173
diff changeset
9 :ID: @(#) $Header$
4
f76d85ccc5b9 Switch to the "New BSD License"
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 0
diff changeset
10
f76d85ccc5b9 Switch to the "New BSD License"
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 0
diff changeset
11 """
0
53ea2bc254e7 Begin a package to abstract some of the important configuration handling stuff.
Franz Glasner <hg@dom66.de>
parents:
diff changeset
12
53ea2bc254e7 Begin a package to abstract some of the important configuration handling stuff.
Franz Glasner <hg@dom66.de>
parents:
diff changeset
13 from __future__ import division, print_function, absolute_import
53ea2bc254e7 Begin a package to abstract some of the important configuration handling stuff.
Franz Glasner <hg@dom66.de>
parents:
diff changeset
14
53ea2bc254e7 Begin a package to abstract some of the important configuration handling stuff.
Franz Glasner <hg@dom66.de>
parents:
diff changeset
15
763
f7d888526349 +++++ v0.23.3
Franz Glasner <fzglas.hg@dom66.de>
parents: 757
diff changeset
16 __version__ = "0.23.3"
779
49d43c125781 Put the logical release date directly into configmix.__init__.
Franz Glasner <fzglas.hg@dom66.de>
parents: 763
diff changeset
17 __date__ = "2023-12-07"
163
ff03cdf36139 The README.txt should be in the most important parts readable without keyword substitutions.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
18 __revision__ = "|VCSRevision|"
52
6c7f90dbce98 Adjusted the Copyright and change the RCS keywords in accordance with "kwarchive"
Franz Glasner <hg@dom66.de>
parents: 45
diff changeset
19
177
6dde1e344ae8 Style: put "__all__" into the meta-variables section as recommended by PEP
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 175
diff changeset
20 __all__ = ["load", "safe_load",
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
21 "set_assoc", "get_assoc", "clear_assoc",
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
22 "get_default_assoc",
180
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
23 "Configuration",
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
24 "try_determine_filemode"]
177
6dde1e344ae8 Style: put "__all__" into the meta-variables section as recommended by PEP
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 175
diff changeset
25
0
53ea2bc254e7 Begin a package to abstract some of the important configuration handling stuff.
Franz Glasner <hg@dom66.de>
parents:
diff changeset
26
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
27 import fnmatch
10
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
28 import copy
180
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
29 import io
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
30 import os
180
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
31 import re
10
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
32
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
33 from .compat import u2fs
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: 535
diff changeset
34 from .config import Configuration, quote, unquote, pathstr2path # noqa: F401
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
35 from . import constants
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
36
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
37
172
8138d56d7cd3 ".load" and ".safe_load" get a keyword parameter "defaults" that allows the provision of a configuration dictionary with default settings
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 171
diff changeset
38 def load(*files, **kwargs):
22
6a91db2c2469 A convenience function to load and merge a list of configuration files with different styles
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 13
diff changeset
39 """Load the given configuration files, merge them in the given order
108
2196362c0467 Enhance documentation of "configmix.load()"
Franz Glasner <hg@dom66.de>
parents: 107
diff changeset
40 and return the resulting configuration dictionary.
2196362c0467 Enhance documentation of "configmix.load()"
Franz Glasner <hg@dom66.de>
parents: 107
diff changeset
41
269
Franz Glasner <fzglas.hg@dom66.de>
parents: 268
diff changeset
42 :param files: the filenames of the configuration files to read and merge;
274
90bbade12d8e Docu: use local lookup firstly
Franz Glasner <fzglas.hg@dom66.de>
parents: 273
diff changeset
43 if a filename starts with ``<dir>`` then the name is
269
Franz Glasner <fzglas.hg@dom66.de>
parents: 268
diff changeset
44 interpreted as directory and all files are loaded in
Franz Glasner <fzglas.hg@dom66.de>
parents: 268
diff changeset
45 sorted order (non-resursively, ignoring unknown filetypes)
173
b3ba2b0265b5 Docu: Explicitely tag "defaults" as keyword argument
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 172
diff changeset
46 :keyword defaults: optional configuration dictionary with some default
b3ba2b0265b5 Docu: Explicitely tag "defaults" as keyword argument
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 172
diff changeset
47 settings where the settings from `files` are merged
b3ba2b0265b5 Docu: Explicitely tag "defaults" as keyword argument
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 172
diff changeset
48 into
214
a35b0ca8b81f FIX: Docu: Sphinx markup
Franz Glasner <fzglas.hg@dom66.de>
parents: 211
diff changeset
49 :type defaults: dict-alike or None
221
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
50 :keyword extras: optional configuration dictionary that will applied
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
51 last
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
52
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
53 Use this for example to overwrite configuration file
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
54 settings from commandline arguments.
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
55 :type extras: dict-alike or None
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
56 :keyword strict: enable strict parsing mode for parsers that support it
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
57 (e.g. to prevent duplicate keys)
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
58 :type strict: bool
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
59 :keyword merge_lists: When ``None`` then lists will be overwritten
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
60 by the merge process. When ``extend`` then
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
61 lists will be extended instead.
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
62 This parameter is passed to :func:`.merge`.
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
63 :type merge_lists: str or None
108
2196362c0467 Enhance documentation of "configmix.load()"
Franz Glasner <hg@dom66.de>
parents: 107
diff changeset
64 :returns: the configuration
2196362c0467 Enhance documentation of "configmix.load()"
Franz Glasner <hg@dom66.de>
parents: 107
diff changeset
65 :rtype: ~configmix.config.Configuration
22
6a91db2c2469 A convenience function to load and merge a list of configuration files with different styles
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 13
diff changeset
66
6a91db2c2469 A convenience function to load and merge a list of configuration files with different styles
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 13
diff changeset
67 """
172
8138d56d7cd3 ".load" and ".safe_load" get a keyword parameter "defaults" that allows the provision of a configuration dictionary with default settings
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 171
diff changeset
68 defaults = kwargs.get("defaults")
221
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
69 extras = kwargs.get("extras")
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
70 strict = kwargs.get("strict", False)
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
71 merge_lists = kwargs.get("merge_lists", None)
217
b869e792310e FIX: Extra merged and unsafe merges in configmix.safe_load
Franz Glasner <fzglas.hg@dom66.de>
parents: 216
diff changeset
72 if defaults is None:
220
2034da70f8fd Simplify the implementation of configmix.load() and .safe_load():
Franz Glasner <fzglas.hg@dom66.de>
parents: 217
diff changeset
73 ex = Configuration()
22
6a91db2c2469 A convenience function to load and merge a list of configuration files with different styles
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 13
diff changeset
74 else:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
75 ex = merge(None, Configuration(defaults), merge_lists=merge_lists)
220
2034da70f8fd Simplify the implementation of configmix.load() and .safe_load():
Franz Glasner <fzglas.hg@dom66.de>
parents: 217
diff changeset
76 for f in files:
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
77 if f.startswith(constants.DIR_PREFIX):
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
78 for f2 in _get_configuration_files_from_dir(f[5:]):
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
79 nx = _load_cfg_from_file(f2, ignore_unknown=True, strict=strict)
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
80 if nx is not None:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
81 ex = merge(nx, ex, merge_lists=merge_lists)
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
82 else:
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
83 nx = _load_cfg_from_file(f, strict=strict)
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
84 if nx is not None:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
85 ex = merge(nx, ex, merge_lists=merge_lists)
221
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
86 if extras:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
87 ex = merge(Configuration(extras), ex, merge_lists=merge_lists)
217
b869e792310e FIX: Extra merged and unsafe merges in configmix.safe_load
Franz Glasner <fzglas.hg@dom66.de>
parents: 216
diff changeset
88 return Configuration(ex)
22
6a91db2c2469 A convenience function to load and merge a list of configuration files with different styles
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 13
diff changeset
89
6a91db2c2469 A convenience function to load and merge a list of configuration files with different styles
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 13
diff changeset
90
172
8138d56d7cd3 ".load" and ".safe_load" get a keyword parameter "defaults" that allows the provision of a configuration dictionary with default settings
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 171
diff changeset
91 def safe_load(*files, **kwargs):
112
c50ad93eb5dc Implemented a "safe_load()" to load with safe merging
Franz Glasner <hg@dom66.de>
parents: 111
diff changeset
92 """Analogous to :func:`load` but do merging with :func:`safe_merge`
275
e2fd8fea1a4c Docu: more local lookup
Franz Glasner <fzglas.hg@dom66.de>
parents: 274
diff changeset
93 instead of :func:`.merge`
112
c50ad93eb5dc Implemented a "safe_load()" to load with safe merging
Franz Glasner <hg@dom66.de>
parents: 111
diff changeset
94
c50ad93eb5dc Implemented a "safe_load()" to load with safe merging
Franz Glasner <hg@dom66.de>
parents: 111
diff changeset
95 """
172
8138d56d7cd3 ".load" and ".safe_load" get a keyword parameter "defaults" that allows the provision of a configuration dictionary with default settings
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 171
diff changeset
96 defaults = kwargs.get("defaults")
221
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
97 extras = kwargs.get("extras")
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
98 strict = kwargs.get("strict", False)
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
99 merge_lists = kwargs.get("merge_lists", None)
217
b869e792310e FIX: Extra merged and unsafe merges in configmix.safe_load
Franz Glasner <fzglas.hg@dom66.de>
parents: 216
diff changeset
100 if defaults is None:
220
2034da70f8fd Simplify the implementation of configmix.load() and .safe_load():
Franz Glasner <fzglas.hg@dom66.de>
parents: 217
diff changeset
101 ex = Configuration()
112
c50ad93eb5dc Implemented a "safe_load()" to load with safe merging
Franz Glasner <hg@dom66.de>
parents: 111
diff changeset
102 else:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
103 ex = safe_merge(None, Configuration(defaults), merge_lists=merge_lists)
220
2034da70f8fd Simplify the implementation of configmix.load() and .safe_load():
Franz Glasner <fzglas.hg@dom66.de>
parents: 217
diff changeset
104 for f in files:
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
105 if f.startswith(constants.DIR_PREFIX):
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
106 for f2 in _get_configuration_files_from_dir(f[5:]):
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
107 nx = _load_cfg_from_file(f2, ignore_unknown=True, strict=strict)
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
108 if nx is not None:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
109 ex = safe_merge(nx, ex, merge_lists=merge_lists)
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
110 else:
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
111 nx = _load_cfg_from_file(f, strict=strict)
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
112 if nx is not None:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
113 ex = safe_merge(nx, ex, merge_lists=merge_lists)
221
6f0f39a9a46f configmix.load() and .safe_load() got a new keyword argument "extras" to be merged in as last configuration dictionary
Franz Glasner <fzglas.hg@dom66.de>
parents: 220
diff changeset
114 if extras:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
115 ex = safe_merge(Configuration(extras), ex, merge_lists=merge_lists)
217
b869e792310e FIX: Extra merged and unsafe merges in configmix.safe_load
Franz Glasner <fzglas.hg@dom66.de>
parents: 216
diff changeset
116 return Configuration(ex)
112
c50ad93eb5dc Implemented a "safe_load()" to load with safe merging
Franz Glasner <hg@dom66.de>
parents: 111
diff changeset
117
c50ad93eb5dc Implemented a "safe_load()" to load with safe merging
Franz Glasner <hg@dom66.de>
parents: 111
diff changeset
118
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
119 def _get_configuration_files_from_dir(root):
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
120 """Returns the sorted list of files within directory `root`
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
121
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
122 """
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
123 files = []
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
124
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
125 if not os.path.isdir(root):
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
126 return files
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
127
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
128 dirfiles = os.listdir(root)
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
129 dirfiles.sort()
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
130 for f in dirfiles:
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
131 path = os.path.join(root, f)
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
132 if os.path.isdir(path):
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
133 # XXX TBD Recurse??? depth-first???
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
134 continue
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
135 files.append(path)
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
136 return files
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
137
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
138
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
139 def _load_yaml(filename, strict=False):
138
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
140 from . import yaml
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 165
diff changeset
141 with open(u2fs(filename), "rb") as yf:
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
142 return yaml.safe_load(yf, strict=strict)
138
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
143
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
144
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
145 def _load_json(filename, strict=False):
138
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
146 from . import json
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
147 return json.load(filename)
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
148
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
149
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
150 def _load_py(filename, strict=False):
138
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
151 from . import py
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
152 return py.load(filename)
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
153
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
154
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
155 def _load_ini(filename, strict=False):
138
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
156 from . import ini
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
157 return ini.load(filename)
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
158
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
159
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
160 def _load_toml(filename, strict=False):
195
28e6c1413947 Added support for TOML style configuration files
Franz Glasner <fzglas.hg@dom66.de>
parents: 191
diff changeset
161 from . import toml
28e6c1413947 Added support for TOML style configuration files
Franz Glasner <fzglas.hg@dom66.de>
parents: 191
diff changeset
162 return toml.load(filename)
28e6c1413947 Added support for TOML style configuration files
Franz Glasner <fzglas.hg@dom66.de>
parents: 191
diff changeset
163
28e6c1413947 Added support for TOML style configuration files
Franz Glasner <fzglas.hg@dom66.de>
parents: 191
diff changeset
164
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
165 def _load_ignore(filename, strict=False):
227
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
166 """A loader that returns `None` just to ignore `filename`"""
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
167 return None
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
168
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
169
180
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
170 EMACS_MODELINE = re.compile(r"-\*-(.*?)-\*-")
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
171 EMACS_MODE = re.compile(r"(?:\A\s*|;\s*)mode[:=]\s*([-_.a-zA-Z0-9]+)")
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
172
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
173
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
174 def try_determine_filemode(filename):
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
175 """Try to determine an explicitely given filemode from an Emacs-compatible
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
176 mode declaration (e.g. ``mode=python``).
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
177
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
178 :param str filename:
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
179 :return: the found mode string or `None`
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
180 :rtype: str or None
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
181
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
182 Only the first two lines are searched for.
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
183
206
5064e3a2e54a Doc: "configmix.try_determine_filemode()" got additional documentation
Franz Glasner <fzglas.hg@dom66.de>
parents: 197
diff changeset
184 Conveniently to be used in calls to :func:`~.set_assoc` to determine
5064e3a2e54a Doc: "configmix.try_determine_filemode()" got additional documentation
Franz Glasner <fzglas.hg@dom66.de>
parents: 197
diff changeset
185 the file-mode by content instead of filename extension.
5064e3a2e54a Doc: "configmix.try_determine_filemode()" got additional documentation
Franz Glasner <fzglas.hg@dom66.de>
parents: 197
diff changeset
186
180
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
187 """
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
188 with io.open(filename, encoding="ascii", errors="replace") as f:
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
189 idx = 0
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
190 for l in f:
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
191 idx += 1
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
192 mo = EMACS_MODELINE.search(l)
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
193 if mo:
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
194 mo = EMACS_MODE.search(mo.group(1))
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
195 if mo:
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
196 return mo.group(1)
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
197 if idx >= 2:
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
198 break
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
199 return None
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
200
e87fa5bd68e7 Implemented "try_determine_filemode()" to determine a file-mode from an Emacs-compatible declaration
Franz Glasner <fzglas.hg@dom66.de>
parents: 179
diff changeset
201
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
202 DEFAULT_MODE_LOADERS = {
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
203 "python": _load_py,
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
204 "yaml": _load_yaml,
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
205 "conf": _load_ini,
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
206 "conf-windows": _load_ini,
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
207 "ini": _load_ini,
195
28e6c1413947 Added support for TOML style configuration files
Franz Glasner <fzglas.hg@dom66.de>
parents: 191
diff changeset
208 "toml": _load_toml,
197
7865f28038a4 Add the "conf-toml" mode mapping to the TOML loader
Franz Glasner <fzglas.hg@dom66.de>
parents: 195
diff changeset
209 "conf-toml": _load_toml,
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
210 "javascript": _load_json,
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
211 "json": _load_json,
228
b2c75efad9e4 Renamed the 'ignore' loader key to '-*-ignore-*-'
Franz Glasner <fzglas.hg@dom66.de>
parents: 227
diff changeset
212 "-*-ignore-*-": _load_ignore,
268
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
213 "-*- ignore -*-": _load_ignore,
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
214 }
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
215 """Default associations between file modes and loader functions"""
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
216
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
217
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
218 DEFAULT_ASSOC = [
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
219 ("*.yml", "yaml"),
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
220 ("*.yaml", "yaml"),
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
221 ("*.json", "json"),
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
222 ("*.py", "python"),
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
223 ("*.ini", "conf"),
195
28e6c1413947 Added support for TOML style configuration files
Franz Glasner <fzglas.hg@dom66.de>
parents: 191
diff changeset
224 ("*.toml", "toml"),
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
225 ]
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
226 """The builtin default associations of filename extensions with
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
227 file modes -- in that order.
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
228
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
229 The "mode" part may be a string or a callable with a filename
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
230 parameter that returns the mode string for the file or `None` if it
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
231 can not determined.
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
232
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
233 """
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
234
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
235
188
17b938ccecb8 Rename the DEFAULT_LOADER marker to USE_DEFAULT_ASSOC
Franz Glasner <fzglas.hg@dom66.de>
parents: 187
diff changeset
236 USE_DEFAULT_ASSOC = object()
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
237 """Marker for the default association for an extension.
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
238
274
90bbade12d8e Docu: use local lookup firstly
Franz Glasner <fzglas.hg@dom66.de>
parents: 273
diff changeset
239 To be used in :func:`.set_assoc`.
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
240 """
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
241
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
242
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
243 def get_default_assoc(pattern):
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
244 """Return the default file-mode association for the :mod:`fnmatch`
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
245 pattern `pattern`.
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
246
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
247 :raises: :class:`KeyError` if the `pattern` is not found.
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
248
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
249 """
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
250 for pat, fmode in DEFAULT_ASSOC:
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
251 if pattern == pat:
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
252 return fmode
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
253 else:
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
254 raise KeyError("No loader for pattern %r" % pattern)
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
255
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
256
189
0d0980ed74cc Rename the "_mode_loaders" variable to "mode_loaders" and make is therefore a "public" item
Franz Glasner <fzglas.hg@dom66.de>
parents: 188
diff changeset
257 mode_loaders = {}
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
258 """All configured associations between file modes and loader functions.
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
259
274
90bbade12d8e Docu: use local lookup firstly
Franz Glasner <fzglas.hg@dom66.de>
parents: 273
diff changeset
260 See :data:`.DEFAULT_MODE_LOADERS`.
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
261
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
262 """
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
263
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
264 _extensions = []
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
265 """All configured assiciations of filename extensions with file modes.
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
266
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
267 See :data:`DEFAULT_ASSOC`
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
268
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
269 """
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
270
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
271
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
272 def clear_assoc():
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
273 """Remove all configured loader associations.
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
274
187
d2eb83720ad8 FIX: Docu: names of predefined constants
Franz Glasner <fzglas.hg@dom66.de>
parents: 186
diff changeset
275 The :data:`.DEFAULT_ASSOC` are **not** changed.
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
276
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
277 """
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
278 del _extensions[:]
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
279
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
280
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
281 def get_assoc(pattern):
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
282 """Return the default loader for the :mod:`fnmatch` pattern `pattern`.
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
283
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
284 :raises: :class:`KeyError` if the `pattern` is not found.
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
285
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
286 """
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
287 for pat, fmode in _extensions:
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
288 if pattern == pat:
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
289 return fmode
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
290 else:
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
291 raise KeyError("No associated file-mode for pattern %r" % pattern)
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
292
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
293
186
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
294 def set_assoc(fnpattern, mode, append=False):
179
62db35db8939 Docu: formatting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 178
diff changeset
295 """Associate a :mod:`fnmatch` style pattern `fnpattern` with a
62db35db8939 Docu: formatting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 178
diff changeset
296 file-mode `mode` that determines what will be called when
62db35db8939 Docu: formatting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 178
diff changeset
297 :func:`load` encounters a file argument that matches `fnpattern`.
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
298
179
62db35db8939 Docu: formatting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 178
diff changeset
299 :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader
62db35db8939 Docu: formatting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 178
diff changeset
300 with
186
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
301 :param mode: a mode string or a callable that accepts a `filename`
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
302 argument and returns a file-mode for the given file
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
303 (or `None`)
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
304 :type mode: str or callable
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
305
186
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
306 :keyword bool append: If `False` (which is the default) then this
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
307 function inserts the given pattern at the head position of the
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
308 currently defined associations, if `True` the pattern will be appended
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
309
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
310 The OS specific case-sensitivity behaviour of
175
327032bb0f6b Docu: wording
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 174
diff changeset
311 :func:`fnmatch.fnmatch` applies (i.e. :func:`os.path.normpath`
327032bb0f6b Docu: wording
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 174
diff changeset
312 will be called for both arguments).
141
647782859ae1 An extra hint that filename extension comparisons for loader lookup are case-insensitive
Franz Glasner <hg@dom66.de>
parents: 139
diff changeset
313
188
17b938ccecb8 Rename the DEFAULT_LOADER marker to USE_DEFAULT_ASSOC
Franz Glasner <fzglas.hg@dom66.de>
parents: 187
diff changeset
314 If `loader` is :data:`.USE_DEFAULT_ASSOC` then the default association
187
d2eb83720ad8 FIX: Docu: names of predefined constants
Franz Glasner <fzglas.hg@dom66.de>
parents: 186
diff changeset
315 from :data:`.DEFAULT_ASSOC` will be used -- if any.
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
316
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
317 """
188
17b938ccecb8 Rename the DEFAULT_LOADER marker to USE_DEFAULT_ASSOC
Franz Glasner <fzglas.hg@dom66.de>
parents: 187
diff changeset
318 if mode is USE_DEFAULT_ASSOC:
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
319 for p, m in DEFAULT_ASSOC:
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
320 if p == fnpattern:
186
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
321 if append:
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
322 _extensions.append((fnpattern, m))
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
323 else:
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
324 _extensions.insert(0, (fnpattern, m))
171
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
325 break
1ff11462a5c1 The associations from filename extensions to parsers are "fnmatch" style patterns now.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 170
diff changeset
326 else:
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
327 raise ValueError("no DEFAULT mode for pattern: %r" % fnpattern)
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
328 else:
186
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
329 if append:
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
330 _extensions.append((fnpattern, mode))
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
331 else:
fa101fb0cd7a Implement an "append" keyword to "configmix.set_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 181
diff changeset
332 _extensions.insert(0, (fnpattern, mode))
138
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
333
b883f4ef1967 Indirectly map extensions to configuration file styles
Franz Glasner <hg@dom66.de>
parents: 122
diff changeset
334
268
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
335 def del_assoc(fnpattern):
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
336 """Remove all associations for `fnpattern`.
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
337
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
338 :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
339 with
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
340
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
341 """
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
342 while True:
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
343 for i in range(len(_extensions)):
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
344 pat, fmode = _extensions[i]
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
345 if fnpattern == pat:
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
346 del _extensions[i]
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
347 break # restart
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
348 else:
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
349 return # nothing deleted -> done
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
350
1484f6c0223a Implemented "del_assoc()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 267
diff changeset
351
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
352 def _load_cfg_from_file(filename, ignore_unknown=False, strict=False):
227
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
353 """Determine the loader for file `filename` and return the loaded
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
354 configuration dict.
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
355
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
356 If `ignore_unknown` is `True` then unknown extensions are ignored.
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
357 Otherwise a :exc:`ValueError` exception is raised.
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
358
227
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
359 Can return `None` is the file should be ignored by the caller.
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
360
f5011eec3b6e Added a loader with key "ignore" that ignores the given configuration file
Franz Glasner <fzglas.hg@dom66.de>
parents: 226
diff changeset
361 """
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
362 for p, m in _extensions:
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
363 if fnmatch.fnmatch(filename, p):
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
364 if callable(m):
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
365 m = m(filename)
265
39c379fa8c65 FIX: Check the return value when calling only when the callable has been really called.
Franz Glasner <fzglas.hg@dom66.de>
parents: 262
diff changeset
366 if m is None:
39c379fa8c65 FIX: Check the return value when calling only when the callable has been really called.
Franz Glasner <fzglas.hg@dom66.de>
parents: 262
diff changeset
367 continue
293
6f0bf673d4ff Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 290
diff changeset
368 return mode_loaders[m](filename, strict=strict)
22
6a91db2c2469 A convenience function to load and merge a list of configuration files with different styles
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 13
diff changeset
369 else:
266
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
370 if ignore_unknown:
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
371 return None
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
372 else:
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
373 raise ValueError("Unknown configuration file type for filename "
46571485b7d4 Allow loading configuration files from directories when using the "<dir>" prefix in filenames.
Franz Glasner <fzglas.hg@dom66.de>
parents: 265
diff changeset
374 "%r" % filename)
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
375
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
376
11
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
377 if 0:
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
378 #
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
379 # From: https://github.com/jet9/python-yconfig/blob/master/yconfig.py
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
380 # License: BSD License
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
381 #
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
382 def dict_merge(a, b):
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
383 """Recursively merges dict's. not just simple a['key'] = b['key'], if
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
384 both a and bhave a key who's value is a dict then dict_merge is called
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
385 on both values and the result stored in the returned dictionary."""
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
386
11
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
387 if not isinstance(b, dict):
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
388 return b
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 206
diff changeset
389 result = deepcopy(a) # noqa
11
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
390 for k, v in b.iteritems():
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
391 if k in result and isinstance(result[k], dict):
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
392 result[k] = dict_merge(result[k], v)
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
393 else:
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 206
diff changeset
394 result[k] = deepcopy(v) # noqa
11
aecb36d4025f Deactivate the "dict_merge()" function from yconfig
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 10
diff changeset
395 return result
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
396
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
397
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
398 def merge(user, default, filter_comments=True, merge_lists=None):
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
399 """Logically merge the configuration in `user` into `default`.
98
d6ba53ce2091 Better documentation of the core function in "configmix"
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 96
diff changeset
400
d6ba53ce2091 Better documentation of the core function in "configmix"
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 96
diff changeset
401 :param ~configmix.config.Configuration user:
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
402 the new configuration that will be logically merged
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
403 into `default`
98
d6ba53ce2091 Better documentation of the core function in "configmix"
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 96
diff changeset
404 :param ~configmix.config.Configuration default:
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
405 the base configuration where `user` is logically merged into
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
406 :param bool filter_comments: flag whether to filter comment keys that
274
90bbade12d8e Docu: use local lookup firstly
Franz Glasner <fzglas.hg@dom66.de>
parents: 273
diff changeset
407 start with any of the items in :data:`.COMMENTS`
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
408 :param merge_lists: When ``None`` then lists will be overwritten
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
409 by the merge process. When ``extend`` then
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
410 lists will be extended instead.
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
411 :type merge_lists: str or None
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
412 :returns: `user` with the necessary amendments from `default`.
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
413 If `user` is ``None`` then `default` is returned.
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
414
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
415 .. note:: The configuration in `user` is augmented/changed
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
416 **inplace**.
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
417
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
418 The configuration in `default` will be changed **inplace**
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
419 when filtering out comments (which is the default).
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
420
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
421 If a value in `user` is equal to :data:`.constants.DEL_VALUE`
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
422 (``{{::DEL::}}``) the corresponding key will be deleted from the
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
423 merged output.
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
424
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
425 From http://stackoverflow.com/questions/823196/yaml-merge-in-python
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
426
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
427 """
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
428 if user is None:
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
429 if filter_comments:
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
430 _filter_comments(default)
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
431 _filter_deletions(default)
13
24ba462b9b4b Return the `default' argument when the given `user' argument is `None' and when it is the first call on merge
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 11
diff changeset
432 return default
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
433 if filter_comments:
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
434 _filter_comments(user)
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
435 if isinstance(user, dict) and isinstance(default, dict):
9
6835a5663008 FIX: Style
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 8
diff changeset
436 for k, v in default.items():
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
437 if filter_comments and _is_comment(k):
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
438 continue
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
439 if k in user:
638
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
440 if isinstance(user, Configuration):
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
441 ukv = user.getitem_ns(k)
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
442 else:
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
443 ukv = user[k]
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
444 if ukv == constants.DEL_VALUE:
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
445 # do not copy
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
446 del user[k]
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
447 else:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
448 user[k] = _merge(ukv, v, filter_comments, merge_lists)
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
449 else:
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
450 user[k] = v
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
451 elif merge_lists is not None:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
452 if merge_lists == "extend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
453 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
454 for idx, value in enumerate(default):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
455 user.insert(idx, value)
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
456 elif merge_lists == "prepend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
457 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
458 user.extend(default)
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
459 else:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
460 raise ValueError(
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
461 "unknown strategy for merge_lists: %s" % (merge_lists,))
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
462 _filter_deletions(user)
8
7090c295c940 Two differend tree merge function implementations: not yet finished
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
463 return user
10
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
464
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
465
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
466 def _merge(user, default, filter_comments, merge_lists):
275
e2fd8fea1a4c Docu: more local lookup
Franz Glasner <fzglas.hg@dom66.de>
parents: 274
diff changeset
467 """Recursion helper for :func:`.merge`
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
468
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
469 """
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
470 if isinstance(user, dict) and isinstance(default, dict):
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
471 for k, v in default.items():
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
472 if filter_comments and _is_comment(k):
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
473 continue
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
474 if k in user:
638
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
475 if isinstance(user, Configuration):
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
476 ukv = user.getitem_ns(k)
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
477 else:
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
478 ukv = user[k]
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
479 if ukv == constants.DEL_VALUE:
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
480 # do not copy
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
481 del user[k]
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
482 else:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
483 user[k] = _merge(ukv, v, filter_comments, merge_lists)
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
484 else:
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
485 user[k] = v
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
486 elif merge_lists is not None:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
487 if merge_lists == "extend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
488 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
489 for idx, value in enumerate(default):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
490 user.insert(idx, value)
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
491 elif merge_lists == "prepend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
492 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
493 user.extend(default)
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
494 else:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
495 raise ValueError(
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
496 "unknown strategy for merge_lists: %s" % (merge_lists,))
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
497 return user
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
498
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
499
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
500 def safe_merge(user, default, filter_comments=True, merge_lists=None):
275
e2fd8fea1a4c Docu: more local lookup
Franz Glasner <fzglas.hg@dom66.de>
parents: 274
diff changeset
501 """A more safe version of :func:`.merge` that makes deep copies of
13
24ba462b9b4b Return the `default' argument when the given `user' argument is `None' and when it is the first call on merge
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 11
diff changeset
502 the returned container objects.
24ba462b9b4b Return the `default' argument when the given `user' argument is `None' and when it is the first call on merge
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 11
diff changeset
503
275
e2fd8fea1a4c Docu: more local lookup
Franz Glasner <fzglas.hg@dom66.de>
parents: 274
diff changeset
504 Contrary to :func:`.merge` no given argument is ever changed
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
505 inplace. Every object from `default` is decoupled from the result
165
6ca90e80f4f4 FIX: Docu: wording
Franz Glasner <fzglas.hg@dom66.de>
parents: 163
diff changeset
506 -- so changing the `default` configuration later does not propagate
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
507 into a merged configuration later.
111
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
508
10
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
509 """
110
29cf359ddf4d Remove the "_first" parameter from "merge" and "safe_merge" by splitting into two functions
Franz Glasner <hg@dom66.de>
parents: 108
diff changeset
510 if user is None:
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
511 if filter_comments:
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
512 _filter_comments(default)
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
513 _filter_deletions(default)
111
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
514 return copy.deepcopy(default)
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
515 user = copy.deepcopy(user)
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
516 if filter_comments:
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
517 _filter_comments(user)
10
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
518 if isinstance(user, dict) and isinstance(default, dict):
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
519 for k, v in default.items():
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
520 if filter_comments and _is_comment(k):
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
521 continue
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
522 if k in user:
638
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
523 if isinstance(user, Configuration):
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
524 ukv = user.getitem_ns(k)
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
525 else:
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
526 ukv = user[k]
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
527 if ukv == constants.DEL_VALUE:
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
528 # do not copy
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
529 del user[k]
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
530 else:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
531 user[k] = _safe_merge(ukv, v, filter_comments, merge_lists)
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
532 else:
111
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
533 user[k] = copy.deepcopy(v)
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
534 elif merge_lists is not None:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
535 if merge_lists == "extend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
536 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
537 for idx, value in enumerate(default):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
538 user.insert(idx, copy.deepcopy(value))
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
539 elif merge_lists == "prepend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
540 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
541 user.extend(copy.deepcopy(default))
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
542 else:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
543 raise ValueError(
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
544 "unknown strategy for merge_lists: %s" % (merge_lists,))
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
545 _filter_deletions(user)
10
58af59d5af40 A "safe_merge" that makes (shallow) copies instead of directly manipulating given containers
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 9
diff changeset
546 return user
111
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
547
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
548
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
549 def _safe_merge(user, default, filter_comments, merge_lists):
274
90bbade12d8e Docu: use local lookup firstly
Franz Glasner <fzglas.hg@dom66.de>
parents: 273
diff changeset
550 """Recursion helper for :func:`safe_merge`
111
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
551
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
552 """
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
553 if isinstance(user, dict) and isinstance(default, dict):
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
554 for k, v in default.items():
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
555 if filter_comments and _is_comment(k):
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
556 continue
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
557 if k in user:
638
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
558 if isinstance(user, Configuration):
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
559 ukv = user.getitem_ns(k)
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
560 else:
ef485419d723 FIX: Now the merge logic does not interpolate variables in any case.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 633
diff changeset
561 ukv = user[k]
641
226aae226b8d FIX: Remove last expanding lookup of a value when merging configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 638
diff changeset
562 if ukv == constants.DEL_VALUE:
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
563 # do not copy
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
564 del user[k]
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
565 else:
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
566 user[k] = _safe_merge(ukv, v, filter_comments, merge_lists)
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
567 else:
111
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
568 user[k] = copy.deepcopy(v)
741
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
569 elif merge_lists is not None:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
570 if merge_lists == "extend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
571 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
572 for idx, value in enumerate(default):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
573 user.insert(idx, copy.deepcopy(value))
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
574 elif merge_lists == "prepend":
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
575 if isinstance(user, list) and isinstance(default, list):
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
576 user.extend(copy.deepcopy(default))
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
577 else:
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
578 raise ValueError(
e069797f0e36 Implemented the new merge stragegies when merging lists: "extend" and "prepend"
Franz Glasner <fzglas.hg@dom66.de>
parents: 736
diff changeset
579 "unknown strategy for merge_lists: %s" % (merge_lists,))
111
d51a18e5b0e3 Reimplement configmix.safe_merge() do to a deepcopy of all source configurations when merging.
Franz Glasner <hg@dom66.de>
parents: 110
diff changeset
580 return user
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
581
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
582
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
583 def _filter_comments(d):
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
584 """Recursively filter comments keys in the dict `d`.
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
585
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
586 Comment keys are keys that start with any of the items in
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
587 :data:`.constants.COMMENTS`.
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
588
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
589 """
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
590 if not isinstance(d, dict):
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
591 return
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
592 # use a copy of the keys because we change `d` while iterating
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: 434
diff changeset
593 if isinstance(d, Configuration):
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: 434
diff changeset
594 for k in list(d.keys()):
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: 434
diff changeset
595 if _is_comment(k):
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: 434
diff changeset
596 del d[k]
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: 434
diff changeset
597 else:
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: 434
diff changeset
598 dk = d.getitem_ns(k)
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: 434
diff changeset
599 if isinstance(dk, dict):
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: 434
diff changeset
600 _filter_comments(dk)
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: 434
diff changeset
601 else:
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: 434
diff changeset
602 for k in list(d.keys()):
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: 434
diff changeset
603 if _is_comment(k):
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: 434
diff changeset
604 del d[k]
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: 434
diff changeset
605 else:
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: 434
diff changeset
606 if isinstance(d[k], dict):
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: 434
diff changeset
607 _filter_comments(d[k])
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
608
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
609
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
610 def _is_comment(k):
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
611 for i in constants.COMMENTS:
256
7e26d31f52de FIX: Allow non-text keys when merging: handle .startswith() errors gracefully
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 253
diff changeset
612 try:
7e26d31f52de FIX: Allow non-text keys when merging: handle .startswith() errors gracefully
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 253
diff changeset
613 if k.startswith(i):
7e26d31f52de FIX: Allow non-text keys when merging: handle .startswith() errors gracefully
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 253
diff changeset
614 return True
7e26d31f52de FIX: Allow non-text keys when merging: handle .startswith() errors gracefully
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 253
diff changeset
615 except AttributeError:
7e26d31f52de FIX: Allow non-text keys when merging: handle .startswith() errors gracefully
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 253
diff changeset
616 # non-string key
7e26d31f52de FIX: Allow non-text keys when merging: handle .startswith() errors gracefully
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 253
diff changeset
617 return False
144
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
618 return False
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
619
7e6ec99d5ff5 Allow comments as keys and filter them by default
Franz Glasner <hg@dom66.de>
parents: 141
diff changeset
620
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
621 def _filter_deletions(d):
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
622 """Recursively filter deletions in the dict `d`.
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
623
303
2a2f5b86fe34 Move some important public constants into the .constants sub-module
Franz Glasner <fzglas.hg@dom66.de>
parents: 297
diff changeset
624 Deletions have values that equal :data:`.constants.DEL_VALUE`.
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
625
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
626 """
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
627 if not isinstance(d, dict):
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
628 return
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
629 # use a copy of the items because we change `d` while iterating
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: 434
diff changeset
630 if isinstance(d, Configuration):
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: 434
diff changeset
631 for k, v in list(d.items()):
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: 434
diff changeset
632 if v == constants.DEL_VALUE:
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: 434
diff changeset
633 del d[k]
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: 434
diff changeset
634 else:
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: 434
diff changeset
635 dk = d.getitem_ns(k)
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: 434
diff changeset
636 if isinstance(dk, dict):
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: 434
diff changeset
637 _filter_deletions(dk)
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: 434
diff changeset
638 else:
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: 434
diff changeset
639 for k, v in list(d.items()):
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: 434
diff changeset
640 if v == constants.DEL_VALUE:
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: 434
diff changeset
641 del d[k]
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: 434
diff changeset
642 else:
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: 434
diff changeset
643 if isinstance(d[k], dict):
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: 434
diff changeset
644 _filter_deletions(d[k])
276
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
645
af371f9c016d Allow deletion of key-value pairs when merging is done.
Franz Glasner <fzglas.hg@dom66.de>
parents: 275
diff changeset
646
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
647 #
178
eeb3ed104ea1 Begin refactoring the associations between filename extensions and loader functions:
Franz Glasner <fzglas.hg@dom66.de>
parents: 177
diff changeset
648 # Init loader defaults: mode->loader and extension->mode
139
c87b0dc54e1d Allow custom configuration filename extensions and custom loaders that can handle custom configuration file syntax styles
Franz Glasner <hg@dom66.de>
parents: 138
diff changeset
649 #
189
0d0980ed74cc Rename the "_mode_loaders" variable to "mode_loaders" and make is therefore a "public" item
Franz Glasner <fzglas.hg@dom66.de>
parents: 188
diff changeset
650 mode_loaders.update(DEFAULT_MODE_LOADERS)
181
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
651 for _pattern, _mode in DEFAULT_ASSOC:
7cfdc972af42 Refactor: Renamed public functions to be conform with the new loader search
Franz Glasner <fzglas.hg@dom66.de>
parents: 180
diff changeset
652 set_assoc(_pattern, _mode)