##// END OF EJS Templates
config.sortdict: override clear method...
Sune Foldager -
r14696:5fb3cb72 stable
parent child Browse files
Show More
@@ -1,146 +1,149
1 1 # config.py - configuration parsing for Mercurial
2 2 #
3 3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from i18n import _
9 9 import error, util
10 10 import re, os, errno
11 11
12 12 class sortdict(dict):
13 13 'a simple sorted dictionary'
14 14 def __init__(self, data=None):
15 15 self._list = []
16 16 if data:
17 17 self.update(data)
18 18 def copy(self):
19 19 return sortdict(self)
20 20 def __setitem__(self, key, val):
21 21 if key in self:
22 22 self._list.remove(key)
23 23 self._list.append(key)
24 24 dict.__setitem__(self, key, val)
25 25 def __iter__(self):
26 26 return self._list.__iter__()
27 27 def update(self, src):
28 28 for k in src:
29 29 self[k] = src[k]
30 def clear(self):
31 dict.clear(self)
32 self._list = []
30 33 def items(self):
31 34 return [(k, self[k]) for k in self._list]
32 35 def __delitem__(self, key):
33 36 dict.__delitem__(self, key)
34 37 self._list.remove(key)
35 38
36 39 class config(object):
37 40 def __init__(self, data=None):
38 41 self._data = {}
39 42 self._source = {}
40 43 if data:
41 44 for k in data._data:
42 45 self._data[k] = data[k].copy()
43 46 self._source = data._source.copy()
44 47 def copy(self):
45 48 return config(self)
46 49 def __contains__(self, section):
47 50 return section in self._data
48 51 def __getitem__(self, section):
49 52 return self._data.get(section, {})
50 53 def __iter__(self):
51 54 for d in self.sections():
52 55 yield d
53 56 def update(self, src):
54 57 for s in src:
55 58 if s not in self:
56 59 self._data[s] = sortdict()
57 60 self._data[s].update(src._data[s])
58 61 self._source.update(src._source)
59 62 def get(self, section, item, default=None):
60 63 return self._data.get(section, {}).get(item, default)
61 64 def source(self, section, item):
62 65 return self._source.get((section, item), "")
63 66 def sections(self):
64 67 return sorted(self._data.keys())
65 68 def items(self, section):
66 69 return self._data.get(section, {}).items()
67 70 def set(self, section, item, value, source=""):
68 71 if section not in self:
69 72 self._data[section] = sortdict()
70 73 self._data[section][item] = value
71 74 self._source[(section, item)] = source
72 75
73 76 def parse(self, src, data, sections=None, remap=None, include=None):
74 77 sectionre = re.compile(r'\[([^\[]+)\]')
75 78 itemre = re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)')
76 79 contre = re.compile(r'\s+(\S|\S.*\S)\s*$')
77 80 emptyre = re.compile(r'(;|#|\s*$)')
78 81 commentre = re.compile(r'(;|#)')
79 82 unsetre = re.compile(r'%unset\s+(\S+)')
80 83 includere = re.compile(r'%include\s+(\S|\S.*\S)\s*$')
81 84 section = ""
82 85 item = None
83 86 line = 0
84 87 cont = False
85 88
86 89 for l in data.splitlines(True):
87 90 line += 1
88 91 if cont:
89 92 if commentre.match(l):
90 93 continue
91 94 m = contre.match(l)
92 95 if m:
93 96 if sections and section not in sections:
94 97 continue
95 98 v = self.get(section, item) + "\n" + m.group(1)
96 99 self.set(section, item, v, "%s:%d" % (src, line))
97 100 continue
98 101 item = None
99 102 cont = False
100 103 m = includere.match(l)
101 104 if m:
102 105 inc = util.expandpath(m.group(1))
103 106 base = os.path.dirname(src)
104 107 inc = os.path.normpath(os.path.join(base, inc))
105 108 if include:
106 109 try:
107 110 include(inc, remap=remap, sections=sections)
108 111 except IOError, inst:
109 112 if inst.errno != errno.ENOENT:
110 113 raise error.ParseError(_("cannot include %s (%s)")
111 114 % (inc, inst.strerror),
112 115 "%s:%s" % (src, line))
113 116 continue
114 117 if emptyre.match(l):
115 118 continue
116 119 m = sectionre.match(l)
117 120 if m:
118 121 section = m.group(1)
119 122 if remap:
120 123 section = remap.get(section, section)
121 124 if section not in self:
122 125 self._data[section] = sortdict()
123 126 continue
124 127 m = itemre.match(l)
125 128 if m:
126 129 item = m.group(1)
127 130 cont = True
128 131 if sections and section not in sections:
129 132 continue
130 133 self.set(section, item, m.group(2), "%s:%d" % (src, line))
131 134 continue
132 135 m = unsetre.match(l)
133 136 if m:
134 137 name = m.group(1)
135 138 if sections and section not in sections:
136 139 continue
137 140 if self.get(section, name) is not None:
138 141 del self._data[section][name]
139 142 continue
140 143
141 144 raise error.ParseError(l.rstrip(), ("%s:%s" % (src, line)))
142 145
143 146 def read(self, path, fp=None, sections=None, remap=None):
144 147 if not fp:
145 148 fp = util.posixfile(path)
146 149 self.parse(path, fp.read(), sections, remap, self.read)
General Comments 0
You need to be logged in to leave comments. Login now