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