##// END OF EJS Templates
config: track the "level" of a value...
marmoute -
r47367:a3dced4b default
parent child Browse files
Show More
@@ -22,11 +22,19 b' from . import ('
22
22
23 class config(object):
23 class config(object):
24 def __init__(self, data=None):
24 def __init__(self, data=None):
25 self._current_source_level = 0
25 self._data = {}
26 self._data = {}
26 self._unset = []
27 self._unset = []
27 if data:
28 if data:
28 for k in data._data:
29 for k in data._data:
29 self._data[k] = data[k].copy()
30 self._data[k] = data[k].copy()
31 self._current_source_level = data._current_source_level + 1
32
33 def new_source(self):
34 """increment the source counter
35
36 This is used to define source priority when reading"""
37 self._current_source_level += 1
30
38
31 def copy(self):
39 def copy(self):
32 return config(self)
40 return config(self)
@@ -45,6 +53,9 b' class config(object):'
45 yield d
53 yield d
46
54
47 def update(self, src):
55 def update(self, src):
56 current_level = self._current_source_level
57 current_level += 1
58 max_level = self._current_source_level
48 for s, n in src._unset:
59 for s, n in src._unset:
49 ds = self._data.get(s, None)
60 ds = self._data.get(s, None)
50 if ds is not None and n in ds:
61 if ds is not None and n in ds:
@@ -56,7 +67,12 b' class config(object):'
56 self._data[s] = ds.preparewrite()
67 self._data[s] = ds.preparewrite()
57 else:
68 else:
58 self._data[s] = util.cowsortdict()
69 self._data[s] = util.cowsortdict()
59 self._data[s].update(src._data[s])
70 for k, v in src._data[s].items():
71 value, source, level = v
72 level += current_level
73 max_level = max(level, current_level)
74 self._data[s][k] = (value, source, level)
75 self._current_source_level = max_level
60
76
61 def _get(self, section, item):
77 def _get(self, section, item):
62 return self._data.get(section, {}).get(item)
78 return self._data.get(section, {}).get(item)
@@ -85,12 +101,18 b' class config(object):'
85 return b""
101 return b""
86 return result[1]
102 return result[1]
87
103
104 def level(self, section, item):
105 result = self._get(section, item)
106 if result is None:
107 return None
108 return result[2]
109
88 def sections(self):
110 def sections(self):
89 return sorted(self._data.keys())
111 return sorted(self._data.keys())
90
112
91 def items(self, section):
113 def items(self, section):
92 items = pycompat.iteritems(self._data.get(section, {}))
114 items = pycompat.iteritems(self._data.get(section, {}))
93 return [(k, v) for (k, (v, s)) in items]
115 return [(k, v[0]) for (k, v) in items]
94
116
95 def set(self, section, item, value, source=b""):
117 def set(self, section, item, value, source=b""):
96 if pycompat.ispy3:
118 if pycompat.ispy3:
@@ -107,7 +129,7 b' class config(object):'
107 self._data[section] = util.cowsortdict()
129 self._data[section] = util.cowsortdict()
108 else:
130 else:
109 self._data[section] = self._data[section].preparewrite()
131 self._data[section] = self._data[section].preparewrite()
110 self._data[section][item] = (value, source)
132 self._data[section][item] = (value, source, self._current_source_level)
111
133
112 def alter(self, section, key, new_value):
134 def alter(self, section, key, new_value):
113 """alter a value without altering its source or level
135 """alter a value without altering its source or level
@@ -215,6 +237,7 b' class config(object):'
215 raise error.ConfigError(message, (b"%s:%d" % (src, line)))
237 raise error.ConfigError(message, (b"%s:%d" % (src, line)))
216
238
217 def read(self, path, fp=None, sections=None, remap=None):
239 def read(self, path, fp=None, sections=None, remap=None):
240 self.new_source()
218 if not fp:
241 if not fp:
219 fp = util.posixfile(path, b'rb')
242 fp = util.posixfile(path, b'rb')
220 assert (
243 assert (
@@ -229,6 +252,8 b' class config(object):'
229 def include(rel, remap, sections):
252 def include(rel, remap, sections):
230 abs = os.path.normpath(os.path.join(dir, rel))
253 abs = os.path.normpath(os.path.join(dir, rel))
231 self.read(abs, remap=remap, sections=sections)
254 self.read(abs, remap=remap, sections=sections)
255 # anything after the include has a higher level
256 self.new_source()
232
257
233 self.parse(
258 self.parse(
234 path, fp.read(), sections=sections, remap=remap, include=include
259 path, fp.read(), sections=sections, remap=remap, include=include
@@ -302,6 +302,11 b' class ui(object):'
302 if k in self.environ:
302 if k in self.environ:
303 self._exportableenviron[k] = self.environ[k]
303 self._exportableenviron[k] = self.environ[k]
304
304
305 def _new_source(self):
306 self._ocfg.new_source()
307 self._tcfg.new_source()
308 self._ucfg.new_source()
309
305 @classmethod
310 @classmethod
306 def load(cls):
311 def load(cls):
307 """Create a ui and load global and user configs"""
312 """Create a ui and load global and user configs"""
@@ -313,6 +318,7 b' class ui(object):'
313 elif t == b'resource':
318 elif t == b'resource':
314 u.read_resource_config(f, trust=True)
319 u.read_resource_config(f, trust=True)
315 elif t == b'items':
320 elif t == b'items':
321 u._new_source()
316 sections = set()
322 sections = set()
317 for section, name, value, source in f:
323 for section, name, value, source in f:
318 # do not set u._ocfg
324 # do not set u._ocfg
@@ -325,6 +331,7 b' class ui(object):'
325 else:
331 else:
326 raise error.ProgrammingError(b'unknown rctype: %s' % t)
332 raise error.ProgrammingError(b'unknown rctype: %s' % t)
327 u._maybetweakdefaults()
333 u._maybetweakdefaults()
334 u._new_source() # anything after that is a different level
328 return u
335 return u
329
336
330 def _maybetweakdefaults(self):
337 def _maybetweakdefaults(self):
General Comments 0
You need to be logged in to leave comments. Login now