mercurial/config.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 29 Jan 2021 10:28:03 +0100
changeset 46621 d3df397e7a59
parent 46620 7621ab4005bf
child 46622 a3dced4b7b04
permissions -rw-r--r--
config: track "source" along side value Currently the source is stored in a entirely different way than the data. This is impractical. Especially if we are about to add more of such metadata. So lets put them back together. Differential Revision: https://phab.mercurial-scm.org/D9925
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8229
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
     1
# config.py - configuration parsing for Mercurial
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
     2
#
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
     3
#  Copyright 2009 Matt Mackall <mpm@selenic.com> and others
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
     4
#
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10042
diff changeset
     6
# GNU General Public License version 2 or any later version.
8229
ddf3d6656e7c config: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents: 8222
diff changeset
     7
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     8
from __future__ import absolute_import
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     9
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    10
import errno
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    11
import os
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    12
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    13
from .i18n import _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
    14
from .pycompat import getattr
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    15
from . import (
43276
d201a637c971 py3: encode underlying error message during parse error of %include
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43106
diff changeset
    16
    encoding,
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    17
    error,
31306
c920efa9d34b config: guard against setconfig specifying unicode values on py3
Augie Fackler <raf@durin42.com>
parents: 31176
diff changeset
    18
    pycompat,
25923
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    19
    util,
a027a0813b44 config: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    20
)
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    22
8186
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
    23
class config(object):
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
    24
    def __init__(self, data=None):
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    25
        self._data = {}
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
    26
        self._unset = []
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    27
        if data:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    28
            for k in data._data:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    29
                self._data[k] = data[k].copy()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    30
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    31
    def copy(self):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    32
        return config(self)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    33
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    34
    def __contains__(self, section):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    35
        return section in self._data
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    36
27696
e70c97cc9243 config: add hasconfig method and supporting plumbing
Bryan O'Sullivan <bos@serpentine.com>
parents: 25923
diff changeset
    37
    def hasitem(self, section, item):
e70c97cc9243 config: add hasconfig method and supporting plumbing
Bryan O'Sullivan <bos@serpentine.com>
parents: 25923
diff changeset
    38
        return item in self._data.get(section, {})
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    39
8186
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
    40
    def __getitem__(self, section):
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
    41
        return self._data.get(section, {})
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    42
8186
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
    43
    def __iter__(self):
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
    44
        for d in self.sections():
6a0018cdb2fe config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents: 8185
diff changeset
    45
            yield d
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    46
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
    47
    def update(self, src):
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
    48
        for s, n in src._unset:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    49
            ds = self._data.get(s, None)
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    50
            if ds is not None and n in ds:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    51
                self._data[s] = ds.preparewrite()
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
    52
                del self._data[s][n]
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
    53
        for s in src:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    54
            ds = self._data.get(s, None)
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    55
            if ds:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    56
                self._data[s] = ds.preparewrite()
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    57
            else:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
    58
                self._data[s] = util.cowsortdict()
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
    59
            self._data[s].update(src._data[s])
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    60
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    61
    def _get(self, section, item):
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    62
        return self._data.get(section, {}).get(item)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    63
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
    def get(self, section, item, default=None):
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    65
        result = self._get(section, item)
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    66
        if result is None:
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    67
            return default
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    68
        return result[0]
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
    69
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    70
    def backup(self, section, key):
17527
6e11d5cf8e39 spelling: value
timeless@mozdev.org
parents: 16944
diff changeset
    71
        """return a tuple allowing restore to reinstall a previous value
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
    72
17530
0dff04ffa15d grammar: it-handles
timeless@mozdev.org
parents: 17527
diff changeset
    73
        The main reason we need it is because it handles the "no data" case.
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
    74
        """
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
    75
        try:
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    76
            item = self._data[section][key]
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
    77
        except KeyError:
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    78
            return (section, key)
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    79
        else:
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    80
            return (section, key) + item
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
    81
8198
cf9accffd0b3 config: getsource -> source
Matt Mackall <mpm@selenic.com>
parents: 8193
diff changeset
    82
    def source(self, section, item):
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    83
        result = self._get(section, item)
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    84
        if result is None:
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    85
            return b""
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    86
        return result[1]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    87
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    88
    def sections(self):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    89
        return sorted(self._data.keys())
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    90
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    91
    def items(self, section):
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    92
        items = pycompat.iteritems(self._data.get(section, {}))
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
    93
        return [(k, v) for (k, (v, s)) in items]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    94
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    95
    def set(self, section, item, value, source=b""):
