config.py
311 lines
| 10.4 KiB
| text/x-python
|
PythonLexer
/ mercurial / config.py
Martin Geisler
|
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 | ||||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Martin Geisler
|
r8229 | |||
Gregory Szorc
|
r25923 | from __future__ import absolute_import | ||
import errno | ||||
import os | ||||
from .i18n import _ | ||||
Gregory Szorc
|
r43359 | from .pycompat import getattr | ||
Gregory Szorc
|
r25923 | from . import ( | ||
Denis Laxalde
|
r43570 | encoding, | ||
Gregory Szorc
|
r25923 | error, | ||
Augie Fackler
|
r31306 | pycompat, | ||
Gregory Szorc
|
r25923 | util, | ||
) | ||||
Matt Mackall
|
r8144 | |||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8186 | class config(object): | ||
Martijn Pieters
|
r31374 | def __init__(self, data=None, includepaths=None): | ||
Matt Mackall
|
r8144 | self._data = {} | ||
FUJIWARA Katsunori
|
r19087 | self._unset = [] | ||
Martijn Pieters
|
r31374 | self._includepaths = includepaths or [] | ||
Matt Mackall
|
r8144 | if data: | ||
for k in data._data: | ||||
self._data[k] = data[k].copy() | ||||
Matt Mackall
|
r8185 | self._source = data._source.copy() | ||
Jun Wu
|
r34353 | else: | ||
self._source = util.cowdict() | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8144 | def copy(self): | ||
return config(self) | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8144 | def __contains__(self, section): | ||
return section in self._data | ||||
Augie Fackler
|
r43346 | |||
Bryan O'Sullivan
|
r27696 | def hasitem(self, section, item): | ||
return item in self._data.get(section, {}) | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8186 | def __getitem__(self, section): | ||
return self._data.get(section, {}) | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8186 | def __iter__(self): | ||
for d in self.sections(): | ||||
yield d | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8193 | def update(self, src): | ||
Jun Wu
|
r34353 | self._source = self._source.preparewrite() | ||
FUJIWARA Katsunori
|
r19087 | for s, n in src._unset: | ||
Jun Wu
|
r34353 | ds = self._data.get(s, None) | ||
if ds is not None and n in ds: | ||||
self._data[s] = ds.preparewrite() | ||||
FUJIWARA Katsunori
|
r19087 | del self._data[s][n] | ||
del self._source[(s, n)] | ||||
Matt Mackall
|
r8193 | for s in src: | ||
Jun Wu
|
r34353 | ds = self._data.get(s, None) | ||
if ds: | ||||
self._data[s] = ds.preparewrite() | ||||
else: | ||||
self._data[s] = util.cowsortdict() | ||||
Matt Mackall
|
r8193 | self._data[s].update(src._data[s]) | ||
self._source.update(src._source) | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8144 | def get(self, section, item, default=None): | ||
Matt Mackall
|
r8185 | return self._data.get(section, {}).get(item, default) | ||
Pierre-Yves David
|
r15919 | |||
def backup(self, section, item): | ||||
timeless@mozdev.org
|
r17527 | """return a tuple allowing restore to reinstall a previous value | ||
Pierre-Yves David
|
r15919 | |||
timeless@mozdev.org
|
r17530 | The main reason we need it is because it handles the "no data" case. | ||
Pierre-Yves David
|
r15919 | """ | ||
try: | ||||
value = self._data[section][item] | ||||
source = self.source(section, item) | ||||
return (section, item, value, source) | ||||
except KeyError: | ||||
return (section, item) | ||||
Matt Mackall
|
r8198 | def source(self, section, item): | ||
Augie Fackler
|
r43347 | return self._source.get((section, item), b"") | ||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8144 | def sections(self): | ||
return sorted(self._data.keys()) | ||||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r8144 | def items(self, section): | ||
Gregory Szorc
|
r43376 | return list(pycompat.iteritems(self._data.get(section, {}))) | ||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | def set(self, section, item, value, source=b""): | ||
Augie Fackler
|
r31306 | if pycompat.ispy3: | ||
Augie Fackler
|
r43346 | assert not isinstance( | ||
section, str | ||||
Augie Fackler
|
r43347 | ), b'config section may not be unicode strings on Python 3' | ||
Augie Fackler
|
r43346 | assert not isinstance( | ||
item, str | ||||
Augie Fackler
|
r43347 | ), b'config item may not be unicode strings on Python 3' | ||
Augie Fackler
|
r43346 | assert not isinstance( | ||
value, str | ||||
Augie Fackler
|
r43347 | ), b'config values may not be unicode strings on Python 3' | ||
Matt Mackall
|
r8144 | if section not in self: | ||
Jun Wu
|
r34353 | self._data[section] = util.cowsortdict() | ||
else: | ||||
self._data[section] = self._data[section].preparewrite() | ||||
Matt Mackall
|
r8185 | self._data[section][item] = value | ||
Mads Kiilerich
|
r20789 | if source: | ||
Jun Wu
|
r34353 | self._source = self._source.preparewrite() | ||
Mads Kiilerich
|
r20789 | self._source[(section, item)] = source | ||
Matt Mackall
|
r8144 | |||
Pierre-Yves David
|
r15919 | def restore(self, data): | ||
"""restore data returned by self.backup""" | ||||
Jun Wu
|
r34353 | self._source = self._source.preparewrite() | ||
Pierre-Yves David
|
r15919 | if len(data) == 4: | ||
# restore old data | ||||
section, item, value, source = data | ||||
Jun Wu
|
r34353 | self._data[section] = self._data[section].preparewrite() | ||
Pierre-Yves David
|
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: | ||||
Pierre-Yves David
|
r22037 | self._data[section].pop(item, None) | ||
Pierre-Yves David
|
r15919 | self._source.pop((section, item), None) | ||
Matt Mackall
|
r8265 | def parse(self, src, data, sections=None, remap=None, include=None): | ||
Augie Fackler
|
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*$') | ||||
Augie Fackler
|
r43347 | section = b"" | ||
Matt Mackall
|
r8144 | item = None | ||
line = 0 | ||||
r9339 | cont = False | |||
Matt Mackall
|
r8180 | |||
Yuya Nishihara
|
r34714 | if remap: | ||
section = remap.get(section, section) | ||||
Nicolas Dumazet
|
r9136 | for l in data.splitlines(True): | ||
Matt Mackall
|
r8144 | line += 1 | ||
Augie Fackler
|
r43347 | if line == 1 and l.startswith(b'\xef\xbb\xbf'): | ||
Matt Mackall
|
r16348 | # Someone set us up the BOM | ||
l = l[3:] | ||||
Matt Mackall
|
r8144 | if cont: | ||
Matt Mackall
|
r14642 | if commentre.match(l): | ||
continue | ||||
Matt Mackall
|
r8144 | m = contre.match(l) | ||
if m: | ||||
Matt Mackall
|
r8193 | if sections and section not in sections: | ||
continue | ||||
Augie Fackler
|
r43347 | v = self.get(section, item) + b"\n" + m.group(1) | ||
self.set(section, item, v, b"%s:%d" % (src, line)) | ||||
Matt Mackall
|
r8144 | continue | ||
item = None | ||||
Nicolas Dumazet
|
r9469 | cont = False | ||
Matt Mackall
|
r8183 | m = includere.match(l) | ||
Jordi Gutiérrez Hermoso
|
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)) | ||||
Martin Geisler
|
r10042 | try: | ||
include(inc, remap=remap, sections=sections) | ||||
Jordi Gutiérrez Hermoso
|
r25095 | break | ||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Matt Mackall
|
r14486 | if inst.errno != errno.ENOENT: | ||
Augie Fackler
|
r43346 | raise error.ParseError( | ||
Augie Fackler
|
r43347 | _(b"cannot include %s (%s)") | ||
Denis Laxalde
|
r43570 | % (inc, encoding.strtolocal(inst.strerror)), | ||
Augie Fackler
|
r43347 | b"%s:%d" % (src, line), | ||
Augie Fackler
|
r43346 | ) | ||
Matt Mackall
|
r8183 | continue | ||
Matt Mackall
|
r8144 | if emptyre.match(l): | ||
continue | ||||
m = sectionre.match(l) | ||||
if m: | ||||
section = m.group(1) | ||||
Matt Mackall
|
r8298 | if remap: | ||
section = remap.get(section, section) | ||||
Matt Mackall
|
r8144 | if section not in self: | ||
Jun Wu
|
r34353 | self._data[section] = util.cowsortdict() | ||
Matt Mackall
|
r8144 | continue | ||
m = itemre.match(l) | ||||
if m: | ||||
item = m.group(1) | ||||
r9339 | cont = True | |||
Matt Mackall
|
r8193 | if sections and section not in sections: | ||
continue | ||||
Augie Fackler
|
r43347 | self.set(section, item, m.group(2), b"%s:%d" % (src, line)) | ||
Matt Mackall
|
r8144 | continue | ||
Matt Mackall
|
r8184 | m = unsetre.match(l) | ||
if m: | ||||
name = m.group(1) | ||||
Matt Mackall
|
r8193 | if sections and section not in sections: | ||
continue | ||||
Martin Geisler
|
r13031 | if self.get(section, name) is not None: | ||
Jun Wu
|
r34455 | self._data[section] = self._data[section].preparewrite() | ||
Matt Mackall
|
r8184 | del self._data[section][name] | ||
FUJIWARA Katsunori
|
r19087 | self._unset.append((section, name)) | ||
Matt Mackall
|
r8184 | continue | ||
Augie Fackler
|
r43347 | raise error.ParseError(l.rstrip(), (b"%s:%d" % (src, line))) | ||
Matt Mackall
|
r8265 | |||
def read(self, path, fp=None, sections=None, remap=None): | ||||
if not fp: | ||||
Augie Fackler
|
r43347 | fp = util.posixfile(path, b'rb') | ||
Augie Fackler
|
r43906 | assert getattr(fp, 'mode', 'rb') == 'rb', ( | ||
r43663 | b'config files must be opened in binary mode, got fp=%r mode=%r' | |||
% (fp, fp.mode,) | ||||
Augie Fackler
|
r43346 | ) | ||
self.parse( | ||||
path, fp.read(), sections=sections, remap=remap, include=self.read | ||||
) | ||||
Jun Wu
|
r31481 | |||
def parselist(value): | ||||
"""parse a configuration value as a list of comma/space separated strings | ||||
Yuya Nishihara
|
r34133 | >>> parselist(b'this,is "a small" ,test') | ||
Jun Wu
|
r31481 | ['this', 'is', 'a small', 'test'] | ||
""" | ||||
def _parse_plain(parts, s, offset): | ||||
whitespace = False | ||||
Augie Fackler
|
r43346 | while offset < len(s) and ( | ||
Augie Fackler
|
r43347 | s[offset : offset + 1].isspace() or s[offset : offset + 1] == b',' | ||
Augie Fackler
|
r43346 | ): | ||
Jun Wu
|
r31481 | whitespace = True | ||
offset += 1 | ||||
if offset >= len(s): | ||||
return None, parts, offset | ||||
if whitespace: | ||||
Augie Fackler
|
r43347 | parts.append(b'') | ||
if s[offset : offset + 1] == b'"' and not parts[-1]: | ||||
Jun Wu
|
r31481 | return _parse_quote, parts, offset + 1 | ||
Augie Fackler
|
r43347 | elif s[offset : offset + 1] == b'"' and parts[-1][-1:] == b'\\': | ||
Augie Fackler
|
r43346 | parts[-1] = parts[-1][:-1] + s[offset : offset + 1] | ||
Jun Wu
|
r31481 | return _parse_plain, parts, offset + 1 | ||
Augie Fackler
|
r43346 | parts[-1] += s[offset : offset + 1] | ||
Jun Wu
|
r31481 | return _parse_plain, parts, offset + 1 | ||
def _parse_quote(parts, s, offset): | ||||
Augie Fackler
|
r43347 | if offset < len(s) and s[offset : offset + 1] == b'"': # "" | ||
parts.append(b'') | ||||
Jun Wu
|
r31481 | offset += 1 | ||
Augie Fackler
|
r43346 | while offset < len(s) and ( | ||
s[offset : offset + 1].isspace() | ||||
Augie Fackler
|
r43347 | or s[offset : offset + 1] == b',' | ||
Augie Fackler
|
r43346 | ): | ||
Jun Wu
|
r31481 | offset += 1 | ||
return _parse_plain, parts, offset | ||||
Augie Fackler
|
r43347 | while offset < len(s) and s[offset : offset + 1] != b'"': | ||
Augie Fackler
|
r43346 | if ( | ||
Augie Fackler
|
r43347 | s[offset : offset + 1] == b'\\' | ||
Augie Fackler
|
r43346 | and offset + 1 < len(s) | ||
Augie Fackler
|
r43347 | and s[offset + 1 : offset + 2] == b'"' | ||
Augie Fackler
|
r43346 | ): | ||
Jun Wu
|
r31481 | offset += 1 | ||
Augie Fackler
|
r43347 | parts[-1] += b'"' | ||
Jun Wu
|
r31481 | else: | ||
Augie Fackler
|
r43346 | parts[-1] += s[offset : offset + 1] | ||
Jun Wu
|
r31481 | offset += 1 | ||
if offset >= len(s): | ||||
real_parts = _configlist(parts[-1]) | ||||
if not real_parts: | ||||
Augie Fackler
|
r43347 | parts[-1] = b'"' | ||
Jun Wu
|
r31481 | else: | ||
Augie Fackler
|
r43347 | real_parts[0] = b'"' + real_parts[0] | ||
Jun Wu
|
r31481 | parts = parts[:-1] | ||
parts.extend(real_parts) | ||||
return None, parts, offset | ||||
offset += 1 | ||||
Augie Fackler
|
r43347 | while offset < len(s) and s[offset : offset + 1] in [b' ', b',']: | ||
Jun Wu
|
r31481 | offset += 1 | ||
if offset < len(s): | ||||
Augie Fackler
|
r43347 | if offset + 1 == len(s) and s[offset : offset + 1] == b'"': | ||
parts[-1] += b'"' | ||||
Jun Wu
|
r31481 | offset += 1 | ||
else: | ||||
Augie Fackler
|
r43347 | parts.append(b'') | ||
Jun Wu
|
r31481 | else: | ||
return None, parts, offset | ||||
return _parse_plain, parts, offset | ||||
def _configlist(s): | ||||
Augie Fackler
|
r43347 | s = s.rstrip(b' ,') | ||
Jun Wu
|
r31481 | if not s: | ||
return [] | ||||
Augie Fackler
|
r43347 | parser, parts, offset = _parse_plain, [b''], 0 | ||
Jun Wu
|
r31481 | while parser: | ||
parser, parts, offset = parser(parts, s, offset) | ||||
return parts | ||||
if value is not None and isinstance(value, bytes): | ||||
Augie Fackler
|
r43347 | result = _configlist(value.lstrip(b' ,\n')) | ||
Jun Wu
|
r31481 | else: | ||
result = value | ||||
return result or [] | ||||