Mercurial > public > mercurial-scm > hg
comparison mercurial/config.py @ 21813:c2262004c2e2
config: move config.sortdict class into util
This makes it more natural to use the sortdict class from outside config.py.
author | Angel Ezquerra <angel.ezquerra@gmail.com> |
---|---|
date | Sun, 23 Feb 2014 01:56:31 +0100 |
parents | d19c9bdbbf35 |
children | fc04fdb2b349 |
comparison
equal
deleted
inserted
replaced
21812:73e4a02e6d23 | 21813:c2262004c2e2 |
---|---|
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
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 | |
12 class sortdict(dict): | |
13 'a simple sorted dictionary' | |
14 def __init__(self, data=None): | |
15 self._list = [] | |
16 if data: | |
17 self.update(data) | |
18 def copy(self): | |
19 return sortdict(self) | |
20 def __setitem__(self, key, val): | |
21 if key in self: | |
22 self._list.remove(key) | |
23 self._list.append(key) | |
24 dict.__setitem__(self, key, val) | |
25 def __iter__(self): | |
26 return self._list.__iter__() | |
27 def update(self, src): | |
28 for k in src: | |
29 self[k] = src[k] | |
30 def clear(self): | |
31 dict.clear(self) | |
32 self._list = [] | |
33 def items(self): | |
34 return [(k, self[k]) for k in self._list] | |
35 def __delitem__(self, key): | |
36 dict.__delitem__(self, key) | |
37 self._list.remove(key) | |
38 def keys(self): | |
39 return self._list | |
40 def iterkeys(self): | |
41 return self._list.__iter__() | |
42 | 11 |
43 class config(object): | 12 class config(object): |
44 def __init__(self, data=None): | 13 def __init__(self, data=None): |
45 self._data = {} | 14 self._data = {} |
46 self._source = {} | 15 self._source = {} |
63 if s in self and n in self._data[s]: | 32 if s in self and n in self._data[s]: |
64 del self._data[s][n] | 33 del self._data[s][n] |
65 del self._source[(s, n)] | 34 del self._source[(s, n)] |
66 for s in src: | 35 for s in src: |
67 if s not in self: | 36 if s not in self: |
68 self._data[s] = sortdict() | 37 self._data[s] = util.sortdict() |
69 self._data[s].update(src._data[s]) | 38 self._data[s].update(src._data[s]) |
70 self._source.update(src._source) | 39 self._source.update(src._source) |
71 def get(self, section, item, default=None): | 40 def get(self, section, item, default=None): |
72 return self._data.get(section, {}).get(item, default) | 41 return self._data.get(section, {}).get(item, default) |
73 | 42 |
89 return sorted(self._data.keys()) | 58 return sorted(self._data.keys()) |
90 def items(self, section): | 59 def items(self, section): |
91 return self._data.get(section, {}).items() | 60 return self._data.get(section, {}).items() |
92 def set(self, section, item, value, source=""): | 61 def set(self, section, item, value, source=""): |
93 if section not in self: | 62 if section not in self: |
94 self._data[section] = sortdict() | 63 self._data[section] = util.sortdict() |
95 self._data[section][item] = value | 64 self._data[section][item] = value |
96 if source: | 65 if source: |
97 self._source[(section, item)] = source | 66 self._source[(section, item)] = source |
98 | 67 |
99 def restore(self, data): | 68 def restore(self, data): |
160 if m: | 129 if m: |
161 section = m.group(1) | 130 section = m.group(1) |
162 if remap: | 131 if remap: |
163 section = remap.get(section, section) | 132 section = remap.get(section, section) |
164 if section not in self: | 133 if section not in self: |
165 self._data[section] = sortdict() | 134 self._data[section] = util.sortdict() |
166 continue | 135 continue |
167 m = itemre.match(l) | 136 m = itemre.match(l) |
168 if m: | 137 if m: |
169 item = m.group(1) | 138 item = m.group(1) |
170 cont = True | 139 cont = True |