Mercurial > hg
comparison mercurial/config.py @ 15919:69e792cf7851
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.
author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
---|---|
date | Wed, 18 Jan 2012 16:52:26 +0100 |
parents | 5fb3cb7266e5 |
children | f350021ee32e |
comparison
equal
deleted
inserted
replaced
15918:4f9853e7f690 | 15919:69e792cf7851 |
---|---|
59 self._data[s] = sortdict() | 59 self._data[s] = sortdict() |
60 self._data[s].update(src._data[s]) | 60 self._data[s].update(src._data[s]) |
61 self._source.update(src._source) | 61 self._source.update(src._source) |
62 def get(self, section, item, default=None): | 62 def get(self, section, item, default=None): |
63 return self._data.get(section, {}).get(item, default) | 63 return self._data.get(section, {}).get(item, default) |
64 | |
65 def backup(self, section, item): | |
66 """return a tuple allowing restore to reinstall a previous valuesi | |
67 | |
68 The main reason we need it is because it handle the "no data" case. | |
69 """ | |
70 try: | |
71 value = self._data[section][item] | |
72 source = self.source(section, item) | |
73 return (section, item, value, source) | |
74 except KeyError: | |
75 return (section, item) | |
76 | |
64 def source(self, section, item): | 77 def source(self, section, item): |
65 return self._source.get((section, item), "") | 78 return self._source.get((section, item), "") |
66 def sections(self): | 79 def sections(self): |
67 return sorted(self._data.keys()) | 80 return sorted(self._data.keys()) |
68 def items(self, section): | 81 def items(self, section): |
70 def set(self, section, item, value, source=""): | 83 def set(self, section, item, value, source=""): |
71 if section not in self: | 84 if section not in self: |
72 self._data[section] = sortdict() | 85 self._data[section] = sortdict() |
73 self._data[section][item] = value | 86 self._data[section][item] = value |
74 self._source[(section, item)] = source | 87 self._source[(section, item)] = source |
88 | |
89 def restore(self, data): | |
90 """restore data returned by self.backup""" | |
91 if len(data) == 4: | |
92 # restore old data | |
93 section, item, value, source = data | |
94 self._data[section][item] = value | |
95 self._source[(section, item)] = source | |
96 else: | |
97 # no data before, remove everything | |
98 section, item = data | |
99 if section in self._data: | |
100 del self._data[section][item] | |
101 self._source.pop((section, item), None) | |
75 | 102 |
76 def parse(self, src, data, sections=None, remap=None, include=None): | 103 def parse(self, src, data, sections=None, remap=None, include=None): |
77 sectionre = re.compile(r'\[([^\[]+)\]') | 104 sectionre = re.compile(r'\[([^\[]+)\]') |
78 itemre = re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') | 105 itemre = re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') |
79 contre = re.compile(r'\s+(\S|\S.*\S)\s*$') | 106 contre = re.compile(r'\s+(\S|\S.*\S)\s*$') |