Show More
@@ -27,9 +27,6 b' class config(object):' | |||
|
27 | 27 | if data: |
|
28 | 28 | for k in data._data: |
|
29 | 29 | self._data[k] = data[k].copy() |
|
30 | self._source = data._source.copy() | |
|
31 | else: | |
|
32 | self._source = util.cowdict() | |
|
33 | 30 | |
|
34 | 31 | def copy(self): |
|
35 | 32 | return config(self) |
@@ -48,13 +45,11 b' class config(object):' | |||
|
48 | 45 | yield d |
|
49 | 46 | |
|
50 | 47 | def update(self, src): |
|
51 | self._source = self._source.preparewrite() | |
|
52 | 48 | for s, n in src._unset: |
|
53 | 49 | ds = self._data.get(s, None) |
|
54 | 50 | if ds is not None and n in ds: |
|
55 | 51 | self._data[s] = ds.preparewrite() |
|
56 | 52 | del self._data[s][n] |
|
57 | del self._source[(s, n)] | |
|
58 | 53 | for s in src: |
|
59 | 54 | ds = self._data.get(s, None) |
|
60 | 55 | if ds: |
@@ -62,31 +57,40 b' class config(object):' | |||
|
62 | 57 | else: |
|
63 | 58 | self._data[s] = util.cowsortdict() |
|
64 | 59 | self._data[s].update(src._data[s]) |
|
65 | self._source.update(src._source) | |
|
60 | ||
|
61 | def _get(self, section, item): | |
|
62 | return self._data.get(section, {}).get(item) | |
|
66 | 63 | |
|
67 | 64 | def get(self, section, item, default=None): |
|
68 |
re |
|
|
65 | result = self._get(section, item) | |
|
66 | if result is None: | |
|
67 | return default | |
|
68 | return result[0] | |
|
69 | 69 | |
|
70 |
def backup(self, section, |
|
|
70 | def backup(self, section, key): | |
|
71 | 71 | """return a tuple allowing restore to reinstall a previous value |
|
72 | 72 | |
|
73 | 73 | The main reason we need it is because it handles the "no data" case. |
|
74 | 74 | """ |
|
75 | 75 | try: |
|
76 |
|
|
|
77 | source = self.source(section, item) | |
|
78 | return (section, item, value, source) | |
|
76 | item = self._data[section][key] | |
|
79 | 77 | except KeyError: |
|
80 |
return (section, |
|
|
78 | return (section, key) | |
|
79 | else: | |
|
80 | return (section, key) + item | |
|
81 | 81 | |
|
82 | 82 | def source(self, section, item): |
|
83 |
re |
|
|
83 | result = self._get(section, item) | |
|
84 | if result is None: | |
|
85 | return b"" | |
|
86 | return result[1] | |
|
84 | 87 | |
|
85 | 88 | def sections(self): |
|
86 | 89 | return sorted(self._data.keys()) |
|
87 | 90 | |
|
88 | 91 | def items(self, section): |
|
89 |
|
|
|
92 | items = pycompat.iteritems(self._data.get(section, {})) | |
|
93 | return [(k, v) for (k, (v, s)) in items] | |
|
90 | 94 | |
|
91 | 95 | def set(self, section, item, value, source=b""): |
|
92 | 96 | if pycompat.ispy3: |
@@ -103,10 +107,7 b' class config(object):' | |||
|
103 | 107 | self._data[section] = util.cowsortdict() |
|
104 | 108 | else: |
|
105 | 109 | self._data[section] = self._data[section].preparewrite() |
|
106 | self._data[section][item] = value | |
|
107 | if source: | |
|
108 | self._source = self._source.preparewrite() | |
|
109 | self._source[(section, item)] = source | |
|
110 | self._data[section][item] = (value, source) | |
|
110 | 111 | |
|
111 | 112 | def alter(self, section, key, new_value): |
|
112 | 113 | """alter a value without altering its source or level |
@@ -120,19 +121,17 b' class config(object):' | |||
|
120 | 121 | |
|
121 | 122 | def restore(self, data): |
|
122 | 123 | """restore data returned by self.backup""" |
|
123 | self._source = self._source.preparewrite() | |
|
124 | if len(data) == 4: | |
|
124 | if len(data) != 2: | |
|
125 | 125 | # restore old data |
|
126 |
section, |
|
|
126 | section, key = data[:2] | |
|
127 | item = data[2:] | |
|
127 | 128 | self._data[section] = self._data[section].preparewrite() |
|
128 |
self._data[section][ |
|
|
129 | self._source[(section, item)] = source | |
|
129 | self._data[section][key] = item | |
|
130 | 130 | else: |
|
131 | 131 | # no data before, remove everything |
|
132 | 132 | section, item = data |
|
133 | 133 | if section in self._data: |
|
134 | 134 | self._data[section].pop(item, None) |
|
135 | self._source.pop((section, item), None) | |
|
136 | 135 | |
|
137 | 136 | def parse(self, src, data, sections=None, remap=None, include=None): |
|
138 | 137 | sectionre = util.re.compile(br'\[([^\[]+)\]') |
General Comments 0
You need to be logged in to leave comments.
Login now