Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/config.py @ 8186:6a0018cdb2fe
config: add some helper methods
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Sun, 26 Apr 2009 16:50:43 -0500 |
parents | dc10a7a3f1d4 |
children | d2504744e7a5 |
rev | line source |
---|---|
8144 | 1 from i18n import _ |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
2 import re, error, os |
8144 | 3 |
4 class sortdict(dict): | |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
5 'a simple sorted dictionary' |
8144 | 6 def __init__(self, data=None): |
7 self._list = [] | |
8 if data: | |
9 if hasattr(data, '_list'): | |
10 self._list = list(data._list) | |
11 self.update(data) | |
12 def copy(self): | |
13 return sortdict(self) | |
14 def __setitem__(self, key, val): | |
15 if key in self: | |
16 self._list.remove(key) | |
17 self._list.append(key) | |
18 dict.__setitem__(self, key, val) | |
19 def __iter__(self): | |
20 return self._list.__iter__() | |
21 def update(self, src): | |
22 for k in src: | |
23 self[k] = src[k] | |
24 def items(self): | |
25 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
|
26 def __delitem__(self, key): |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
27 dict.__delitem__(self, key) |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
28 self._list.remove(key) |
8144 | 29 |
8186
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
30 class config(object): |
8144 | 31 def __init__(self, data=None): |
32 self._data = {} | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
33 self._source = {} |
8144 | 34 if data: |
35 for k in data._data: | |
36 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
|
37 self._source = data._source.copy() |
8144 | 38 def copy(self): |
39 return config(self) | |
40 def __contains__(self, section): | |
41 return section in self._data | |
8186
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
42 def __getitem__(self, section): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
43 return self._data.get(section, {}) |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
44 def __iter__(self): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
45 for d in self.sections(): |
6a0018cdb2fe
config: add some helper methods
Matt Mackall <mpm@selenic.com>
parents:
8185
diff
changeset
|
46 yield d |
8144 | 47 def update(self, src, sections=None): |
48 if not sections: | |
49 sections = src.sections() | |
50 for s in sections: | |
51 if s not in src: | |
52 continue | |
53 if s not in self: | |
54 self._data[s] = sortdict() | |
55 for k in src._data[s]: | |
56 self._data[s][k] = src._data[s][k] | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
57 self._source[(s, k)] = src._source[(s, k)] |
8144 | 58 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
|
59 return self._data.get(section, {}).get(item, default) |
8144 | 60 def getsource(self, section, item): |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
61 return self._source.get((section, item), "") |
8144 | 62 def sections(self): |
63 return sorted(self._data.keys()) | |
64 def items(self, section): | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
65 return self._data.get(section, {}).items() |
8144 | 66 def set(self, section, item, value, source=""): |
67 if section not in self: | |
68 self._data[section] = sortdict() | |
8185
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
69 self._data[section][item] = value |
dc10a7a3f1d4
config: split source data out into separate map
Matt Mackall <mpm@selenic.com>
parents:
8184
diff
changeset
|
70 self._source[(section, item)] = source |
8144 | 71 |
8180 | 72 def read(self, path, fp=None): |
8144 | 73 sectionre = re.compile(r'\[([^\[]+)\]') |
74 itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)') | |
75 contre = re.compile(r'\s+(\S.*)') | |
76 emptyre = re.compile(r'(;|#|\s*$)') | |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
77 unsetre = re.compile(r'%unset\s+(\S.*)') |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
78 includere = re.compile(r'%include\s+(\S.*)') |
8144 | 79 section = "" |
80 item = None | |
81 line = 0 | |
82 cont = 0 | |
8180 | 83 |
84 if not fp: | |
85 fp = open(path) | |
86 | |
8144 | 87 for l in fp: |
88 line += 1 | |
89 if cont: | |
90 m = contre.match(l) | |
91 if m: | |
92 v = self.get(section, item) + "\n" + m.group(1) | |
93 self.set(section, item, v, "%s:%d" % (path, line)) | |
94 continue | |
95 item = None | |
8183
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
96 m = includere.match(l) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
97 if m: |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
98 inc = m.group(1) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
99 base = os.path.dirname(path) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
100 inc = os.path.normpath(os.path.join(base, inc)) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
101 incfp = open(inc) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
102 self.read(inc, incfp) |
2858ab754995
config: allow including other config files
Matt Mackall <mpm@selenic.com>
parents:
8180
diff
changeset
|
103 continue |
8144 | 104 if emptyre.match(l): |
105 continue | |
106 m = sectionre.match(l) | |
107 if m: | |
108 section = m.group(1) | |
109 if section not in self: | |
110 self._data[section] = sortdict() | |
111 continue | |
112 m = itemre.match(l) | |
113 if m: | |
114 item = m.group(1) | |
115 self.set(section, item, m.group(2), "%s:%d" % (path, line)) | |
116 cont = 1 | |
117 continue | |
8184
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
118 m = unsetre.match(l) |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
119 if m: |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
120 name = m.group(1) |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
121 if self.get(section, name) != None: |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
122 del self._data[section][name] |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
123 continue |
9189afe1eba3
config: add %unset name support
Matt Mackall <mpm@selenic.com>
parents:
8183
diff
changeset
|
124 |
8144 | 125 raise error.ConfigError(_('config error at %s:%d: \'%s\'') |
126 % (path, line, l.rstrip())) |