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

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date May 5, 2015, 9:39 p.m.
Message ID <b7e9b8d77e46052e639f.1430861952@Iris>
Download mbox | patch
Permalink /patch/8915/
State Accepted
Headers show

Comments

Jordi Gutiérrez Hermoso - May 5, 2015, 9:39 p.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1430860459 14400
#      Tue May 05 17:14:19 2015 -0400
# Node ID b7e9b8d77e46052e639fca04b1aee2cfdefbd4f7
# Parent  dabfe2777f993de4ed1ed16886559d5ebf42a385
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.
Yuya Nishihara - May 6, 2015, 6:40 a.m.
On Tue, 05 May 2015 17:39:12 -0400, Jordi Gutiérrez Hermoso wrote:
> # HG changeset patch
> # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
> # Date 1430860459 14400
> #      Tue May 05 17:14:19 2015 -0400
> # Node ID b7e9b8d77e46052e639fca04b1aee2cfdefbd4f7
> # Parent  dabfe2777f993de4ed1ed16886559d5ebf42a385
> 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=[]):
>          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):
> +                        continue

typo of "break" ?
Jordi Gutiérrez Hermoso - May 6, 2015, 1:22 p.m.
On Wed, 2015-05-06 at 15:40 +0900, Yuya Nishihara wrote:
> On Tue, 05 May 2015 17:39:12 -0400, Jordi Gutiérrez Hermoso wrote:
> > # HG changeset patch
> > # User Jordi Gutiérrez Hermoso <jordigh@octave.org>
> > # Date 1430860459 14400
> > #      Tue May 05 17:14:19 2015 -0400
> > # Node ID b7e9b8d77e46052e639fca04b1aee2cfdefbd4f7
> > # Parent  dabfe2777f993de4ed1ed16886559d5ebf42a385
> > config: give it a searchpaths option, for extra places for config files
[snip]
> > +                        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):
> > +                        continue
> 
> typo of "break" ?

Ah, curses, yes, you're right again. Thank you. I'll amend and resend.

- Jordi G. H.

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):
+                        continue
+
                 continue
             if emptyre.match(l):
                 continue