Mercurial > public > mercurial-scm > hg-stable
diff mercurial/config.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 4ad002b2584d |
children | 687b865b95ad |
line wrap: on
line diff
--- a/mercurial/config.py Sat Oct 05 10:29:34 2019 -0400 +++ b/mercurial/config.py Sun Oct 06 09:45:02 2019 -0400 @@ -17,6 +17,7 @@ util, ) + class config(object): def __init__(self, data=None, includepaths=None): self._data = {} @@ -28,17 +29,23 @@ self._source = data._source.copy() else: self._source = util.cowdict() + def copy(self): return config(self) + def __contains__(self, section): return section in self._data + def hasitem(self, section, item): return item in self._data.get(section, {}) + def __getitem__(self, section): return self._data.get(section, {}) + def __iter__(self): for d in self.sections(): yield d + def update(self, src): self._source = self._source.preparewrite() for s, n in src._unset: @@ -55,6 +62,7 @@ self._data[s] = util.cowsortdict() self._data[s].update(src._data[s]) self._source.update(src._source) + def get(self, section, item, default=None): return self._data.get(section, {}).get(item, default) @@ -72,18 +80,24 @@ def source(self, section, item): return self._source.get((section, item), "") + def sections(self): return sorted(self._data.keys()) + def items(self, section): return list(self._data.get(section, {}).iteritems()) + def set(self, section, item, value, source=""): if pycompat.ispy3: - assert not isinstance(section, str), ( - 'config section may not be unicode strings on Python 3') - assert not isinstance(item, str), ( - 'config item may not be unicode strings on Python 3') - assert not isinstance(value, str), ( - 'config values may not be unicode strings on Python 3') + assert not isinstance( + section, str + ), 'config section may not be unicode strings on Python 3' + assert not isinstance( + item, str + ), 'config item may not be unicode strings on Python 3' + assert not isinstance( + value, str + ), 'config values may not be unicode strings on Python 3' if section not in self: self._data[section] = util.cowsortdict() else: @@ -156,9 +170,11 @@ break except IOError as inst: if inst.errno != errno.ENOENT: - raise error.ParseError(_("cannot include %s (%s)") - % (inc, inst.strerror), - "%s:%d" % (src, line)) + raise error.ParseError( + _("cannot include %s (%s)") + % (inc, inst.strerror), + "%s:%d" % (src, line), + ) continue if emptyre.match(l): continue @@ -194,11 +210,16 @@ def read(self, path, fp=None, sections=None, remap=None): if not fp: fp = util.posixfile(path, 'rb') - assert getattr(fp, 'mode', r'rb') == r'rb', ( - 'config files must be opened in binary mode, got fp=%r mode=%r' % ( - fp, fp.mode)) - self.parse(path, fp.read(), - sections=sections, remap=remap, include=self.read) + assert ( + getattr(fp, 'mode', r'rb') == r'rb' + ), 'config files must be opened in binary mode, got fp=%r mode=%r' % ( + fp, + fp.mode, + ) + self.parse( + path, fp.read(), sections=sections, remap=remap, include=self.read + ) + def parselist(value): """parse a configuration value as a list of comma/space separated strings @@ -209,38 +230,44 @@ def _parse_plain(parts, s, offset): whitespace = False - while offset < len(s) and (s[offset:offset + 1].isspace() - or s[offset:offset + 1] == ','): + while offset < len(s) and ( + s[offset : offset + 1].isspace() or s[offset : offset + 1] == ',' + ): whitespace = True offset += 1 if offset >= len(s): return None, parts, offset if whitespace: parts.append('') - if s[offset:offset + 1] == '"' and not parts[-1]: + if s[offset : offset + 1] == '"' and not parts[-1]: return _parse_quote, parts, offset + 1 - elif s[offset:offset + 1] == '"' and parts[-1][-1:] == '\\': - parts[-1] = parts[-1][:-1] + s[offset:offset + 1] + elif s[offset : offset + 1] == '"' and parts[-1][-1:] == '\\': + parts[-1] = parts[-1][:-1] + s[offset : offset + 1] return _parse_plain, parts, offset + 1 - parts[-1] += s[offset:offset + 1] + parts[-1] += s[offset : offset + 1] return _parse_plain, parts, offset + 1 def _parse_quote(parts, s, offset): - if offset < len(s) and s[offset:offset + 1] == '"': # "" + if offset < len(s) and s[offset : offset + 1] == '"': # "" parts.append('') offset += 1 - while offset < len(s) and (s[offset:offset + 1].isspace() or - s[offset:offset + 1] == ','): + while offset < len(s) and ( + s[offset : offset + 1].isspace() + or s[offset : offset + 1] == ',' + ): offset += 1 return _parse_plain, parts, offset - while offset < len(s) and s[offset:offset + 1] != '"': - if (s[offset:offset + 1] == '\\' and offset + 1 < len(s) - and s[offset + 1:offset + 2] == '"'): + while offset < len(s) and s[offset : offset + 1] != '"': + if ( + s[offset : offset + 1] == '\\' + and offset + 1 < len(s) + and s[offset + 1 : offset + 2] == '"' + ): offset += 1 parts[-1] += '"' else: - parts[-1] += s[offset:offset + 1] + parts[-1] += s[offset : offset + 1] offset += 1 if offset >= len(s): @@ -254,11 +281,11 @@ return None, parts, offset offset += 1 - while offset < len(s) and s[offset:offset + 1] in [' ', ',']: + while offset < len(s) and s[offset : offset + 1] in [' ', ',']: offset += 1 if offset < len(s): - if offset + 1 == len(s) and s[offset:offset + 1] == '"': + if offset + 1 == len(s) and s[offset : offset + 1] == '"': parts[-1] += '"' offset += 1 else: