40 def __getitem__(self, section): |
40 def __getitem__(self, section): |
41 return self._data.get(section, {}) |
41 return self._data.get(section, {}) |
42 def __iter__(self): |
42 def __iter__(self): |
43 for d in self.sections(): |
43 for d in self.sections(): |
44 yield d |
44 yield d |
45 def update(self, src, sections=None): |
45 def update(self, src): |
46 if not sections: |
46 for s in src: |
47 sections = src.sections() |
|
48 for s in sections: |
|
49 if s not in src: |
|
50 continue |
|
51 if s not in self: |
47 if s not in self: |
52 self._data[s] = sortdict() |
48 self._data[s] = sortdict() |
53 for k in src._data[s]: |
49 self._data[s].update(src._data[s]) |
54 self._data[s][k] = src._data[s][k] |
50 self._source.update(src._source) |
55 self._source[(s, k)] = src._source[(s, k)] |
|
56 def get(self, section, item, default=None): |
51 def get(self, section, item, default=None): |
57 return self._data.get(section, {}).get(item, default) |
52 return self._data.get(section, {}).get(item, default) |
58 def getsource(self, section, item): |
53 def getsource(self, section, item): |
59 return self._source.get((section, item), "") |
54 return self._source.get((section, item), "") |
60 def sections(self): |
55 def sections(self): |
65 if section not in self: |
60 if section not in self: |
66 self._data[section] = sortdict() |
61 self._data[section] = sortdict() |
67 self._data[section][item] = value |
62 self._data[section][item] = value |
68 self._source[(section, item)] = source |
63 self._source[(section, item)] = source |
69 |
64 |
70 def read(self, path, fp=None): |
65 def read(self, path, fp=None, sections=None): |
71 sectionre = re.compile(r'\[([^\[]+)\]') |
66 sectionre = re.compile(r'\[([^\[]+)\]') |
72 itemre = re.compile(r'([^=\s]+)\s*=\s*(.*\S|)') |
67 itemre = re.compile(r'([^=\s]+)\s*=\s*(.*\S|)') |
73 contre = re.compile(r'\s+(\S.*\S)') |
68 contre = re.compile(r'\s+(\S.*\S)') |
74 emptyre = re.compile(r'(;|#|\s*$)') |
69 emptyre = re.compile(r'(;|#|\s*$)') |
75 unsetre = re.compile(r'%unset\s+(\S+)') |
70 unsetre = re.compile(r'%unset\s+(\S+)') |
85 for l in fp: |
80 for l in fp: |
86 line += 1 |
81 line += 1 |
87 if cont: |
82 if cont: |
88 m = contre.match(l) |
83 m = contre.match(l) |
89 if m: |
84 if m: |
|
85 if sections and section not in sections: |
|
86 continue |
90 v = self.get(section, item) + "\n" + m.group(1) |
87 v = self.get(section, item) + "\n" + m.group(1) |
91 self.set(section, item, v, "%s:%d" % (path, line)) |
88 self.set(section, item, v, "%s:%d" % (path, line)) |
92 continue |
89 continue |
93 item = None |
90 item = None |
94 m = includere.match(l) |
91 m = includere.match(l) |
108 self._data[section] = sortdict() |
105 self._data[section] = sortdict() |
109 continue |
106 continue |
110 m = itemre.match(l) |
107 m = itemre.match(l) |
111 if m: |
108 if m: |
112 item = m.group(1) |
109 item = m.group(1) |
|
110 cont = 1 |
|
111 if sections and section not in sections: |
|
112 continue |
113 self.set(section, item, m.group(2), "%s:%d" % (path, line)) |
113 self.set(section, item, m.group(2), "%s:%d" % (path, line)) |
114 cont = 1 |
|
115 continue |
114 continue |
116 m = unsetre.match(l) |
115 m = unsetre.match(l) |
117 if m: |
116 if m: |
118 name = m.group(1) |
117 name = m.group(1) |
|
118 if sections and section not in sections: |
|
119 continue |
119 if self.get(section, name) != None: |
120 if self.get(section, name) != None: |
120 del self._data[section][name] |
121 del self._data[section][name] |
121 continue |
122 continue |
122 |
123 |
123 raise error.ConfigError(_('config error at %s:%d: \'%s\'') |
124 raise error.ConfigError(_('config error at %s:%d: \'%s\'') |