##// END OF EJS Templates
config: add %unset name support
Matt Mackall -
r8184:9189afe1 default
parent child Browse files
Show More
@@ -1,106 +1,117
1 from i18n import _
1 from i18n import _
2 import re, error, os
2 import re, error, os
3
3
4 class sortdict(dict):
4 class sortdict(dict):
5 'a simple append-only sorted dictionary'
5 'a simple sorted dictionary'
6 def __init__(self, data=None):
6 def __init__(self, data=None):
7 self._list = []
7 self._list = []
8 if data:
8 if data:
9 if hasattr(data, '_list'):
9 if hasattr(data, '_list'):
10 self._list = list(data._list)
10 self._list = list(data._list)
11 self.update(data)
11 self.update(data)
12 def copy(self):
12 def copy(self):
13 return sortdict(self)
13 return sortdict(self)
14 def __setitem__(self, key, val):
14 def __setitem__(self, key, val):
15 if key in self:
15 if key in self:
16 self._list.remove(key)
16 self._list.remove(key)
17 self._list.append(key)
17 self._list.append(key)
18 dict.__setitem__(self, key, val)
18 dict.__setitem__(self, key, val)
19 def __iter__(self):
19 def __iter__(self):
20 return self._list.__iter__()
20 return self._list.__iter__()
21 def update(self, src):
21 def update(self, src):
22 for k in src:
22 for k in src:
23 self[k] = src[k]
23 self[k] = src[k]
24 def items(self):
24 def items(self):
25 return [(k,self[k]) for k in self._list]
25 return [(k,self[k]) for k in self._list]
26 def __delitem__(self, key):
27 dict.__delitem__(self, key)
28 self._list.remove(key)
26
29
27 class config:
30 class config:
28 def __init__(self, data=None):
31 def __init__(self, data=None):
29 self._data = {}
32 self._data = {}
30 if data:
33 if data:
31 for k in data._data:
34 for k in data._data:
32 self._data[k] = data[k].copy()
35 self._data[k] = data[k].copy()
33 def copy(self):
36 def copy(self):
34 return config(self)
37 return config(self)
35 def __contains__(self, section):
38 def __contains__(self, section):
36 return section in self._data
39 return section in self._data
37 def update(self, src, sections=None):
40 def update(self, src, sections=None):
38 if not sections:
41 if not sections:
39 sections = src.sections()
42 sections = src.sections()
40 for s in sections:
43 for s in sections:
41 if s not in src:
44 if s not in src:
42 continue
45 continue
43 if s not in self:
46 if s not in self:
44 self._data[s] = sortdict()
47 self._data[s] = sortdict()
45 for k in src._data[s]:
48 for k in src._data[s]:
46 self._data[s][k] = src._data[s][k]
49 self._data[s][k] = src._data[s][k]
47 def get(self, section, item, default=None):
50 def get(self, section, item, default=None):
48 return self._data.get(section, {}).get(item, (default, ""))[0]
51 return self._data.get(section, {}).get(item, (default, ""))[0]
49 def getsource(self, section, item):
52 def getsource(self, section, item):
50 return self._data.get(section, {}).get(item, (None, ""))[1]
53 return self._data.get(section, {}).get(item, (None, ""))[1]
51 def sections(self):
54 def sections(self):
52 return sorted(self._data.keys())
55 return sorted(self._data.keys())
53 def items(self, section):
56 def items(self, section):
54 return [(k, v[0]) for k,v in self._data.get(section, {}).items()]
57 return [(k, v[0]) for k,v in self._data.get(section, {}).items()]
55 def set(self, section, item, value, source=""):
58 def set(self, section, item, value, source=""):
56 if section not in self:
59 if section not in self:
57 self._data[section] = sortdict()
60 self._data[section] = sortdict()
58 self._data[section][item] = (value, source)
61 self._data[section][item] = (value, source)
59
62
60 def read(self, path, fp=None):
63 def read(self, path, fp=None):
61 sectionre = re.compile(r'\[([^\[]+)\]')
64 sectionre = re.compile(r'\[([^\[]+)\]')
62 itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)')
65 itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)')
63 contre = re.compile(r'\s+(\S.*)')
66 contre = re.compile(r'\s+(\S.*)')
64 emptyre = re.compile(r'(;|#|\s*$)')
67 emptyre = re.compile(r'(;|#|\s*$)')
68 unsetre = re.compile(r'%unset\s+(\S.*)')
65 includere = re.compile(r'%include\s+(\S.*)')
69 includere = re.compile(r'%include\s+(\S.*)')
66 section = ""
70 section = ""
67 item = None
71 item = None
68 line = 0
72 line = 0
69 cont = 0
73 cont = 0
70
74
71 if not fp:
75 if not fp:
72 fp = open(path)
76 fp = open(path)
73
77
74 for l in fp:
78 for l in fp:
75 line += 1
79 line += 1
76 if cont:
80 if cont:
77 m = contre.match(l)
81 m = contre.match(l)
78 if m:
82 if m:
79 v = self.get(section, item) + "\n" + m.group(1)
83 v = self.get(section, item) + "\n" + m.group(1)
80 self.set(section, item, v, "%s:%d" % (path, line))
84 self.set(section, item, v, "%s:%d" % (path, line))
81 continue
85 continue
82 item = None
86 item = None
83 m = includere.match(l)
87 m = includere.match(l)
84 if m:
88 if m:
85 inc = m.group(1)
89 inc = m.group(1)
86 base = os.path.dirname(path)
90 base = os.path.dirname(path)
87 inc = os.path.normpath(os.path.join(base, inc))
91 inc = os.path.normpath(os.path.join(base, inc))
88 incfp = open(inc)
92 incfp = open(inc)
89 self.read(inc, incfp)
93 self.read(inc, incfp)
90 continue
94 continue
91 if emptyre.match(l):
95 if emptyre.match(l):
92 continue
96 continue
93 m = sectionre.match(l)
97 m = sectionre.match(l)
94 if m:
98 if m:
95 section = m.group(1)
99 section = m.group(1)
96 if section not in self:
100 if section not in self:
97 self._data[section] = sortdict()
101 self._data[section] = sortdict()
98 continue
102 continue
99 m = itemre.match(l)
103 m = itemre.match(l)
100 if m:
104 if m:
101 item = m.group(1)
105 item = m.group(1)
102 self.set(section, item, m.group(2), "%s:%d" % (path, line))
106 self.set(section, item, m.group(2), "%s:%d" % (path, line))
103 cont = 1
107 cont = 1
104 continue
108 continue
109 m = unsetre.match(l)
110 if m:
111 name = m.group(1)
112 if self.get(section, name) != None:
113 del self._data[section][name]
114 continue
115
105 raise error.ConfigError(_('config error at %s:%d: \'%s\'')
116 raise error.ConfigError(_('config error at %s:%d: \'%s\'')
106 % (path, line, l.rstrip()))
117 % (path, line, l.rstrip()))
General Comments 0
You need to be logged in to leave comments. Login now