[PATCH 1 of 8 phases] config: have a way to backup and restore value in config

pierre-yves.david at logilab.fr pierre-yves.david at logilab.fr
Tue Jan 17 11:35:34 CST 2012


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at ens-lyon.org>
# Date 1326770637 -3600
# Node ID bea078f4023a0d0e2483ed2c6f87b52cb10f4822
# Parent  476a981fdf341f5bedbd958ca6c8e930fe35b5f9
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.

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
@@ -7,10 +7,13 @@
 
 from i18n import _
 import error, util
 import re, os, errno
 
+# a unique object to serve as "no data marker"
+_nodata = object()
+
 class sortdict(dict):
     'a simple sorted dictionary'
     def __init__(self, data=None):
         self._list = []
         if data:
@@ -59,10 +62,19 @@ class config(object):
                 self._data[s] = sortdict()
             self._data[s].update(src._data[s])
         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.
+        """
+        value = self.get(section, item, _nodata)
+        source = self.source(section, item)
+        return (section, item, value, source)
+
     def source(self, section, item):
         return self._source.get((section, item), "")
     def sections(self):
         return sorted(self._data.keys())
     def items(self, section):
@@ -71,10 +83,23 @@ class config(object):
         if section not in self:
             self._data[section] = sortdict()
         self._data[section][item] = value
         self._source[(section, item)] = source
 
+    def restore(self, bckdata):
+        """restore data returned by self.backup"""
+        section, item, value, source = bckdata
+        if value is _nodata:
+            # no original date remove any value
+            if section in self._data:
+                self._data[section][item]
+            self._source.pop((section, item), None)
+        else:
+            # restore old data
+            self._data[section][item] = value
+            self._source[(section, item)] = source
+
     def parse(self, src, data, sections=None, remap=None, include=None):
         sectionre = re.compile(r'\[([^\[]+)\]')
         itemre = re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)')
         contre = re.compile(r'\s+(\S|\S.*\S)\s*$')
         emptyre = re.compile(r'(;|#|\s*$)')
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -140,10 +140,19 @@ class ui(object):
         if section in (None, 'trusted'):
             # update trust information
             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, bckdata):
+        self._ocfg.restore(bckdata[0])
+        self._tcfg.restore(bckdata[1])
+        self._ucfg.restore(bckdata[2])
+
     def setconfig(self, section, name, value, overlay=True):
         if overlay:
             self._ocfg.set(section, name, value)
         self._tcfg.set(section, name, value)
         self._ucfg.set(section, name, value)


More information about the Mercurial-devel mailing list