changeset 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 5c27e52c3483
children d2eb83720ad8
files configmix/__init__.py
diffstat 1 files changed, 16 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/__init__.py	Fri May 03 19:32:04 2019 +0200
+++ b/configmix/__init__.py	Fri May 03 19:41:35 2019 +0200
@@ -235,19 +235,21 @@
         raise KeyError("No associated file-mode for pattern %r" % pattern)
 
 
-def set_assoc(fnpattern, mode):
+def set_assoc(fnpattern, mode, append=False):
     """Associate a :mod:`fnmatch` style pattern `fnpattern` with a
     file-mode `mode` that determines what will be called when
     :func:`load` encounters a file argument that matches `fnpattern`.
 
     :param str fnpattern: the :mod:`fnmatch` pattern to associate a loader
                           with
-    :param callable mode: a mode string or a callable that accepts a
-                          `filename` argument and returns a file-mode for
-                          the given file (or `None`)
+    :param mode: a mode string or a callable that accepts a `filename`
+                 argument and returns a file-mode for the given file
+                 (or `None`)
+    :type mode: str or callable
 
-    This function prepends to the given pattern to the currently defined
-    associations.
+    :keyword bool append: If `False` (which is the default) then this
+        function inserts the given pattern at the head position of the
+        currently defined associations, if `True` the pattern will be appended
 
     The OS specific case-sensitivity behaviour of
     :func:`fnmatch.fnmatch` applies (i.e. :func:`os.path.normpath`
@@ -260,12 +262,18 @@
     if mode is DEFAULT_LOADER:
         for p, m in DEFAULT_ASSOC:
             if p == fnpattern:
-                _extensions.insert(0, (fnpattern, m))
+                if append:
+                    _extensions.append((fnpattern, m))
+                else:
+                    _extensions.insert(0, (fnpattern, m))
                 break
         else:
             raise ValueError("no DEFAULT mode for pattern: %r" % fnpattern)
     else:
-        _extensions.insert(0, (fnpattern, mode))
+        if append:
+            _extensions.append((fnpattern, mode))
+        else:
+            _extensions.insert(0, (fnpattern, mode))
 
 
 def _load_cfg_from_file(filename):