##// END OF EJS Templates
config: add some helper methods
Matt Mackall -
r8186:6a0018cd default
parent child Browse files
Show More
@@ -1,121 +1,126
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 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):
26 def __delitem__(self, key):
27 dict.__delitem__(self, key)
27 dict.__delitem__(self, key)
28 self._list.remove(key)
28 self._list.remove(key)
29
29
30 class config:
30 class config(object):
31 def __init__(self, data=None):
31 def __init__(self, data=None):
32 self._data = {}
32 self._data = {}
33 self._source = {}
33 self._source = {}
34 if data:
34 if data:
35 for k in data._data:
35 for k in data._data:
36 self._data[k] = data[k].copy()
36 self._data[k] = data[k].copy()
37 self._source = data._source.copy()
37 self._source = data._source.copy()
38 def copy(self):
38 def copy(self):
39 return config(self)
39 return config(self)
40 def __contains__(self, section):
40 def __contains__(self, section):
41 return section in self._data
41 return section in self._data
42 def __getitem__(self, section):
43 return self._data.get(section, {})
44 def __iter__(self):
45 for d in self.sections():
46 yield d
42 def update(self, src, sections=None):
47 def update(self, src, sections=None):
43 if not sections:
48 if not sections:
44 sections = src.sections()
49 sections = src.sections()
45 for s in sections:
50 for s in sections:
46 if s not in src:
51 if s not in src:
47 continue
52 continue
48 if s not in self:
53 if s not in self:
49 self._data[s] = sortdict()
54 self._data[s] = sortdict()
50 for k in src._data[s]:
55 for k in src._data[s]:
51 self._data[s][k] = src._data[s][k]
56 self._data[s][k] = src._data[s][k]
52 self._source[(s, k)] = src._source[(s, k)]
57 self._source[(s, k)] = src._source[(s, k)]
53 def get(self, section, item, default=None):
58 def get(self, section, item, default=None):
54 return self._data.get(section, {}).get(item, default)
59 return self._data.get(section, {}).get(item, default)
55 def getsource(self, section, item):
60 def getsource(self, section, item):
56 return self._source.get((section, item), "")
61 return self._source.get((section, item), "")
57 def sections(self):
62 def sections(self):
58 return sorted(self._data.keys())
63 return sorted(self._data.keys())
59 def items(self, section):
64 def items(self, section):
60 return self._data.get(section, {}).items()
65 return self._data.get(section, {}).items()
61 def set(self, section, item, value, source=""):
66 def set(self, section, item, value, source=""):
62 if section not in self:
67 if section not in self:
63 self._data[section] = sortdict()
68 self._data[section] = sortdict()
64 self._data[section][item] = value
69 self._data[section][item] = value
65 self._source[(section, item)] = source
70 self._source[(section, item)] = source
66
71
67 def read(self, path, fp=None):
72 def read(self, path, fp=None):
68 sectionre = re.compile(r'\[([^\[]+)\]')
73 sectionre = re.compile(r'\[([^\[]+)\]')
69 itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)')
74 itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)')
70 contre = re.compile(r'\s+(\S.*)')
75 contre = re.compile(r'\s+(\S.*)')
71 emptyre = re.compile(r'(;|#|\s*$)')
76 emptyre = re.compile(r'(;|#|\s*$)')
72 unsetre = re.compile(r'%unset\s+(\S.*)')
77 unsetre = re.compile(r'%unset\s+(\S.*)')
73 includere = re.compile(r'%include\s+(\S.*)')
78 includere = re.compile(r'%include\s+(\S.*)')
74 section = ""
79 section = ""
75 item = None
80 item = None
76 line = 0
81 line = 0
77 cont = 0
82 cont = 0
78
83
79 if not fp:
84 if not fp:
80 fp = open(path)
85 fp = open(path)
81
86
82 for l in fp:
87 for l in fp:
83 line += 1
88 line += 1
84 if cont:
89 if cont:
85 m = contre.match(l)
90 m = contre.match(l)
86 if m:
91 if m:
87 v = self.get(section, item) + "\n" + m.group(1)
92 v = self.get(section, item) + "\n" + m.group(1)
88 self.set(section, item, v, "%s:%d" % (path, line))
93 self.set(section, item, v, "%s:%d" % (path, line))
89 continue
94 continue
90 item = None
95 item = None
91 m = includere.match(l)
96 m = includere.match(l)
92 if m:
97 if m:
93 inc = m.group(1)
98 inc = m.group(1)
94 base = os.path.dirname(path)
99 base = os.path.dirname(path)
95 inc = os.path.normpath(os.path.join(base, inc))
100 inc = os.path.normpath(os.path.join(base, inc))
96 incfp = open(inc)
101 incfp = open(inc)
97 self.read(inc, incfp)
102 self.read(inc, incfp)
98 continue
103 continue
99 if emptyre.match(l):
104 if emptyre.match(l):
100 continue
105 continue
101 m = sectionre.match(l)
106 m = sectionre.match(l)
102 if m:
107 if m:
103 section = m.group(1)
108 section = m.group(1)
104 if section not in self:
109 if section not in self:
105 self._data[section] = sortdict()
110 self._data[section] = sortdict()
106 continue
111 continue
107 m = itemre.match(l)
112 m = itemre.match(l)
108 if m:
113 if m:
109 item = m.group(1)
114 item = m.group(1)
110 self.set(section, item, m.group(2), "%s:%d" % (path, line))
115 self.set(section, item, m.group(2), "%s:%d" % (path, line))
111 cont = 1
116 cont = 1
112 continue
117 continue
113 m = unsetre.match(l)
118 m = unsetre.match(l)
114 if m:
119 if m:
115 name = m.group(1)
120 name = m.group(1)
116 if self.get(section, name) != None:
121 if self.get(section, name) != None:
117 del self._data[section][name]
122 del self._data[section][name]
118 continue
123 continue
119
124
120 raise error.ConfigError(_('config error at %s:%d: \'%s\'')
125 raise error.ConfigError(_('config error at %s:%d: \'%s\'')
121 % (path, line, l.rstrip()))
126 % (path, line, l.rstrip()))
General Comments 0
You need to be logged in to leave comments. Login now