31306
c920efa9d34b config: guard against setconfig specifying unicode values on py3
Augie Fackler <raf@durin42.com>
parents: 31176
diff changeset
    96
        if pycompat.ispy3:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    97
            assert not isinstance(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
    98
                section, str
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    99
            ), b'config section may not be unicode strings on Python 3'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   100
            assert not isinstance(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   101
                item, str
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
            ), b'config item may not be unicode strings on Python 3'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   103
            assert not isinstance(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   104
                value, str
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   105
            ), b'config values may not be unicode strings on Python 3'
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   106
        if section not in self:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
   107
            self._data[section] = util.cowsortdict()
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
   108
        else:
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
   109
            self._data[section] = self._data[section].preparewrite()
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
   110
        self._data[section][item] = (value, source)
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   111
46620
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   112
    def alter(self, section, key, new_value):
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   113
        """alter a value without altering its source or level
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   114
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   115
        This method is meant to be used by `ui.fixconfig` only."""
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   116
        item = self._data[section][key]
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   117
        size = len(item)
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   118
        new_item = (new_value,) + item[1:]
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   119
        assert len(new_item) == size
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   120
        self._data[section][key] = new_item
7621ab4005bf config: use a new `alter` method in `fixconfig`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45942
diff changeset
   121
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   122
    def restore(self, data):
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   123
        """restore data returned by self.backup"""
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
   124
        if len(data) != 2:
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   125
            # restore old data
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
   126
            section, key = data[:2]
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
   127
            item = data[2:]
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
   128
            self._data[section] = self._data[section].preparewrite()
46621
d3df397e7a59 config: track "source" along side value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46620
diff changeset
   129
            self._data[section][key] = item
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   130
        else:
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   131
            # no data before, remove everything
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   132
            section, item = data
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   133
            if section in self._data:
22037
8665c647da6e config: fix restoreconfig of non existing config
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21910
diff changeset
   134
                self._data[section].pop(item, None)
15919
69e792cf7851 config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 14696
diff changeset
   135
8265
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
   136
    def parse(self, src, data, sections=None, remap=None, include=None):
