comparison configmix/__init__.py @ 227:f5011eec3b6e

Added a loader with key "ignore" that ignores the given configuration file
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 12 May 2019 18:06:37 +0200
parents 8b1c52d5e7d5
children b2c75efad9e4
comparison
equal deleted inserted replaced
226:8b1c52d5e7d5 227:f5011eec3b6e
68 if defaults is None: 68 if defaults is None:
69 ex = Configuration() 69 ex = Configuration()
70 else: 70 else:
71 ex = merge(None, Configuration(defaults)) 71 ex = merge(None, Configuration(defaults))
72 for f in files: 72 for f in files:
73 ex = merge(_load_cfg_from_file(f), ex) 73 nx = _load_cfg_from_file(f)
74 if nx is not None:
75 ex = merge(nx, ex)
74 if extras: 76 if extras:
75 ex = merge(Configuration(extras), ex) 77 ex = merge(Configuration(extras), ex)
76 return Configuration(ex) 78 return Configuration(ex)
77 79
78 80
86 if defaults is None: 88 if defaults is None:
87 ex = Configuration() 89 ex = Configuration()
88 else: 90 else:
89 ex = safe_merge(None, Configuration(defaults)) 91 ex = safe_merge(None, Configuration(defaults))
90 for f in files: 92 for f in files:
91 ex = safe_merge(_load_cfg_from_file(f), ex) 93 nx = _load_cfg_from_file(f)
94 if nx is not None:
95 ex = safe_merge(nx, ex)
92 if extras: 96 if extras:
93 ex = safe_merge(Configuration(extras), ex) 97 ex = safe_merge(Configuration(extras), ex)
94 return Configuration(ex) 98 return Configuration(ex)
95 99
96 100
116 120
117 121
118 def _load_toml(filename): 122 def _load_toml(filename):
119 from . import toml 123 from . import toml
120 return toml.load(filename) 124 return toml.load(filename)
125
126
127 def _load_ignore(filename):
128 """A loader that returns `None` just to ignore `filename`"""
129 return None
121 130
122 131
123 EMACS_MODELINE = re.compile(r"-\*-(.*?)-\*-") 132 EMACS_MODELINE = re.compile(r"-\*-(.*?)-\*-")
124 EMACS_MODE = re.compile(r"(?:\A\s*|;\s*)mode[:=]\s*([-_.a-zA-Z0-9]+)") 133 EMACS_MODE = re.compile(r"(?:\A\s*|;\s*)mode[:=]\s*([-_.a-zA-Z0-9]+)")
125 134
160 "ini": _load_ini, 169 "ini": _load_ini,
161 "toml": _load_toml, 170 "toml": _load_toml,
162 "conf-toml": _load_toml, 171 "conf-toml": _load_toml,
163 "javascript": _load_json, 172 "javascript": _load_json,
164 "json": _load_json, 173 "json": _load_json,
174 "ignore": _load_ignore,
165 } 175 }
166 """Default associations between file modes and loader functions""" 176 """Default associations between file modes and loader functions"""
167 177
168 178
169 DEFAULT_ASSOC = [ 179 DEFAULT_ASSOC = [
282 else: 292 else:
283 _extensions.insert(0, (fnpattern, mode)) 293 _extensions.insert(0, (fnpattern, mode))
284 294
285 295
286 def _load_cfg_from_file(filename): 296 def _load_cfg_from_file(filename):
297 """Determine the loader for file `filename` and return the loaded
298 configuration dict.
299
300 Can return `None` is the file should be ignored by the caller.
301
302 """
287 for p, m in _extensions: 303 for p, m in _extensions:
288 if fnmatch.fnmatch(filename, p): 304 if fnmatch.fnmatch(filename, p):
289 if callable(m): 305 if callable(m):
290 m = m(filename) 306 m = m(filename)
291 if m is None: 307 if m is None: