config.py
350 lines
| 11.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 | |||
|
r25923 | from __future__ import absolute_import | ||
import errno | ||||
import os | ||||
from .i18n import _ | ||||
|
r43359 | from .pycompat import getattr | ||
|
r25923 | from . import ( | ||
|
r43570 | encoding, | ||
|
r25923 | error, | ||
|
r31306 | pycompat, | ||
|
r25923 | util, | ||
) | ||||
|
r8144 | |||
|
r43346 | |||
|
r8186 | class config(object): | ||
|
r45814 | def __init__(self, data=None): | ||
r47367 | self._current_source_level = 0 | |||
|
r8144 | self._data = {} | ||
|
r19087 | self._unset = [] | ||
|
r8144 | if data: | ||
for k in data._data: | ||||
self._data[k] = data[k].copy() | ||||
r47367 | self._current_source_level = data._current_source_level + 1 | |||
def new_source(self): | ||||
"""increment the source counter | ||||
This is used to define source priority when reading""" | ||||
self._current_source_level += 1 | ||||
|
r43346 | |||
|
r8144 | def copy(self): | ||
return config(self) | ||||
|
r43346 | |||
|
r8144 | def __contains__(self, section): | ||
return section in self._data | ||||
|
r43346 | |||
|
r27696 | def hasitem(self, section, item): | ||
return item in self._data.get(section, {}) | ||||
|
r43346 | |||
|
r8186 | def __getitem__(self, section): | ||
return self._data.get(section, {}) | ||||
|
r43346 | |||
|
r8186 | def __iter__(self): | ||
for d in self.sections(): | ||||
yield d | ||||
|
r43346 | |||
|
r8193 | def update(self, src): | ||
r47367 | current_level = self._current_source_level | |||
current_level += 1 | ||||
max_level = self._current_source_level | ||||
|
r19087 | for s, n in src._unset: | ||
|
r34353 | ds = self._data.get(s, None) | ||
if ds is not None and n in ds: | ||||
self._data[s] = ds.preparewrite() | ||||
|
r19087 | del self._data[s][n] | ||
|
r8193 | for s in src: | ||
|
r34353 | ds = self._data.get(s, None) | ||
if ds: | ||||
self._data[s] = ds.preparewrite() | ||||
else: | ||||
self._data[s] = util.cowsortdict() | ||||
r47367 | for k, v in src._data[s].items(): | |||
value, source, level = v | ||||
level += current_level | ||||
max_level = max(level, current_level) | ||||
self._data[s][k] = (value, source, level) | ||||
self._current_source_level = max_level | ||||
r47366 | ||||
def _get(self, section, item): | ||||
return self._data.get(section, {}).get(item) | ||||
|
r43346 | |||
|
r8144 | def get(self, section, item, default=None): | ||
r47366 | result = self._get(section, item) | |||
if result is None: | ||||
return default | ||||
return result[0] | ||||
|
r15919 | |||
r47366 | def backup(self, section, key): | |||
|
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: | ||||
r47366 | item = self._data[section][key] | |||
|
r15919 | except KeyError: | ||
r47366 | return (section, key) | |||
else: | ||||
return (section, key) + item | ||||
|
r15919 | |||
|
r8198 | def source(self, section, item): | ||
r47366 | result = self._get(section, item) | |||
if result is None: | ||||
return b"" | ||||
return result[1] | ||||
|
r43346 | |||
r47367 | def level(self, section, item): | |||
result = self._get(section, item) | ||||
if result is None: | ||||
return None | ||||
return result[2] | ||||
|
r8144 | def sections(self): | ||
return sorted(self._data.keys()) | ||||
|
r43346 | |||
|
r8144 | def items(self, section): | ||
r47366 | items = pycompat.iteritems(self._data.get(section, {})) | |||
r47367 | return [(k, v[0]) for (k, v) in items] | |||
|
r43346 | |||
|
r43347 | def set(self, section, item, value, source=b""): | ||
|
r31306 | if pycompat.ispy3: | ||
|
r43346 | assert not isinstance( | ||
section, str | ||||
|
r43347 | ), b'config section may not be unicode strings on Python 3' | ||
|
r43346 | assert not isinstance( | ||
item, str | ||||
|
r43347 | ), b'config item may not be unicode strings on Python 3' | ||
|
r43346 | assert not isinstance( | ||
value, str | ||||
|
r43347 | ), b'config values may not be unicode strings on Python 3' | ||
|
r8144 | if section not in self: | ||
|
r34353 | self._data[section] = util.cowsortdict() | ||
else: | ||||
self._data[section] = self._data[section].preparewrite() | ||||
r47367 | self._data[section][item] = (value, source, self._current_source_level) | |||
|
r8144 | |||
r47365 | def alter(self, section, key, new_value): | |||
"""alter a value without altering its source or level | ||||
This method is meant to be used by `ui.fixconfig` only.""" | ||||
item = self._data[section][key] | ||||
size = len(item) | ||||
new_item = (new_value,) + item[1:] | ||||
assert len(new_item) == size | ||||
self._data[section][key] = new_item | ||||
|
r15919 | def restore(self, data): | ||
"""restore data returned by self.backup""" | ||||
r47366 | if len(data) != 2: | |||
|
r15919 | # restore old data | ||
r47366 | section, key = data[:2] | |||
item = data[2:] | ||||
|
r34353 | self._data[section] = self._data[section].preparewrite() | ||
r47366 | self._data[section][key] = item | |||
|
r15919 | else: | ||
# no data before, remove everything | ||||
section, item = data | ||||
if section in self._data: | ||||
|
r22037 | self._data[section].pop(item, None) | ||
|
r15919 | |||
|
r8265 | def parse(self, src, data, sections=None, remap=None, include=None): | ||
|
r30349 | sectionre = util.re.compile(br'\[([^\[]+)\]') | ||
itemre = util.re.compile(br'([^=\s][^=]*?)\s*=\s*(.*\S|)') | ||||
contre = util.re.compile(br'\s+(\S|\S.*\S)\s*$') | ||||
emptyre = util.re.compile(br'(;|#|\s*$)') | ||||
commentre = util.re.compile(br'(;|#)') | ||||
unsetre = util.re.compile(br'%unset\s+(\S+)') | ||||
includere = util.re.compile(br'%include\s+(\S|\S.*\S)\s*$') | ||||
|
r43347 | section = b"" | ||
|
r8144 | item = None | ||
line = 0 | ||||
r9339 | cont = False | |||
|
r8180 | |||
|
r34714 | if remap: | ||
section = remap.get(section, section) | ||||
|
r9136 | for l in data.splitlines(True): | ||
|
r8144 | line += 1 | ||
|
r43347 | if line == 1 and l.startswith(b'\xef\xbb\xbf'): | ||
|
r16348 | # 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 | ||||
|
r43347 | v = self.get(section, item) + b"\n" + m.group(1) | ||
self.set(section, item, v, b"%s:%d" % (src, line)) | ||||
|
r8144 | continue | ||
item = None | ||||
|
r9469 | cont = False | ||
|
r8183 | m = includere.match(l) | ||
|
r25095 | |||
if m and include: | ||||
expanded = util.expandpath(m.group(1)) | ||||
|
r45814 | try: | ||
|
r45817 | include(expanded, remap=remap, sections=sections) | ||
|
r45814 | except IOError as inst: | ||
if inst.errno != errno.ENOENT: | ||||
|
r46506 | raise error.ConfigError( | ||
|
r45814 | _(b"cannot include %s (%s)") | ||
|
r45817 | % (expanded, encoding.strtolocal(inst.strerror)), | ||
|
r45814 | b"%s:%d" % (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: | ||
|
r34353 | self._data[section] = util.cowsortdict() | ||
|
r8144 | continue | ||
m = itemre.match(l) | ||||
if m: | ||||
item = m.group(1) | ||||
r9339 | cont = True | |||
|
r8193 | if sections and section not in sections: | ||
continue | ||||
|
r43347 | self.set(section, item, m.group(2), b"%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: | ||
|
r34455 | self._data[section] = self._data[section].preparewrite() | ||
|
r8184 | del self._data[section][name] | ||
|
r19087 | self._unset.append((section, name)) | ||
|
r8184 | continue | ||
|
r46362 | message = l.rstrip() | ||
if l.startswith(b' '): | ||||
message = b"unexpected leading whitespace: %s" % message | ||||
|
r46506 | raise error.ConfigError(message, (b"%s:%d" % (src, line))) | ||
|
r8265 | |||
def read(self, path, fp=None, sections=None, remap=None): | ||||
r47367 | self.new_source() | |||
|
r8265 | if not fp: | ||
|
r43347 | fp = util.posixfile(path, b'rb') | ||
|
r46554 | assert ( | ||
getattr(fp, 'mode', 'rb') == 'rb' | ||||
), b'config files must be opened in binary mode, got fp=%r mode=%r' % ( | ||||
fp, | ||||
fp.mode, | ||||
|
r43346 | ) | ||
|
r45768 | |||
|
r45816 | dir = os.path.dirname(path) | ||
|
r45817 | def include(rel, remap, sections): | ||
|
r45816 | abs = os.path.normpath(os.path.join(dir, rel)) | ||
|
r45768 | self.read(abs, remap=remap, sections=sections) | ||
r47367 | # anything after the include has a higher level | |||
self.new_source() | ||||
|
r45768 | |||
|
r43346 | self.parse( | ||
|
r45768 | path, fp.read(), sections=sections, remap=remap, include=include | ||
|
r43346 | ) | ||
|
r31481 | |||
def parselist(value): | ||||
"""parse a configuration value as a list of comma/space separated strings | ||||
|
r34133 | >>> parselist(b'this,is "a small" ,test') | ||
|
r31481 | ['this', 'is', 'a small', 'test'] | ||
""" | ||||
def _parse_plain(parts, s, offset): | ||||
whitespace = False | ||||
|
r43346 | while offset < len(s) and ( | ||
|
r43347 | s[offset : offset + 1].isspace() or s[offset : offset + 1] == b',' | ||
|
r43346 | ): | ||
|
r31481 | whitespace = True | ||
offset += 1 | ||||
if offset >= len(s): | ||||
return None, parts, offset | ||||
if whitespace: | ||||
|
r43347 | parts.append(b'') | ||
if s[offset : offset + 1] == b'"' and not parts[-1]: | ||||
|
r31481 | return _parse_quote, parts, offset + 1 | ||
|
r43347 | elif s[offset : offset + 1] == b'"' and parts[-1][-1:] == b'\\': | ||
|
r43346 | parts[-1] = parts[-1][:-1] + s[offset : offset + 1] | ||
|
r31481 | return _parse_plain, parts, offset + 1 | ||
|
r43346 | parts[-1] += s[offset : offset + 1] | ||
|
r31481 | return _parse_plain, parts, offset + 1 | ||
def _parse_quote(parts, s, offset): | ||||
|
r43347 | if offset < len(s) and s[offset : offset + 1] == b'"': # "" | ||
parts.append(b'') | ||||
|
r31481 | offset += 1 | ||
|
r43346 | while offset < len(s) and ( | ||
s[offset : offset + 1].isspace() | ||||
|
r43347 | or s[offset : offset + 1] == b',' | ||
|
r43346 | ): | ||
|
r31481 | offset += 1 | ||
return _parse_plain, parts, offset | ||||
|
r43347 | while offset < len(s) and s[offset : offset + 1] != b'"': | ||
|
r43346 | if ( | ||
|
r43347 | s[offset : offset + 1] == b'\\' | ||
|
r43346 | and offset + 1 < len(s) | ||
|
r43347 | and s[offset + 1 : offset + 2] == b'"' | ||
|
r43346 | ): | ||
|
r31481 | offset += 1 | ||
|
r43347 | parts[-1] += b'"' | ||
|
r31481 | else: | ||
|
r43346 | parts[-1] += s[offset : offset + 1] | ||
|
r31481 | offset += 1 | ||
if offset >= len(s): | ||||
real_parts = _configlist(parts[-1]) | ||||
if not real_parts: | ||||
|
r43347 | parts[-1] = b'"' | ||
|
r31481 | else: | ||
|
r43347 | real_parts[0] = b'"' + real_parts[0] | ||
|
r31481 | parts = parts[:-1] | ||
parts.extend(real_parts) | ||||
return None, parts, offset | ||||
offset += 1 | ||||
|
r43347 | while offset < len(s) and s[offset : offset + 1] in [b' ', b',']: | ||
|
r31481 | offset += 1 | ||
if offset < len(s): | ||||
|
r43347 | if offset + 1 == len(s) and s[offset : offset + 1] == b'"': | ||
parts[-1] += b'"' | ||||
|
r31481 | offset += 1 | ||
else: | ||||
|
r43347 | parts.append(b'') | ||
|
r31481 | else: | ||
return None, parts, offset | ||||
return _parse_plain, parts, offset | ||||
def _configlist(s): | ||||
|
r43347 | s = s.rstrip(b' ,') | ||
|
r31481 | if not s: | ||
return [] | ||||
|
r43347 | parser, parts, offset = _parse_plain, [b''], 0 | ||
|
r31481 | while parser: | ||
parser, parts, offset = parser(parts, s, offset) | ||||
return parts | ||||
if value is not None and isinstance(value, bytes): | ||||
|
r43347 | result = _configlist(value.lstrip(b' ,\n')) | ||
|
r31481 | else: | ||
result = value | ||||
return result or [] | ||||