Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/__init__.py @ 186:fa101fb0cd7a
Implement an "append" keyword to "configmix.set_assoc()"
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 03 May 2019 19:41:35 +0200 |
| parents | 7cfdc972af42 |
| children | d2eb83720ad8 |
comparison
equal
deleted
inserted
replaced
| 185:5c27e52c3483 | 186:fa101fb0cd7a |
|---|---|
| 233 return fmode | 233 return fmode |
| 234 else: | 234 else: |
| 235 raise KeyError("No associated file-mode for pattern %r" % pattern) | 235 raise KeyError("No associated file-mode for pattern %r" % pattern) |
| 236 | 236 |
| 237 | 237 |
| 238 def set_assoc(fnpattern, mode): | 238 def set_assoc(fnpattern, mode, append=False): |
| 239 """Associate a :mod:`fnmatch` style pattern `fnpattern` with a | 239 """Associate a :mod:`fnmatch` style pattern `fnpattern` with a |
| 240 file-mode `mode` that determines what will be called when | 240 file-mode `mode` that determines what will be called when |
| 241 :func:`load` encounters a file argument that matches `fnpattern`. | 241 :func:`load` encounters a file argument that matches `fnpattern`. |
| 242 | 242 |
| 243 :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader | 243 :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader |
| 244 with | 244 with |
| 245 :param callable mode: a mode string or a callable that accepts a | 245 :param mode: a mode string or a callable that accepts a `filename` |
| 246 `filename` argument and returns a file-mode for | 246 argument and returns a file-mode for the given file |
| 247 the given file (or `None`) | 247 (or `None`) |
| 248 | 248 :type mode: str or callable |
| 249 This function prepends to the given pattern to the currently defined | 249 |
| 250 associations. | 250 :keyword bool append: If `False` (which is the default) then this |
| 251 function inserts the given pattern at the head position of the | |
| 252 currently defined associations, if `True` the pattern will be appended | |
| 251 | 253 |
| 252 The OS specific case-sensitivity behaviour of | 254 The OS specific case-sensitivity behaviour of |
| 253 :func:`fnmatch.fnmatch` applies (i.e. :func:`os.path.normpath` | 255 :func:`fnmatch.fnmatch` applies (i.e. :func:`os.path.normpath` |
| 254 will be called for both arguments). | 256 will be called for both arguments). |
| 255 | 257 |
| 258 | 260 |
| 259 """ | 261 """ |
| 260 if mode is DEFAULT_LOADER: | 262 if mode is DEFAULT_LOADER: |
| 261 for p, m in DEFAULT_ASSOC: | 263 for p, m in DEFAULT_ASSOC: |
| 262 if p == fnpattern: | 264 if p == fnpattern: |
| 263 _extensions.insert(0, (fnpattern, m)) | 265 if append: |
| 266 _extensions.append((fnpattern, m)) | |
| 267 else: | |
| 268 _extensions.insert(0, (fnpattern, m)) | |
| 264 break | 269 break |
| 265 else: | 270 else: |
| 266 raise ValueError("no DEFAULT mode for pattern: %r" % fnpattern) | 271 raise ValueError("no DEFAULT mode for pattern: %r" % fnpattern) |
| 267 else: | 272 else: |
| 268 _extensions.insert(0, (fnpattern, mode)) | 273 if append: |
| 274 _extensions.append((fnpattern, mode)) | |
| 275 else: | |
| 276 _extensions.insert(0, (fnpattern, mode)) | |
| 269 | 277 |
| 270 | 278 |
| 271 def _load_cfg_from_file(filename): | 279 def _load_cfg_from_file(filename): |
| 272 for p, m in _extensions: | 280 for p, m in _extensions: |
| 273 if fnmatch.fnmatch(filename, p): | 281 if fnmatch.fnmatch(filename, p): |
