Patchwork [5,of,6,V3] config: give it a searchpaths option, for extra places for config files

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date May 10, 2015, 6:44 p.m.
Message ID <f23483529388f9dcb66e.1431283485@Iris>
Download mbox | patch
Permalink /patch/9009/
State Accepted
Headers show

Comments

Jordi Gutiérrez Hermoso - May 10, 2015, 6:44 p.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1431281253 14400
#      Sun May 10 14:07:33 2015 -0400
# Node ID f23483529388f9dcb66e4eaceb10b2e6a5c0f0fb
# Parent  c793b56f22127b01564b42d43f018c24f18b84fc
config: give it a searchpaths option, for extra places 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 search paths
for config files.
Pierre-Yves David - May 12, 2015, 2:31 a.m.
On 05/10/2015 11:44 AM, Jordi Gutiérrez Hermoso wrote:
> # HG changeset patch
> # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
> # Date 1431281253 14400
> #      Sun May 10 14:07:33 2015 -0400
> # Node ID f23483529388f9dcb66e4eaceb10b2e6a5c0f0fb
> # Parent  c793b56f22127b01564b42d43f018c24f18b84fc
> config: give it a searchpaths option, for extra places 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 search paths
> for config files.
>
> 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, searchpaths=[]):

Could we call this 'includepaths' as initially suggested by Yuya. I 
would find that clearer.

>           self._data = {}
>           self._source = {}
>           self._unset = []
> +        self._searchpaths = searchpaths
>           if data:
>               for k in data._data:
>                   self._data[k] = data[k].copy()
> @@ -110,18 +111,28 @@ 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))
> +            def includefile(inc):

Why does this has to be a closure? could this be a method or a function 
? (with some doc?)

>                   if include:
>                       try:
>                           include(inc, remap=remap, sections=sections)
> +                        return True
>                       except IOError, inst:
>                           if inst.errno != errno.ENOENT:
>                               raise error.ParseError(_("cannot include %s (%s)")
>                                                      % (inc, inst.strerror),
>                                                      "%s:%s" % (src, line))
> +                        else:
> +                            return False
> +            if m:
> +                expanded = util.expandpath(m.group(1))
> +                searchpaths = [os.path.dirname(src)] + self._searchpaths
> +
> +                for base in searchpaths:
> +                    inc = os.path.normpath(os.path.join(base, expanded))
> +
> +                    if includefile(inc):
> +                        break
> +
>                   continue
>               if emptyre.match(l):
>                   continue

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, searchpaths=[]):
         self._data = {}
         self._source = {}
         self._unset = []
+        self._searchpaths = searchpaths
         if data:
             for k in data._data:
                 self._data[k] = data[k].copy()
@@ -110,18 +111,28 @@  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))
+            def includefile(inc):
                 if include:
                     try:
                         include(inc, remap=remap, sections=sections)
+                        return True
                     except IOError, inst:
                         if inst.errno != errno.ENOENT:
                             raise error.ParseError(_("cannot include %s (%s)")
                                                    % (inc, inst.strerror),
                                                    "%s:%s" % (src, line))
+                        else:
+                            return False
+            if m:
+                expanded = util.expandpath(m.group(1))
+                searchpaths = [os.path.dirname(src)] + self._searchpaths
+
+                for base in searchpaths:
+                    inc = os.path.normpath(os.path.join(base, expanded))
+
+                    if includefile(inc):
+                        break
+
                 continue
             if emptyre.match(l):
                 continue