Patchwork [1,of,2,V4] config: give it an includepaths option for looking for config files

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date May 15, 2015, 3:23 p.m.
Message ID <a5f23191d2ba984ded28.1431703418@Iris>
Download mbox | patch
Permalink /patch/9100/
State Accepted
Headers show

Comments

Jordi Gutiérrez Hermoso - May 15, 2015, 3:23 p.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1431695072 14400
#      Fri May 15 09:04:32 2015 -0400
# Node ID a5f23191d2ba984ded287675452bea072fde5b1f
# Parent  d1bd0fd07ee6adf4ab3be2b0a0a7c0df54d55abf
config: give it an includepaths option for looking for config files

It is desirable to "derive" templates from the provided templates. A
simple way to do this is e.g.

    %include map-cmdline.default

in your own mapfile. Then you only have to redefine a few templates
instead of copying over the whole thing. This %include mechanism
already works for the built-in templates because by default it *only*
looks for files that are in the same directory as the including
mapfile.

With this changeset, config grows an option to add more include paths
for config files.

Patch

diff --git a/mercurial/config.py b/mercurial/config.py
--- a/mercurial/config.py
+++ b/mercurial/config.py
@@ -10,10 +10,11 @@  import error, util
 import os, errno
 
 class config(object):
-    def __init__(self, data=None):
+    def __init__(self, data=None, includepaths=[]):
         self._data = {}
         self._source = {}
         self._unset = []
+        self._includepaths = includepaths
         if data:
             for k in data._data:
                 self._data[k] = data[k].copy()
@@ -110,13 +111,17 @@  class config(object):
                 item = None
                 cont = False
             m = includere.match(l)
-            if m:
-                inc = util.expandpath(m.group(1))
-                base = os.path.dirname(src)
-                inc = os.path.normpath(os.path.join(base, inc))
-                if include:
+
+            if m and include:
+                expanded = util.expandpath(m.group(1))
+                includepaths = [os.path.dirname(src)] + self._includepaths
+
+                for base in includepaths:
+                    inc = os.path.normpath(os.path.join(base, expanded))
+
                     try:
                         include(inc, remap=remap, sections=sections)
+                        break
                     except IOError, inst:
                         if inst.errno != errno.ENOENT:
                             raise error.ParseError(_("cannot include %s (%s)")