Show More
@@ -1,142 +1,143 | |||
|
1 | 1 | # config.py - configuration parsing for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from i18n import _ |
|
9 | 9 | import error, util |
|
10 | import re, os | |
|
10 | import re, os, errno | |
|
11 | 11 | |
|
12 | 12 | class sortdict(dict): |
|
13 | 13 | 'a simple sorted dictionary' |
|
14 | 14 | def __init__(self, data=None): |
|
15 | 15 | self._list = [] |
|
16 | 16 | if data: |
|
17 | 17 | self.update(data) |
|
18 | 18 | def copy(self): |
|
19 | 19 | return sortdict(self) |
|
20 | 20 | def __setitem__(self, key, val): |
|
21 | 21 | if key in self: |
|
22 | 22 | self._list.remove(key) |
|
23 | 23 | self._list.append(key) |
|
24 | 24 | dict.__setitem__(self, key, val) |
|
25 | 25 | def __iter__(self): |
|
26 | 26 | return self._list.__iter__() |
|
27 | 27 | def update(self, src): |
|
28 | 28 | for k in src: |
|
29 | 29 | self[k] = src[k] |
|
30 | 30 | def items(self): |
|
31 | 31 | return [(k, self[k]) for k in self._list] |
|
32 | 32 | def __delitem__(self, key): |
|
33 | 33 | dict.__delitem__(self, key) |
|
34 | 34 | self._list.remove(key) |
|
35 | 35 | |
|
36 | 36 | class config(object): |
|
37 | 37 | def __init__(self, data=None): |
|
38 | 38 | self._data = {} |
|
39 | 39 | self._source = {} |
|
40 | 40 | if data: |
|
41 | 41 | for k in data._data: |
|
42 | 42 | self._data[k] = data[k].copy() |
|
43 | 43 | self._source = data._source.copy() |
|
44 | 44 | def copy(self): |
|
45 | 45 | return config(self) |
|
46 | 46 | def __contains__(self, section): |
|
47 | 47 | return section in self._data |
|
48 | 48 | def __getitem__(self, section): |
|
49 | 49 | return self._data.get(section, {}) |
|
50 | 50 | def __iter__(self): |
|
51 | 51 | for d in self.sections(): |
|
52 | 52 | yield d |
|
53 | 53 | def update(self, src): |
|
54 | 54 | for s in src: |
|
55 | 55 | if s not in self: |
|
56 | 56 | self._data[s] = sortdict() |
|
57 | 57 | self._data[s].update(src._data[s]) |
|
58 | 58 | self._source.update(src._source) |
|
59 | 59 | def get(self, section, item, default=None): |
|
60 | 60 | return self._data.get(section, {}).get(item, default) |
|
61 | 61 | def source(self, section, item): |
|
62 | 62 | return self._source.get((section, item), "") |
|
63 | 63 | def sections(self): |
|
64 | 64 | return sorted(self._data.keys()) |
|
65 | 65 | def items(self, section): |
|
66 | 66 | return self._data.get(section, {}).items() |
|
67 | 67 | def set(self, section, item, value, source=""): |
|
68 | 68 | if section not in self: |
|
69 | 69 | self._data[section] = sortdict() |
|
70 | 70 | self._data[section][item] = value |
|
71 | 71 | self._source[(section, item)] = source |
|
72 | 72 | |
|
73 | 73 | def parse(self, src, data, sections=None, remap=None, include=None): |
|
74 | 74 | sectionre = re.compile(r'\[([^\[]+)\]') |
|
75 | 75 | itemre = re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') |
|
76 | 76 | contre = re.compile(r'\s+(\S|\S.*\S)\s*$') |
|
77 | 77 | emptyre = re.compile(r'(;|#|\s*$)') |
|
78 | 78 | unsetre = re.compile(r'%unset\s+(\S+)') |
|
79 | 79 | includere = re.compile(r'%include\s+(\S|\S.*\S)\s*$') |
|
80 | 80 | section = "" |
|
81 | 81 | item = None |
|
82 | 82 | line = 0 |
|
83 | 83 | cont = False |
|
84 | 84 | |
|
85 | 85 | for l in data.splitlines(True): |
|
86 | 86 | line += 1 |
|
87 | 87 | if cont: |
|
88 | 88 | m = contre.match(l) |
|
89 | 89 | if m: |
|
90 | 90 | if sections and section not in sections: |
|
91 | 91 | continue |
|
92 | 92 | v = self.get(section, item) + "\n" + m.group(1) |
|
93 | 93 | self.set(section, item, v, "%s:%d" % (src, line)) |
|
94 | 94 | continue |
|
95 | 95 | item = None |
|
96 | 96 | cont = False |
|
97 | 97 | m = includere.match(l) |
|
98 | 98 | if m: |
|
99 | 99 | inc = util.expandpath(m.group(1)) |
|
100 | 100 | base = os.path.dirname(src) |
|
101 | 101 | inc = os.path.normpath(os.path.join(base, inc)) |
|
102 | 102 | if include: |
|
103 | 103 | try: |
|
104 | 104 | include(inc, remap=remap, sections=sections) |
|
105 | 105 | except IOError, inst: |
|
106 | raise error.ParseError(_("cannot include %s (%s)") | |
|
107 | % (inc, inst.strerror), | |
|
108 |
|
|
|
106 | if inst.errno != errno.ENOENT: | |
|
107 | raise error.ParseError(_("cannot include %s (%s)") | |
|
108 | % (inc, inst.strerror), | |
|
109 | "%s:%s" % (src, line)) | |
|
109 | 110 | continue |
|
110 | 111 | if emptyre.match(l): |
|
111 | 112 | continue |
|
112 | 113 | m = sectionre.match(l) |
|
113 | 114 | if m: |
|
114 | 115 | section = m.group(1) |
|
115 | 116 | if remap: |
|
116 | 117 | section = remap.get(section, section) |
|
117 | 118 | if section not in self: |
|
118 | 119 | self._data[section] = sortdict() |
|
119 | 120 | continue |
|
120 | 121 | m = itemre.match(l) |
|
121 | 122 | if m: |
|
122 | 123 | item = m.group(1) |
|
123 | 124 | cont = True |
|
124 | 125 | if sections and section not in sections: |
|
125 | 126 | continue |
|
126 | 127 | self.set(section, item, m.group(2), "%s:%d" % (src, line)) |
|
127 | 128 | continue |
|
128 | 129 | m = unsetre.match(l) |
|
129 | 130 | if m: |
|
130 | 131 | name = m.group(1) |
|
131 | 132 | if sections and section not in sections: |
|
132 | 133 | continue |
|
133 | 134 | if self.get(section, name) is not None: |
|
134 | 135 | del self._data[section][name] |
|
135 | 136 | continue |
|
136 | 137 | |
|
137 | 138 | raise error.ParseError(l.rstrip(), ("%s:%s" % (src, line))) |
|
138 | 139 | |
|
139 | 140 | def read(self, path, fp=None, sections=None, remap=None): |
|
140 | 141 | if not fp: |
|
141 | 142 | fp = util.posixfile(path) |
|
142 | 143 | self.parse(path, fp.read(), sections, remap, self.read) |
General Comments 0
You need to be logged in to leave comments.
Login now