author | Henrik Stuart <hg@hstuart.dk> |
Mon, 08 Sep 2014 20:57:44 +0200 | |
changeset 22403 | 41e9d58ec56f |
parent 22384 | 091306562855 |
child 22418 | 192c798aabee |
permissions | -rw-r--r-- |
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 |
|
22383
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
12 |
samplehgrcs = { |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
13 |
'user': |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
14 |
"""# example user config (see "hg help config" for more info) |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
15 |
[ui] |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
16 |
# name and email, e.g. |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
17 |
# username = Jane Doe <jdoe@example.com> |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
18 |
username = |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
19 |
|
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
20 |
[extensions] |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
21 |
# uncomment these lines to enable some popular extensions |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
22 |
# (see "hg help extensions" for more info) |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
23 |
# |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
24 |
# pager = |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
25 |
# progress = |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
26 |
# color =""", |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
27 |
|
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
28 |
'local': |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
29 |
"""# example repository config (see "hg help config" for more info) |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
30 |
""", |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
31 |
|
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
32 |
'global': |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
33 |
"""# example system-wide hg config (see "hg help config" for more info) |
22384
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
34 |
|
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
35 |
[extensions] |
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
36 |
# Uncomment these lines for some possible extensions |
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
37 |
# (see "hg help extensions" for more info) |
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
38 |
# |
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
39 |
# blackbox = |
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
40 |
# progress = |
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
41 |
# color = |
091306562855
config: propose some sample global config file
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22383
diff
changeset
|
42 |
# pager =""", |
22383
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
43 |
} |
f58b41f6708b
config: give more fine-tuned sample hgrcs to this command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
22037
diff
changeset
|
44 |
|
8186
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
45 |
class config(object): |
8144 | 46 |
def __init__(self, data=None): |
47 |
self._data = {} |
|
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
48 |
self._source = {} |
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
|
49 |
self._unset = [] |
8144 | 50 |
if data: |
51 |
for k in data._data: |
|
52 |
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
|
53 |
self._source = data._source.copy() |
8144 | 54 |
def copy(self): |
55 |
return config(self) |
|
56 |
def __contains__(self, section): |
|
57 |
return section in self._data |
|
8186
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
58 |
def __getitem__(self, section): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
59 |
return self._data.get(section, {}) |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
60 |
def __iter__(self): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
61 |
for d in self.sections(): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
62 |
yield d |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
63 |
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
|
64 |
for s, n in src._unset: |
7d82ad4b3727
config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17537
diff
changeset
|
65 |
if s in self and n in self._data[s]: |
7d82ad4b3727
config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17537
diff
changeset
|
66 |
del self._data[s][n] |
7d82ad4b3727
config: discard "%unset" values defined in the other files read in previously
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17537
diff
changeset
|
67 |
del self._source[(s, n)] |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
68 |
for s in src: |
8144 | 69 |
if s not in self: |
21813
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
20789
diff
changeset
|
70 |
self._data[s] = util.sortdict() |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
71 |
self._data[s].update(src._data[s]) |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
72 |
self._source.update(src._source) |
8144 | 73 |
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
|
74 |
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
|
75 |
|
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 |
def backup(self, section, item): |
17527 | 77 |
"""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
|
78 |
|
17530 | 79 |
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
|
80 |
""" |
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 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
|
8198
cf9accffd0b3
config: getsource -> source
Matt Mackall <mpm@selenic.com>
parents:
8193
diff
changeset
|
88 |
def source(self, section, item): |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
89 |
return self._source.get((section, item), "") |
8144 | 90 |
def sections(self): |
91 |
return sorted(self._data.keys()) |
|
92 |
def items(self, section): |
|
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
93 |
return self._data.get(section, {}).items() |
8144 | 94 |
def set(self, section, item, value, source=""): |
95 |
if section not in self: |
|
21813
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
20789
diff
changeset
|
96 |
self._data[section] = util.sortdict() |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
97 |
self._data[section][item] = value |
20789
d19c9bdbbf35
config: don't set source when no source is specified - don't overwrite with ''
Mads Kiilerich <madski@unity3d.com>
parents:
19087
diff
changeset
|
98 |
if source: |
d19c9bdbbf35
config: don't set source when no source is specified - don't overwrite with ''
Mads Kiilerich <madski@unity3d.com>
parents:
19087
diff
changeset
|
99 |
self._source[(section, item)] = source |
8144 | 100 |
|
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
|
101 |
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
|
102 |
"""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
|
103 |
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
|
104 |
# 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
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
# 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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
|
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
115 |
def parse(self, src, data, sections=None, remap=None, include=None): |
21910
fc04fdb2b349
config: use util.re.compile instead of util.compilere
Siddharth Agarwal <sid0@fb.com>
parents:
21813
diff
changeset
|
116 |
sectionre = util.re.compile(r'\[([^\[]+)\]') |
fc04fdb2b349
config: use util.re.compile instead of util.compilere
Siddharth Agarwal <sid0@fb.com>
parents:
21813
diff
changeset
|
117 |
itemre = util.re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') |
fc04fdb2b349
config: use util.re.compile instead of util.compilere
Siddharth Agarwal <sid0@fb.com>
parents:
21813
diff
changeset
|
118 |
contre = util.re.compile(r'\s+(\S|\S.*\S)\s*$') |
fc04fdb2b349
config: use util.re.compile instead of util.compilere
Siddharth Agarwal <sid0@fb.com>
parents:
21813
diff
changeset
|
119 |
emptyre = util.re.compile(r'(;|#|\s*$)') |
fc04fdb2b349
config: use util.re.compile instead of util.compilere
Siddharth Agarwal <sid0@fb.com>
parents:
21813
diff
changeset
|
120 |
commentre = util.re.compile(r'(;|#)') |
fc04fdb2b349
config: use util.re.compile instead of util.compilere
Siddharth Agarwal <sid0@fb.com>
parents:
21813
diff
changeset
|
121 |
unsetre = util.re.compile(r'%unset\s+(\S+)') |
fc04fdb2b349
config: use util.re.compile instead of util.compilere
Siddharth Agarwal <sid0@fb.com>
parents:
21813
diff
changeset
|
122 |
includere = util.re.compile(r'%include\s+(\S|\S.*\S)\s*$') |
8144 | 123 |
section = "" |
124 |
item = None |
|
125 |
line = 0 |
|
9339
9be91129c96e
config: improve code readability
Andrey <py4fun@gmail.com>
parents:
9136
diff
changeset
|
126 |
cont = False |
8180 | 127 |
|
9136
31177742f54a
for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8312
diff
changeset
|
128 |
for l in data.splitlines(True): |
8144 | 129 |
line += 1 |
16348
f350021ee32e
config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents:
15919
diff
changeset
|
130 |
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
|
131 |
# Someone set us up the BOM |
f350021ee32e
config: discard UTF-8 BOM if found
Matt Mackall <mpm@selenic.com>
parents:
15919
diff
changeset
|
132 |
l = l[3:] |
8144 | 133 |
if cont: |
14642
fdcdb221a922
config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents:
14486
diff
changeset
|
134 |
if commentre.match(l): |
fdcdb221a922
config: handle comment lines in continuations (issue2854)
Matt Mackall <mpm@selenic.com>
parents:
14486
diff
changeset
|
135 |
continue |
8144 | 136 |
m = contre.match(l) |
137 |
if m: |
|
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
138 |
if sections and section not in sections: |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
139 |
continue |
8144 | 140 |
v = self.get(section, item) + "\n" + m.group(1) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
141 |
self.set(section, item, v, "%s:%d" % (src, line)) |
8144 | 142 |
continue |
143 |
item = None |
|
9469
7f0f882af23d
config: abort on indented non-continuation lines (issue1829)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8312
diff
changeset
|
144 |
cont = False |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
145 |
m = includere.match(l) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
146 |
if m: |
11224
f23f87462c18
config: expand hgrc %include paths
Chad Dombrova <chadrik@gmail.com>
parents:
10295
diff
changeset
|
147 |
inc = util.expandpath(m.group(1)) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
148 |
base = os.path.dirname(src) |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
149 |
inc = os.path.normpath(os.path.join(base, inc)) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
150 |
if include: |
10042
7cdd2a7db2c2
config: raise ConfigError on non-existing include files
Martin Geisler <mg@lazybytes.net>
parents:
9471
diff
changeset
|
151 |
try: |
7cdd2a7db2c2
config: raise ConfigError on non-existing include files
Martin Geisler <mg@lazybytes.net>
parents:
9471
diff
changeset
|
152 |
include(inc, remap=remap, sections=sections) |
7cdd2a7db2c2
config: raise ConfigError on non-existing include files
Martin Geisler <mg@lazybytes.net>
parents:
9471
diff
changeset
|
153 |
except IOError, inst: |
14486
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
154 |
if inst.errno != errno.ENOENT: |
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
155 |
raise error.ParseError(_("cannot include %s (%s)") |
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
156 |
% (inc, inst.strerror), |
4e3eda05189b
config: ignore include errors for nonexistent files
Matt Mackall <mpm@selenic.com>
parents:
13664
diff
changeset
|
157 |
"%s:%s" % (src, line)) |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
158 |
continue |
8144 | 159 |
if emptyre.match(l): |
160 |
continue |
|
161 |
m = sectionre.match(l) |
|
162 |
if m: |
|
163 |
section = m.group(1) |
|
8298
9542f4c3fa1b
config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents:
8265
diff
changeset
|
164 |
if remap: |
9542f4c3fa1b
config: make remap actually work
Matt Mackall <mpm@selenic.com>
parents:
8265
diff
changeset
|
165 |
section = remap.get(section, section) |
8144 | 166 |
if section not in self: |
21813
c2262004c2e2
config: move config.sortdict class into util
Angel Ezquerra <angel.ezquerra@gmail.com>
parents:
20789
diff
changeset
|
167 |
self._data[section] = util.sortdict() |
8144 | 168 |
continue |
169 |
m = itemre.match(l) |
|
170 |
if m: |
|
171 |
item = m.group(1) |
|
9339
9be91129c96e
config: improve code readability
Andrey <py4fun@gmail.com>
parents:
9136
diff
changeset
|
172 |
cont = True |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
173 |
if sections and section not in sections: |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
174 |
continue |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
175 |
self.set(section, item, m.group(2), "%s:%d" % (src, line)) |
8144 | 176 |
continue |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
177 |
m = unsetre.match(l) |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
178 |
if m: |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
179 |
name = m.group(1) |
8193
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
180 |
if sections and section not in sections: |
94246e90081e
config: add section filter to read
Matt Mackall <mpm@selenic.com>
parents:
8192
diff
changeset
|
181 |
continue |
13031
3da456d0c885
code style: prefer 'is' and 'is not' tests with singletons
Martin Geisler <mg@aragost.com>
parents:
11292
diff
changeset
|
182 |
if self.get(section, name) is not None: |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
183 |
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
|
184 |
self._unset.append((section, name)) |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
185 |
continue |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
186 |
|
11288
2123aad24d56
error: add new ParseError for various parsing errors
Matt Mackall <mpm@selenic.com>
parents:
11224
diff
changeset
|
187 |
raise error.ParseError(l.rstrip(), ("%s:%s" % (src, line))) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
188 |
|
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
189 |
def read(self, path, fp=None, sections=None, remap=None): |
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
190 |
if not fp: |
13664
53db4e2026ab
config: use util.posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents:
13031
diff
changeset
|
191 |
fp = util.posixfile(path) |
8265
52c5be55af82
config: add parse interface
Matt Mackall <mpm@selenic.com>
parents:
8263
diff
changeset
|
192 |
self.parse(path, fp.read(), sections, remap, self.read) |