30349
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
   137
        sectionre = util.re.compile(br'\[([^\[]+)\]')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
   138
        itemre = util.re.compile(br'([^=\s][^=]*?)\s*=\s*(.*\S|)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
   139
        contre = util.re.compile(br'\s+(\S|\S.*\S)\s*$')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
   140
        emptyre = util.re.compile(br'(;|#|\s*$)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
   141
        commentre = util.re.compile(br'(;|#)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
   142
        unsetre = util.re.compile(br'%unset\s+(\S+)')
954002426f78 config: mark parser regexes as bytes explicitly
Augie Fackler <augie@google.com>
parents: 27696
diff changeset
   143
        includere = util.re.compile(br'%include\s+(\S|\S.*\S)\s*$')
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   144
        section = b""
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   145
        item = None
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   146
        line = 0
9339
9be91129c96e config: improve code readability
Andrey <py4fun@gmail.com>
parents: 9136
diff changeset
   147
        cont = False
8180
6fc30fe7f3e7 hgweb: use config.config
Matt Mackall <mpm@selenic.com>
parents: 8144
diff changeset
   148
34713
e5a2cfc524d4 config: allow remapping the default section
Yuya Nishihara <yuya@tcha.org>
parents: 34454
diff changeset
   149
        if remap:
e5a2cfc524d4 config: allow remapping the default section
Yuya Nishihara <yuya@tcha.org>
parents: 34454
diff changeset
   150
            section = remap.get(section, section)
e5a2cfc524d4 config: allow remapping the default section
Yuya Nishihara <yuya@tcha.org>
parents: 34454
diff changeset
   151
9136
31177742f54a for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8312
diff changeset
   152
        for l in data.splitlines(True):
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   153
            line += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   154
            if line == 1 and l.startswith(b'\xef\xbb\xbf'):
16348
f350021ee32e config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents: 15919
diff changeset
   155
                # Someone set us up the BOM
f350021ee32e config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents: 15919
diff changeset
   156
                l = l[3:]
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   157
            if cont:
14642
fdcdb221a922 config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents: 14486
diff changeset
   158
                if commentre.match(l):
fdcdb221a922 config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents: 14486
diff changeset
   159
                    continue
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
                m = contre.match(l)
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   161
                if m:
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
   162
                    if sections and section not in sections:
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
   163
                        continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   164
                    v = self.get(section, item) + b"\n" + m.group(1)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   165
                    self.set(section, item, v, b"%s:%d" % (src, line))
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   166
                    continue
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   167
                item = None
9469
7f0f882af23d config: abort on indented non-continuation lines (issue1829)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8312
diff changeset
   168
                cont = False
8183
2858ab754995 config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents: 8180
diff changeset
   169
            m = includere.match(l)
25095
3182965b3971 config: give it an includepaths option for looking for config files
Jordi Guti?rrez Hermoso <jordigh@octave.org>
parents: 22419
diff changeset
   170
3182965b3971 config: give it an includepaths option for looking for config files
Jordi Guti?rrez Hermoso <jordigh@octave.org>
parents: 22419
diff changeset
   171
            if m and include:
3182965b3971 config: give it an includepaths option for looking for config files
Jordi Guti?rrez Hermoso <jordigh@octave.org>
parents: 22419
diff changeset
   172
                expanded = util.expandpath(m.group(1))
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
   173
                try:
45257
668af67bfd18 config: remove now-unused `abs` argument from `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45256
diff changeset
   174
                    include(expanded, remap=remap, sections=sections)
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
   175
                except IOError as inst:
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
   176
                    if inst.errno != errno.ENOENT:
45894
9dc1351d0b5f errors: raise ConfigError on failure to parse config file
Martin von Zweigbergk <martinvonz@google.com>
parents: 45777
diff changeset
   177
                        raise error.ConfigError(
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
   178
                            _(b"cannot include %s (%s)")
45257
668af67bfd18 config: remove now-unused `abs` argument from `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45256
diff changeset
   179
                            % (expanded, encoding.strtolocal(inst.strerror)),
45254
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
   180
                            b"%s:%d" % (src, line),
3f54242781e9 config: remove now-unused support for "includepaths"
Martin von Zweigbergk <martinvonz@google.com>
parents: 45208
diff changeset
   181
                        )
8183
2858ab754995 config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents: 8180
diff changeset
   182
                continue
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   183
            if emptyre.match(l):
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   184
                continue
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   185
            m = sectionre.match(l)
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   186
            if m:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   187
                section = m.group(1)
8298
9542f4c3fa1b config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents: 8265
diff changeset
   188
                if remap:
9542f4c3fa1b config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents: 8265
diff changeset
   189
                    section = remap.get(section, section)
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   190
                if section not in self:
34357
c41444a39de2 config: use copy-on-write to improve copy performance
Jun Wu <quark@fb.com>
parents: 34131
diff changeset
   191
                    self._data[section] = util.cowsortdict()
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   192
                continue
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   193
            m = itemre.match(l)
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   194
            if m:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   195
                item = m.group(1)
9339
9be91129c96e config: improve code readability
Andrey <py4fun@gmail.com>
parents: 9136
diff changeset
   196
                cont = True
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
   197
                if sections and section not in sections:
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
   198
                    continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   199
                self.set(section, item, m.group(2), b"%s:%d" % (src, line))
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   200
                continue
8184
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
   201
            m = unsetre.match(l)
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
   202
            if m:
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
   203
                name = m.group(1)
8193
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
   204
                if sections and section not in sections:
94246e90081e config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents: 8192
diff changeset
   205
                    continue
13031
3da456d0c885 code style: prefer 'is' and 'is not' tests with singletons
Martin Geisler <mg@aragost.com>
parents: 11292
diff changeset
   206
                if self.get(section, name) is not None:
34454
0efdfb57b05c config: add a missing preparewrite() call
Jun Wu <quark@fb.com>
parents: 34357
diff changeset
   207
                    self._data[section] = self._data[section].preparewrite()
8184
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
   208
                    del self._data[section][name]
19087
7d82ad4b3727 config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17537
diff changeset
   209
                self._unset.append((section, name))
8184
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
   210
                continue
9189afe1eba3 config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents: 8183
diff changeset
   211
45777
0883413e09bc config: move message about leading spaces in config to config.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 45257
diff changeset
   212
            message = l.rstrip()
0883413e09bc config: move message about leading spaces in config to config.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 45257
diff changeset
   213
            if l.startswith(b' '):
0883413e09bc config: move message about leading spaces in config to config.py
Martin von Zweigbergk <martinvonz@google.com>
parents: 45257
diff changeset
   214
                message = b"unexpected leading whitespace: %s" % message
45894
9dc1351d0b5f errors: raise ConfigError on failure to parse config file
Martin von Zweigbergk <martinvonz@google.com>
parents: 45777
diff changeset
   215
            raise error.ConfigError(message, (b"%s:%d" % (src, line)))
8265
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
   216
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
   217
    def read(self, path, fp=None, sections=None, remap=None):
52c5be55af82 config: add parse interface
Matt Mackall <mpm@selenic.com>
parents: 8263
diff changeset
   218
        if not fp:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   219
            fp = util.posixfile(path, b'rb')
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45894
diff changeset
   220
        assert (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45894
diff changeset
   221
            getattr(fp, 'mode', 'rb') == 'rb'
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45894
diff changeset
   222
        ), b'config files must be opened in binary mode, got fp=%r mode=%r' % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45894
diff changeset
   223
            fp,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45894
diff changeset
   224
            fp.mode,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   225
        )
45208
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
   226
45256
83ca8d6f3206 config: re-calculate absolute %include path in `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45254
diff changeset
   227
        dir = os.path.dirname(path)
83ca8d6f3206 config: re-calculate absolute %include path in `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45254
diff changeset
   228
45257
668af67bfd18 config: remove now-unused `abs` argument from `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45256
diff changeset
   229
        def include(rel, remap, sections):
45256
83ca8d6f3206 config: re-calculate absolute %include path in `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 45254
diff changeset
   230
            abs = os.path.normpath(os.path.join(dir, rel))
45208
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
   231
            self.read(abs, remap=remap, sections=sections)
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
   232
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   233
        self.parse(
45208
f7f142d74df3 config: pass both relative and absolute paths to `include` callback
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
   234
            path, fp.read(), sections=sections, remap=remap, include=include
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   235
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   236
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   237
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   238
def parselist(value):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   239
    """parse a configuration value as a list of comma/space separated strings
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   240
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 32318
diff changeset
   241
    >>> parselist(b'this,is "a small" ,test')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   242
    ['this', 'is', 'a small', 'test']
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   243
    """
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   244
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   245
    def _parse_plain(parts, s, offset):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   246
        whitespace = False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   247
        while offset < len(s) and (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   248
            s[offset : offset + 1].isspace() or s[offset : offset + 1] == b','
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   249
        ):
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   250
            whitespace = True
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   251
            offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   252
        if offset >= len(s):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   253
            return None, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   254
        if whitespace:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   255
            parts.append(b'')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   256
        if s[offset : offset + 1] == b'"' and not parts[-1]:
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   257
            return _parse_quote, parts, offset + 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   258
        elif s[offset : offset + 1] == b'"' and parts[-1][-1:] == b'\\':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   259
            parts[-1] = parts[-1][:-1] + s[offset : offset + 1]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   260
            return _parse_plain, parts, offset + 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   261
        parts[-1] += s[offset : offset + 1]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   262
        return _parse_plain, parts, offset + 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   263
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   264
    def _parse_quote(parts, s, offset):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   265
        if offset < len(s) and s[offset : offset + 1] == b'"':  # ""
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   266
            parts.append(b'')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   267
            offset += 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   268
            while offset < len(s) and (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   269
                s[offset : offset + 1].isspace()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   270
                or s[offset : offset + 1] == b','
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   271
            ):
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   272
                offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   273
            return _parse_plain, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   274
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   275
        while offset < len(s) and s[offset : offset + 1] != b'"':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   276
            if (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   277
                s[offset : offset + 1] == b'\\'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   278
                and offset + 1 < len(s)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   279
                and s[offset + 1 : offset + 2] == b'"'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   280
            ):
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   281
                offset += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   282
                parts[-1] += b'"'
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   283
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41317
diff changeset
   284
                parts[-1] += s[offset : offset + 1]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   285
            offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   286
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   287
        if offset >= len(s):
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   288
            real_parts = _configlist(parts[-1])
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   289
            if not real_parts:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   290
                parts[-1] = b'"'
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   291
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   292
                real_parts[0] = b'"' + real_parts[0]
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   293
                parts = parts[:-1]
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   294
                parts.extend(real_parts)
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   295
            return None, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   296
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   297
        offset += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   298
        while offset < len(s) and s[offset : offset + 1] in [b' ', b',']:
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   299
            offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   300
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   301
        if offset < len(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   302
            if offset + 1 == len(s) and s[offset : offset + 1] == b'"':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   303
                parts[-1] += b'"'
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   304
                offset += 1
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   305
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   306
                parts.append(b'')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   307
        else:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   308
            return None, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   309
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   310
        return _parse_plain, parts, offset
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   311
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   312
    def _configlist(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   313
        s = s.rstrip(b' ,')
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   314
        if not s:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   315
            return []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   316
        parser, parts, offset = _parse_plain, [b''], 0
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   317
        while parser:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   318
            parser, parts, offset = parser(parts, s, offset)
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   319
        return parts
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   320
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   321
    if value is not None and isinstance(value, bytes):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   322
        result = _configlist(value.lstrip(b' ,\n'))
31481
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   323
    else:
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   324
        result = value
a7c687c35119 ui: move configlist parser to config.py
Jun Wu <quark@fb.com>
parents: 31374
diff changeset
   325
    return result or []