Mercurial > hg
annotate mercurial/config.py @ 18758:6aca4d1c744e
blackbox: fix exception when logging commands with format characters
When running commands like 'hg export -o mypatch-%N.patch', the blackbox
would throw an exception because it tried to format %N. This change
prevents it from trying to format the command string.
author | Durham Goode <durham@fb.com> |
---|---|
date | Thu, 28 Feb 2013 10:12:26 -0800 |
parents | 31f32a96e1e3 |
children | 7d82ad4b3727 |
rev | line source |
---|---|
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 | 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 |
8144 | 8 from i18n import _ |
11224
f23f87462c18
config: expand hgrc %include paths
Chad Dombrova <chadrik@gmail.com>
parents:
10295
diff
changeset
|
9 import error, util |
16944
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
10 import os, errno |
8144 | 11 |
12 class sortdict(dict): | |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
13 'a simple sorted dictionary' |
8144 | 14 def __init__(self, data=None): |
15 self._list = [] | |
16 if data: | |
17 self.update(data) | |
18 def copy(self): | |
19 return sortdict(self) | |
20 def __setitem__(self, key, val): | |
21 if key in self: | |
22 self._list.remove(key) | |
23 self._list.append(key) | |
24 dict.__setitem__(self, key, val) | |
25 def __iter__(self): | |
26 return self._list.__iter__() | |
27 def update(self, src): | |
28 for k in src: | |
29 self[k] = src[k] | |
14696
5fb3cb7266e5
config.sortdict: override clear method
Sune Foldager <cryo@cyanite.org>
parents:
14659
diff
changeset
|
30 def clear(self): |
5fb3cb7266e5
config.sortdict: override clear method
Sune Foldager <cryo@cyanite.org>
parents:
14659
diff
changeset
|
31 dict.clear(self) |
5fb3cb7266e5
config.sortdict: override clear method
Sune Foldager <cryo@cyanite.org>
parents:
14659
diff
changeset
|
32 self._list = [] |
8144 | 33 def items(self): |
8222
d30a21594812
more whitespace cleanup and some other style nits
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8198
diff
changeset
|
34 return [(k, self[k]) for k in self._list] |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
35 def __delitem__(self, key): |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
36 dict.__delitem__(self, key) |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
37 self._list.remove(key) |
16865
a6543fdcf869
config: make sortdict keys() and iterkeys() methods respect the item order
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
16348
diff
changeset
|
38 def keys(self): |
a6543fdcf869
config: make sortdict keys() and iterkeys() methods respect the item order
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
16348
diff
changeset
|
39 return self._list |
a6543fdcf869
config: make sortdict keys() and iterkeys() methods respect the item order
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
16348
diff
changeset
|
40 def iterkeys(self): |
a6543fdcf869
config: make sortdict keys() and iterkeys() methods respect the item order
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
16348
diff
changeset
|
41 return self._list.__iter__() |
8144 | 42 |
8186
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
43 class config(object): |
8144 | 44 def __init__(self, data=None): |
45 self._data = {} | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
46 self._source = {} |
8144 | 47 if data: |
48 for k in data._data: | |
49 self._data[k] = data[k].copy() | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
50 self._source = data._source.copy() |
8144 | 51 def copy(self): |
52 return config(self) | |
53 def __contains__(self, section): | |
54 return section in self._data | |
8186
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
55 def __getitem__(self, section): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
56 return self._data.get(section, {}) |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
57 def __iter__(self): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
58 for d in self.sections(): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
59 yield d |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
60 def update(self, src): |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
61 for s in src: |
8144 | 62 if s not in self: |
63 self._data[s] = sortdict() | |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
64 self._data[s].update(src._data[s]) |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
65 self._source.update(src._source) |
8144 | 66 def get(self, section, item, default=None): |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
67 return self._data.get(section, {}).get(item, default) |
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
|
68 |
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 def backup(self, section, item): |
17527 | 70 """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
|
71 |
17530 | 72 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
|
73 """ |
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 try: |
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 value = self._data[section][item] |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
76 source = self.source(section, item) |
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 return (section, item, value, source) |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
78 except KeyError: |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
79 return (section, item) |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
80 |
8198
cf9accffd0b3
config: getsource -> source
Matt Mackall <mpm@selenic.com>
parents:
8193
diff
changeset
|
81 def source(self, section, item): |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
82 return self._source.get((section, item), "") |
8144 | 83 def sections(self): |
84 return sorted(self._data.keys()) | |
85 def items(self, section): | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
86 return self._data.get(section, {}).items() |
8144 | 87 def set(self, section, item, value, source=""): |
88 if section not in self: | |
89 self._data[section] = sortdict() | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
90 self._data[section][item] = value |
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
91 self._source[(section, item)] = source |
8144 | 92 |
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
|
93 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
|
94 """restore data returned by self.backup""" |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
95 if len(data) == 4: |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
96 # restore old 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
|
97 section, item, value, source = 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
|
98 self._data[section][item] = value |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
99 self._source[(section, item)] = source |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
100 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
|
101 # 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
|
102 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
|
103 if section in 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
|
104 del self._data[section][item] |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
105 self._source.pop((section, item), None) |
69e792cf7851
config: have a way to backup and restore value in config
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
14696
diff
changeset
|
106 |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
107 def parse(self, src, data, sections=None, remap=None, include=None): |
16944
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
108 sectionre = util.compilere(r'\[([^\[]+)\]') |
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
109 itemre = util.compilere(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') |
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
110 contre = util.compilere(r'\s+(\S|\S.*\S)\s*$') |
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
111 emptyre = util.compilere(r'(;|#|\s*$)') |
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
112 commentre = util.compilere(r'(;|#)') |
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
113 unsetre = util.compilere(r'%unset\s+(\S+)') |
5d3d77b3c512
config: use util.compilere to compile regexps
Bryan O'Sullivan <bryano@fb.com>
parents:
16865
diff
changeset
|
114 includere = util.compilere(r'%include\s+(\S|\S.*\S)\s*$') |
8144 | 115 section = "" |
116 item = None | |
117 line = 0 | |
9339
9be91129c96e
config: improve code readability
Andrey <py4fun@gmail.com>
parents:
9136
diff
changeset
|
118 cont = False |
8180 | 119 |
9136
31177742f54a
for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8312
diff
changeset
|
120 for l in data.splitlines(True): |
8144 | 121 line += 1 |
16348
f350021ee32e
config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents:
15919
diff
changeset
|
122 if line == 1 and l.startswith('\xef\xbb\xbf'): |
f350021ee32e
config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents:
15919
diff
changeset
|
123 # Someone set us up the BOM |
f350021ee32e
config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents:
15919
diff
changeset
|
124 l = l[3:] |
8144 | 125 if cont: |
14642
fdcdb221a922
config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents:
14486
diff
changeset
|
126 if commentre.match(l): |
fdcdb221a922
config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents:
14486
diff
changeset
|
127 continue |
8144 | 128 m = contre.match(l) |
129 if m: | |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
130 if sections and section not in sections: |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
131 continue |
8144 | 132 v = self.get(section, item) + "\n" + m.group(1) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
133 self.set(section, item, v, "%s:%d" % (src, line)) |
8144 | 134 continue |
135 item = None | |
9469
7f0f882af23d
config: abort on indented non-continuation lines (issue1829)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8312
diff
changeset
|
136 cont = False |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
137 m = includere.match(l) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
138 if m: |
11224
f23f87462c18
config: expand hgrc %include paths
Chad Dombrova <chadrik@gmail.com>
parents:
10295
diff
changeset
|
139 inc = util.expandpath(m.group(1)) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
140 base = os.path.dirname(src) |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
141 inc = os.path.normpath(os.path.join(base, inc)) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
142 if include: |
10042
7cdd2a7db2c2
config: raise ConfigError on non-existing include files
Martin Geisler <mg@lazybytes.net>
parents:
9471
diff
changeset
|
143 try: |
7cdd2a7db2c2
config: raise ConfigError on non-existing include files
Martin Geisler <mg@lazybytes.net>
parents:
9471
diff
changeset
|
144 include(inc, remap=remap, sections=sections) |
7cdd2a7db2c2
config: raise ConfigError on non-existing include files
Martin Geisler <mg@lazybytes.net>
parents:
9471
diff
changeset
|
145 except IOError, inst: |
14486
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
146 if inst.errno != errno.ENOENT: |
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
147 raise error.ParseError(_("cannot include %s (%s)") |
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
148 % (inc, inst.strerror), |
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
149 "%s:%s" % (src, line)) |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
150 continue |
8144 | 151 if emptyre.match(l): |
152 continue | |
153 m = sectionre.match(l) | |
154 if m: | |
155 section = m.group(1) | |
8298
9542f4c3fa1b
config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents:
8265
diff
changeset
|
156 if remap: |
9542f4c3fa1b
config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents:
8265
diff
changeset
|
157 section = remap.get(section, section) |
8144 | 158 if section not in self: |
159 self._data[section] = sortdict() | |
160 continue | |
161 m = itemre.match(l) | |
162 if m: | |
163 item = m.group(1) | |
9339
9be91129c96e
config: improve code readability
Andrey <py4fun@gmail.com>
parents:
9136
diff
changeset
|
164 cont = True |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
165 if sections and section not in sections: |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
166 continue |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
167 self.set(section, item, m.group(2), "%s:%d" % (src, line)) |
8144 | 168 continue |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
169 m = unsetre.match(l) |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
170 if m: |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
171 name = m.group(1) |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
172 if sections and section not in sections: |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
173 continue |
13031
3da456d0c885
code style: prefer 'is' and 'is not' tests with singletons
Martin Geisler <mg@aragost.com>
parents:
11292
diff
changeset
|
174 if self.get(section, name) is not None: |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
175 del self._data[section][name] |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
176 continue |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
177 |
11288
2123aad24d56
error: add new ParseError for various parsing errors
Matt Mackall <mpm@selenic.com>
parents:
11224
diff
changeset
|
178 raise error.ParseError(l.rstrip(), ("%s:%s" % (src, line))) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
179 |
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
180 def read(self, path, fp=None, sections=None, remap=None): |
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
181 if not fp: |
13664
53db4e2026ab
config: use util.posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
13031
diff
changeset
|
182 fp = util.posixfile(path) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
183 self.parse(path, fp.read(), sections, remap, self.read) |