config.py
124 lines
| 4.0 KiB
| text/x-python
|
PythonLexer
/ mercurial / config.py
|
r8144 | from i18n import _ | ||
|
r8183 | import re, error, os | ||
|
r8144 | |||
class sortdict(dict): | ||||
|
r8184 | 'a simple sorted dictionary' | ||
|
r8144 | def __init__(self, data=None): | ||
self._list = [] | ||||
if data: | ||||
self.update(data) | ||||
def copy(self): | ||||
return sortdict(self) | ||||
def __setitem__(self, key, val): | ||||
if key in self: | ||||
self._list.remove(key) | ||||
self._list.append(key) | ||||
dict.__setitem__(self, key, val) | ||||
def __iter__(self): | ||||
return self._list.__iter__() | ||||
def update(self, src): | ||||
for k in src: | ||||
self[k] = src[k] | ||||
def items(self): | ||||
return [(k,self[k]) for k in self._list] | ||||
|
r8184 | def __delitem__(self, key): | ||
dict.__delitem__(self, key) | ||||
self._list.remove(key) | ||||
|
r8144 | |||
|
r8186 | class config(object): | ||
|
r8144 | def __init__(self, data=None): | ||
self._data = {} | ||||
|
r8185 | self._source = {} | ||
|
r8144 | if data: | ||
for k in data._data: | ||||
self._data[k] = data[k].copy() | ||||
|
r8185 | self._source = data._source.copy() | ||
|
r8144 | def copy(self): | ||
return config(self) | ||||
def __contains__(self, section): | ||||
return section in self._data | ||||
|
r8186 | def __getitem__(self, section): | ||
return self._data.get(section, {}) | ||||
def __iter__(self): | ||||
for d in self.sections(): | ||||
yield d | ||||
|
r8144 | def update(self, src, sections=None): | ||
if not sections: | ||||
sections = src.sections() | ||||
for s in sections: | ||||
if s not in src: | ||||
continue | ||||
if s not in self: | ||||
self._data[s] = sortdict() | ||||
for k in src._data[s]: | ||||
self._data[s][k] = src._data[s][k] | ||||
|
r8185 | self._source[(s, k)] = src._source[(s, k)] | ||
|
r8144 | def get(self, section, item, default=None): | ||
|
r8185 | return self._data.get(section, {}).get(item, default) | ||
|
r8144 | def getsource(self, section, item): | ||
|
r8185 | return self._source.get((section, item), "") | ||
|
r8144 | def sections(self): | ||
return sorted(self._data.keys()) | ||||
def items(self, section): | ||||
|
r8185 | return self._data.get(section, {}).items() | ||
|
r8144 | def set(self, section, item, value, source=""): | ||
if section not in self: | ||||
self._data[section] = sortdict() | ||||
|
r8185 | self._data[section][item] = value | ||
self._source[(section, item)] = source | ||||
|
r8144 | |||
|
r8180 | def read(self, path, fp=None): | ||
|
r8144 | sectionre = re.compile(r'\[([^\[]+)\]') | ||
itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)') | ||||
contre = re.compile(r'\s+(\S.*)') | ||||
emptyre = re.compile(r'(;|#|\s*$)') | ||||
|
r8184 | unsetre = re.compile(r'%unset\s+(\S.*)') | ||
|
r8183 | includere = re.compile(r'%include\s+(\S.*)') | ||
|
r8144 | section = "" | ||
item = None | ||||
line = 0 | ||||
cont = 0 | ||||
|
r8180 | |||
if not fp: | ||||
fp = open(path) | ||||
|
r8144 | for l in fp: | ||
line += 1 | ||||
if cont: | ||||
m = contre.match(l) | ||||
if m: | ||||
v = self.get(section, item) + "\n" + m.group(1) | ||||
self.set(section, item, v, "%s:%d" % (path, line)) | ||||
continue | ||||
item = None | ||||
|
r8183 | m = includere.match(l) | ||
if m: | ||||
inc = m.group(1) | ||||
base = os.path.dirname(path) | ||||
inc = os.path.normpath(os.path.join(base, inc)) | ||||
incfp = open(inc) | ||||
self.read(inc, incfp) | ||||
continue | ||||
|
r8144 | if emptyre.match(l): | ||
continue | ||||
m = sectionre.match(l) | ||||
if m: | ||||
section = m.group(1) | ||||
if section not in self: | ||||
self._data[section] = sortdict() | ||||
continue | ||||
m = itemre.match(l) | ||||
if m: | ||||
item = m.group(1) | ||||
self.set(section, item, m.group(2), "%s:%d" % (path, line)) | ||||
cont = 1 | ||||
continue | ||||
|
r8184 | m = unsetre.match(l) | ||
if m: | ||||
name = m.group(1) | ||||
if self.get(section, name) != None: | ||||
del self._data[section][name] | ||||
continue | ||||
|
r8144 | raise error.ConfigError(_('config error at %s:%d: \'%s\'') | ||
% (path, line, l.rstrip())) | ||||