config.py
192 lines
| 6.4 KiB
| text/x-python
|
PythonLexer
/ mercurial / config.py
|
r8229 | # config.py - configuration parsing for Mercurial | ||
# | ||||
# Copyright 2009 Matt Mackall <mpm@selenic.com> and others | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r8229 | |||
|
r8144 | from i18n import _ | ||
|
r11224 | import error, util | ||
|
r16944 | import os, errno | ||
|
r8144 | |||
|
r22383 | samplehgrcs = { | ||
'user': | ||||
"""# example user config (see "hg help config" for more info) | ||||
[ui] | ||||
# name and email, e.g. | ||||
# username = Jane Doe <jdoe@example.com> | ||||
username = | ||||
[extensions] | ||||
# uncomment these lines to enable some popular extensions | ||||
# (see "hg help extensions" for more info) | ||||
# | ||||
# pager = | ||||
# progress = | ||||
# color =""", | ||||
'local': | ||||
"""# example repository config (see "hg help config" for more info) | ||||
""", | ||||
'global': | ||||
"""# example system-wide hg config (see "hg help config" for more info) | ||||
|
r22384 | |||
[extensions] | ||||
# Uncomment these lines for some possible extensions | ||||
# (see "hg help extensions" for more info) | ||||
# | ||||
# blackbox = | ||||
# progress = | ||||
# color = | ||||
# pager =""", | ||||
|
r22383 | } | ||
|
r8186 | class config(object): | ||
|
r8144 | def __init__(self, data=None): | ||
self._data = {} | ||||
|
r8185 | self._source = {} | ||
|
r19087 | self._unset = [] | ||
|
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 | ||||
|
r8193 | def update(self, src): | ||
|
r19087 | for s, n in src._unset: | ||
if s in self and n in self._data[s]: | ||||
del self._data[s][n] | ||||
del self._source[(s, n)] | ||||
|
r8193 | for s in src: | ||
|
r8144 | if s not in self: | ||
|
r21813 | self._data[s] = util.sortdict() | ||
|
r8193 | self._data[s].update(src._data[s]) | ||
self._source.update(src._source) | ||||
|
r8144 | def get(self, section, item, default=None): | ||
|
r8185 | return self._data.get(section, {}).get(item, default) | ||
|
r15919 | |||
def backup(self, section, item): | ||||
|
r17527 | """return a tuple allowing restore to reinstall a previous value | ||
|
r15919 | |||
|
r17530 | The main reason we need it is because it handles the "no data" case. | ||
|
r15919 | """ | ||
try: | ||||
value = self._data[section][item] | ||||
source = self.source(section, item) | ||||
return (section, item, value, source) | ||||
except KeyError: | ||||
return (section, item) | ||||
|
r8198 | def source(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: | ||||
|
r21813 | self._data[section] = util.sortdict() | ||
|
r8185 | self._data[section][item] = value | ||
|
r20789 | if source: | ||
self._source[(section, item)] = source | ||||
|
r8144 | |||
|
r15919 | def restore(self, data): | ||
"""restore data returned by self.backup""" | ||||
if len(data) == 4: | ||||
# restore old data | ||||
section, item, value, source = data | ||||
self._data[section][item] = value | ||||
self._source[(section, item)] = source | ||||
else: | ||||
# no data before, remove everything | ||||
section, item = data | ||||
if section in self._data: | ||||
|
r22037 | self._data[section].pop(item, None) | ||
|
r15919 | self._source.pop((section, item), None) | ||
|
r8265 | def parse(self, src, data, sections=None, remap=None, include=None): | ||
|
r21910 | sectionre = util.re.compile(r'\[([^\[]+)\]') | ||
itemre = util.re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') | ||||
contre = util.re.compile(r'\s+(\S|\S.*\S)\s*$') | ||||
emptyre = util.re.compile(r'(;|#|\s*$)') | ||||
commentre = util.re.compile(r'(;|#)') | ||||
unsetre = util.re.compile(r'%unset\s+(\S+)') | ||||
includere = util.re.compile(r'%include\s+(\S|\S.*\S)\s*$') | ||||
|
r8144 | section = "" | ||
item = None | ||||
line = 0 | ||||
r9339 | cont = False | |||
|
r8180 | |||
|
r9136 | for l in data.splitlines(True): | ||
|
r8144 | line += 1 | ||
|
r16348 | if line == 1 and l.startswith('\xef\xbb\xbf'): | ||
# Someone set us up the BOM | ||||
l = l[3:] | ||||
|
r8144 | if cont: | ||
|
r14642 | if commentre.match(l): | ||
continue | ||||
|
r8144 | m = contre.match(l) | ||
if m: | ||||
|
r8193 | if sections and section not in sections: | ||
continue | ||||
|
r8144 | v = self.get(section, item) + "\n" + m.group(1) | ||
|
r8265 | self.set(section, item, v, "%s:%d" % (src, line)) | ||
|
r8144 | continue | ||
item = None | ||||
|
r9469 | cont = False | ||
|
r8183 | m = includere.match(l) | ||
if m: | ||||
|
r11224 | inc = util.expandpath(m.group(1)) | ||
|
r8265 | base = os.path.dirname(src) | ||
|
r8183 | inc = os.path.normpath(os.path.join(base, inc)) | ||
|
r8265 | if include: | ||
|
r10042 | try: | ||
include(inc, remap=remap, sections=sections) | ||||
except IOError, inst: | ||||
|
r14486 | if inst.errno != errno.ENOENT: | ||
raise error.ParseError(_("cannot include %s (%s)") | ||||
% (inc, inst.strerror), | ||||
"%s:%s" % (src, line)) | ||||
|
r8183 | continue | ||
|
r8144 | if emptyre.match(l): | ||
continue | ||||
m = sectionre.match(l) | ||||
if m: | ||||
section = m.group(1) | ||||
|
r8298 | if remap: | ||
section = remap.get(section, section) | ||||
|
r8144 | if section not in self: | ||
|
r21813 | self._data[section] = util.sortdict() | ||
|
r8144 | continue | ||
m = itemre.match(l) | ||||
if m: | ||||
item = m.group(1) | ||||
r9339 | cont = True | |||
|
r8193 | if sections and section not in sections: | ||
continue | ||||
|
r8265 | self.set(section, item, m.group(2), "%s:%d" % (src, line)) | ||
|
r8144 | continue | ||
|
r8184 | m = unsetre.match(l) | ||
if m: | ||||
name = m.group(1) | ||||
|
r8193 | if sections and section not in sections: | ||
continue | ||||
|
r13031 | if self.get(section, name) is not None: | ||
|
r8184 | del self._data[section][name] | ||
|
r19087 | self._unset.append((section, name)) | ||
|
r8184 | continue | ||
|
r11288 | raise error.ParseError(l.rstrip(), ("%s:%s" % (src, line))) | ||
|
r8265 | |||
def read(self, path, fp=None, sections=None, remap=None): | ||||
if not fp: | ||||
|
r13664 | fp = util.posixfile(path) | ||
|
r8265 | self.parse(path, fp.read(), sections, remap, self.read) | ||