Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/__init__.py @ 181:7cfdc972af42
Refactor: Renamed public functions to be conform with the new loader search
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 03 May 2019 09:43:15 +0200 |
| parents | e87fa5bd68e7 |
| children | fa101fb0cd7a |
comparison
equal
deleted
inserted
replaced
| 180:e87fa5bd68e7 | 181:7cfdc972af42 |
|---|---|
| 17 | 17 |
| 18 __revision__ = "|VCSRevision|" | 18 __revision__ = "|VCSRevision|" |
| 19 __date__ = "|VCSJustDate|" | 19 __date__ = "|VCSJustDate|" |
| 20 | 20 |
| 21 __all__ = ["load", "safe_load", | 21 __all__ = ["load", "safe_load", |
| 22 "set_loader", "get_loader", | 22 "set_assoc", "get_assoc", "clear_assoc", |
| 23 "get_default_loader", | 23 "get_default_assoc", |
| 24 "Configuration", | 24 "Configuration", |
| 25 "try_determine_filemode"] | 25 "try_determine_filemode"] |
| 26 | 26 |
| 27 | 27 |
| 28 import fnmatch | 28 import fnmatch |
| 158 "json": _load_json, | 158 "json": _load_json, |
| 159 } | 159 } |
| 160 """Default associations between file modes and loader functions""" | 160 """Default associations between file modes and loader functions""" |
| 161 | 161 |
| 162 | 162 |
| 163 DEFAULT_EXTENSIONS = [ | 163 DEFAULT_ASSOC = [ |
| 164 ("*.yml", "yaml"), | 164 ("*.yml", "yaml"), |
| 165 ("*.yaml", "yaml"), | 165 ("*.yaml", "yaml"), |
| 166 ("*.json", "json"), | 166 ("*.json", "json"), |
| 167 ("*.py", "python"), | 167 ("*.py", "python"), |
| 168 ("*.ini", "conf"), | 168 ("*.ini", "conf"), |
| 176 | 176 |
| 177 """ | 177 """ |
| 178 | 178 |
| 179 | 179 |
| 180 DEFAULT_LOADER = object() | 180 DEFAULT_LOADER = object() |
| 181 """Marker for the default loader for an extension. | 181 """Marker for the default association for an extension. |
| 182 | 182 |
| 183 To be used in :func:`set_loader`. | 183 To be used in :func:`set_assoc`. |
| 184 """ | 184 """ |
| 185 | 185 |
| 186 def get_default_loader(pattern): | 186 |
| 187 """Return the default loader for the :mod:`fnmatch` pattern `pattern`. | 187 def get_default_assoc(pattern): |
| 188 """Return the default file-mode association for the :mod:`fnmatch` | |
| 189 pattern `pattern`. | |
| 188 | 190 |
| 189 :raises: :class:`KeyError` if the `pattern` is not found. | 191 :raises: :class:`KeyError` if the `pattern` is not found. |
| 190 | 192 |
| 191 """ | 193 """ |
| 192 for pat, loader in _default_loaders: | 194 for pat, fmode in DEFAULT_ASSOC: |
| 193 if pattern == pat: | 195 if pattern == pat: |
| 194 return loader | 196 return fmode |
| 195 else: | 197 else: |
| 196 raise KeyError("No loader for pattern %r" % pattern) | 198 raise KeyError("No loader for pattern %r" % pattern) |
| 197 | 199 |
| 198 | 200 |
| 199 _mode_loaders = {} | 201 _mode_loaders = {} |
| 204 """ | 206 """ |
| 205 | 207 |
| 206 _extensions = [] | 208 _extensions = [] |
| 207 """All configured assiciations of filename extensions with file modes. | 209 """All configured assiciations of filename extensions with file modes. |
| 208 | 210 |
| 209 See :data:`DEFAULT_EXTENSIONS` | 211 See :data:`DEFAULT_ASSOC` |
| 210 | 212 |
| 211 """ | 213 """ |
| 212 | 214 |
| 213 | 215 |
| 214 def clear_loader(): | 216 def clear_assoc(): |
| 215 """Remove all configured loader associations. | 217 """Remove all configured loader associations. |
| 216 | 218 |
| 217 The :data:`_default_loaders` are **not** changed. | 219 The :data:`_default_loaders` are **not** changed. |
| 218 | 220 |
| 219 """ | 221 """ |
| 220 del _loaders[:] | 222 del _extensions[:] |
| 221 | 223 |
| 222 | 224 |
| 223 def get_loader(pattern): | 225 def get_assoc(pattern): |
| 224 """Return the default loader for the :mod:`fnmatch` pattern `pattern`. | 226 """Return the default loader for the :mod:`fnmatch` pattern `pattern`. |
| 225 | 227 |
| 226 :raises: :class:`KeyError` if the `pattern` is not found. | 228 :raises: :class:`KeyError` if the `pattern` is not found. |
| 227 | 229 |
| 228 """ | 230 """ |
| 229 for pat, loader in _loaders: | 231 for pat, fmode in _extensions: |
| 230 if pattern == pat: | 232 if pattern == pat: |
| 231 return loader | 233 return fmode |
| 232 else: | 234 else: |
| 233 raise KeyError("No loader for pattern %r" % pattern) | 235 raise KeyError("No associated file-mode for pattern %r" % pattern) |
| 234 | 236 |
| 235 | 237 |
| 236 def set_loader(fnpattern, mode): | 238 def set_assoc(fnpattern, mode): |
| 237 """Associate a :mod:`fnmatch` style pattern `fnpattern` with a | 239 """Associate a :mod:`fnmatch` style pattern `fnpattern` with a |
| 238 file-mode `mode` that determines what will be called when | 240 file-mode `mode` that determines what will be called when |
| 239 :func:`load` encounters a file argument that matches `fnpattern`. | 241 :func:`load` encounters a file argument that matches `fnpattern`. |
| 240 | 242 |
| 241 :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader | 243 :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader |
| 249 | 251 |
| 250 The OS specific case-sensitivity behaviour of | 252 The OS specific case-sensitivity behaviour of |
| 251 :func:`fnmatch.fnmatch` applies (i.e. :func:`os.path.normpath` | 253 :func:`fnmatch.fnmatch` applies (i.e. :func:`os.path.normpath` |
| 252 will be called for both arguments). | 254 will be called for both arguments). |
| 253 | 255 |
| 254 If `loader` is :data:`DEFAULT_LOADER` then the default association | 256 If `loader` is :data:`DEFAULT_ASSOC` then the default association |
| 255 from :data:`_default_loaders` will be used -- if any. | 257 from :data:`_default_loaders` will be used -- if any. |
| 256 | 258 |
| 257 """ | 259 """ |
| 258 if mode is DEFAULT_LOADER: | 260 if mode is DEFAULT_LOADER: |
| 259 for p, m in DEFAULT_EXTENSIONS: | 261 for p, m in DEFAULT_ASSOC: |
| 260 if p == fnpattern: | 262 if p == fnpattern: |
| 261 _extensions.insert(0, (fnpattern, m)) | 263 _extensions.insert(0, (fnpattern, m)) |
| 262 break | 264 break |
| 263 else: | 265 else: |
| 264 raise ValueError("no DEFAULT mode for pattern: %r" % fnpattern) | 266 raise ValueError("no DEFAULT mode for pattern: %r" % fnpattern) |
| 424 | 426 |
| 425 # | 427 # |
| 426 # Init loader defaults: mode->loader and extension->mode | 428 # Init loader defaults: mode->loader and extension->mode |
| 427 # | 429 # |
| 428 _mode_loaders.update(DEFAULT_MODE_LOADERS) | 430 _mode_loaders.update(DEFAULT_MODE_LOADERS) |
| 429 for _pattern, _mode in DEFAULT_EXTENSIONS: | 431 for _pattern, _mode in DEFAULT_ASSOC: |
| 430 set_loader(_pattern, _mode) | 432 set_assoc(_pattern, _mode) |
