comparison mercurial/config.py @ 25095:3182965b3971

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.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Fri, 15 May 2015 09:04:32 -0400
parents fdfc9faca273
children 328739ea70c3
comparison
equal deleted inserted replaced
25093:fe3a72a3e7ca 25095:3182965b3971
8 from i18n import _ 8 from i18n import _
9 import error, util 9 import error, util
10 import os, errno 10 import os, errno
11 11
12 class config(object): 12 class config(object):
13 def __init__(self, data=None): 13 def __init__(self, data=None, includepaths=[]):
14 self._data = {} 14 self._data = {}
15 self._source = {} 15 self._source = {}
16 self._unset = [] 16 self._unset = []
17 self._includepaths = includepaths
17 if data: 18 if data:
18 for k in data._data: 19 for k in data._data:
19 self._data[k] = data[k].copy() 20 self._data[k] = data[k].copy()
20 self._source = data._source.copy() 21 self._source = data._source.copy()
21 def copy(self): 22 def copy(self):
108 self.set(section, item, v, "%s:%d" % (src, line)) 109 self.set(section, item, v, "%s:%d" % (src, line))
109 continue 110 continue
110 item = None 111 item = None
111 cont = False 112 cont = False
112 m = includere.match(l) 113 m = includere.match(l)
113 if m: 114
114 inc = util.expandpath(m.group(1)) 115 if m and include:
115 base = os.path.dirname(src) 116 expanded = util.expandpath(m.group(1))
116 inc = os.path.normpath(os.path.join(base, inc)) 117 includepaths = [os.path.dirname(src)] + self._includepaths
117 if include: 118
119 for base in includepaths:
120 inc = os.path.normpath(os.path.join(base, expanded))
121
118 try: 122 try:
119 include(inc, remap=remap, sections=sections) 123 include(inc, remap=remap, sections=sections)
124 break
120 except IOError, inst: 125 except IOError, inst:
121 if inst.errno != errno.ENOENT: 126 if inst.errno != errno.ENOENT:
122 raise error.ParseError(_("cannot include %s (%s)") 127 raise error.ParseError(_("cannot include %s (%s)")
123 % (inc, inst.strerror), 128 % (inc, inst.strerror),
124 "%s:%s" % (src, line)) 129 "%s:%s" % (src, line))