config.py
313 lines
| 10.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): | ||
|
r31374 | def __init__(self, data=None, includepaths=None): | ||
|
r8144 | self._data = {} | ||
|
r19087 | self._unset = [] | ||
|
r31374 | self._includepaths = includepaths or [] | ||
|
r8144 | if data: | ||
for k in data._data: | ||||
self._data[k] = data[k].copy() | ||||
|
r8185 | self._source = data._source.copy() | ||
|
r34353 | else: | ||
self._source = util.cowdict() | ||||
|
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): | ||
|
r34353 | self._source = self._source.preparewrite() | ||
|
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] | ||
del self._source[(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() | ||||
|
r8193 | self._data[s].update(src._data[s]) | ||
self._source.update(src._source) | ||||
|
r43346 | |||
|
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): | ||
|
r43347 | return self._source.get((section, item), b"") | ||
|
r43346 | |||
|
r8144 | def sections(self): | ||
return sorted(self._data.keys()) | ||||
|
r43346 | |||
|
r8144 | def items(self, section): | ||
|
r43376 | return list(pycompat.iteritems(self._data.get(section, {}))) | ||
|
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() | ||||
|
r8185 | self._data[section][item] = value | ||
|
r20789 | if source: | ||
|
r34353 | self._source = self._source.preparewrite() | ||
|
r20789 | self._source[(section, item)] = source | ||
|
r8144 | |||
|
r15919 | def restore(self, data): | ||
"""restore data returned by self.backup""" | ||||
|
r34353 | self._source = self._source.preparewrite() | ||
|
r15919 | if len(data) == 4: | ||
# restore old data | ||||
section, item, value, source = data | ||||
|
r34353 | self._data[section] = self._data[section].preparewrite() | ||
|
r15919 | 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): | ||
|
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)) | ||||
includepaths = [os.path.dirname(src)] + self._includepaths | ||||
for base in includepaths: | ||||
inc = os.path.normpath(os.path.join(base, expanded)) | ||||
|
r10042 | try: | ||
include(inc, remap=remap, sections=sections) | ||||
|
r25095 | break | ||
|
r25660 | except IOError as inst: | ||
|
r14486 | if inst.errno != errno.ENOENT: | ||
|
r43346 | raise error.ParseError( | ||
|
r43347 | _(b"cannot include %s (%s)") | ||
|
r43570 | % (inc, encoding.strtolocal(inst.strerror)), | ||
|
r43347 | b"%s:%d" % (src, line), | ||
|
r43346 | ) | ||
|
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 | ||
|
r43347 | raise error.ParseError(l.rstrip(), (b"%s:%d" % (src, line))) | ||
|
r8265 | |||
def read(self, path, fp=None, sections=None, remap=None): | ||||
if not fp: | ||||
|
r43347 | fp = util.posixfile(path, b'rb') | ||
|
r43346 | assert ( | ||
getattr(fp, 'mode', r'rb') == r'rb' | ||||
|
r43347 | ), b'config files must be opened in binary mode, got fp=%r mode=%r' % ( | ||
|
r43346 | fp, | ||
fp.mode, | ||||
) | ||||
self.parse( | ||||
path, fp.read(), sections=sections, remap=remap, include=self.read | ||||
) | ||||
|
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 [] | ||||