# HG changeset patch # User Pierre-Yves David # Date 2012-01-18 15:52:26 # Node ID 69e792cf785180fde19d755a9b5b46c9729ccee5 # Parent 4f9853e7f690c89096b594d25d861e35533e673d config: have a way to backup and restore value in config This is introduce to allow temporary overwriting of a config value while being able to reinstall the old value once done. The main advantage over using ``config`` and ``setconfig`` is that backup and restore will properly restore the lack of any config. Restoring the fact that there was no value is important to allow config user to keep using meaniful default value. A more naive approach will result in the following scenario:: Before: config(section, item, my_default) --> my_default temporal overwrite old = config(section, item) … setconfig(section, item, old) After config(section, item, my_default) --> None The first user of this feature should be mq to overwriting minimal phase of future commit. diff --git a/mercurial/config.py b/mercurial/config.py --- a/mercurial/config.py +++ b/mercurial/config.py @@ -61,6 +61,19 @@ class config(object): self._source.update(src._source) def get(self, section, item, default=None): return self._data.get(section, {}).get(item, default) + + def backup(self, section, item): + """return a tuple allowing restore to reinstall a previous valuesi + + The main reason we need it is because it handle the "no data" case. + """ + try: + value = self._data[section][item] + source = self.source(section, item) + return (section, item, value, source) + except KeyError: + return (section, item) + def source(self, section, item): return self._source.get((section, item), "") def sections(self): @@ -73,6 +86,20 @@ class config(object): self._data[section][item] = value self._source[(section, item)] = source + def restore(self, data): + """restore data returned by self.backup""" + if len(data) == 4: + # restore old data + section, item, value, source = data + self._data[section][item] = value + self._source[(section, item)] = source + else: + # no data before, remove everything + section, item = data + if section in self._data: + del self._data[section][item] + self._source.pop((section, item), None) + def parse(self, src, data, sections=None, remap=None, include=None): sectionre = re.compile(r'\[([^\[]+)\]') itemre = re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -142,6 +142,15 @@ class ui(object): self._trustusers.update(self.configlist('trusted', 'users')) self._trustgroups.update(self.configlist('trusted', 'groups')) + def backupconfig(self, section, item): + return (self._ocfg.backup(section, item), + self._tcfg.backup(section, item), + self._ucfg.backup(section, item),) + def restoreconfig(self, data): + self._ocfg.restore(data[0]) + self._tcfg.restore(data[1]) + self._ucfg.restore(data[2]) + def setconfig(self, section, name, value, overlay=True): if overlay: self._ocfg.set(section, name, value)