Show More
@@ -1,117 +1,121 | |||||
1 | from i18n import _ |
|
1 | from i18n import _ | |
2 | import re, error, os |
|
2 | import re, error, os | |
3 |
|
3 | |||
4 | class sortdict(dict): |
|
4 | class sortdict(dict): | |
5 | 'a simple sorted dictionary' |
|
5 | 'a simple sorted dictionary' | |
6 | def __init__(self, data=None): |
|
6 | def __init__(self, data=None): | |
7 | self._list = [] |
|
7 | self._list = [] | |
8 | if data: |
|
8 | if data: | |
9 | if hasattr(data, '_list'): |
|
9 | if hasattr(data, '_list'): | |
10 | self._list = list(data._list) |
|
10 | self._list = list(data._list) | |
11 | self.update(data) |
|
11 | self.update(data) | |
12 | def copy(self): |
|
12 | def copy(self): | |
13 | return sortdict(self) |
|
13 | return sortdict(self) | |
14 | def __setitem__(self, key, val): |
|
14 | def __setitem__(self, key, val): | |
15 | if key in self: |
|
15 | if key in self: | |
16 | self._list.remove(key) |
|
16 | self._list.remove(key) | |
17 | self._list.append(key) |
|
17 | self._list.append(key) | |
18 | dict.__setitem__(self, key, val) |
|
18 | dict.__setitem__(self, key, val) | |
19 | def __iter__(self): |
|
19 | def __iter__(self): | |
20 | return self._list.__iter__() |
|
20 | return self._list.__iter__() | |
21 | def update(self, src): |
|
21 | def update(self, src): | |
22 | for k in src: |
|
22 | for k in src: | |
23 | self[k] = src[k] |
|
23 | self[k] = src[k] | |
24 | def items(self): |
|
24 | def items(self): | |
25 | return [(k,self[k]) for k in self._list] |
|
25 | return [(k,self[k]) for k in self._list] | |
26 | def __delitem__(self, key): |
|
26 | def __delitem__(self, key): | |
27 | dict.__delitem__(self, key) |
|
27 | dict.__delitem__(self, key) | |
28 | self._list.remove(key) |
|
28 | self._list.remove(key) | |
29 |
|
29 | |||
30 | class config: |
|
30 | class config: | |
31 | def __init__(self, data=None): |
|
31 | def __init__(self, data=None): | |
32 | self._data = {} |
|
32 | self._data = {} | |
|
33 | self._source = {} | |||
33 | if data: |
|
34 | if data: | |
34 | for k in data._data: |
|
35 | for k in data._data: | |
35 | self._data[k] = data[k].copy() |
|
36 | self._data[k] = data[k].copy() | |
|
37 | self._source = data._source.copy() | |||
36 | def copy(self): |
|
38 | def copy(self): | |
37 | return config(self) |
|
39 | return config(self) | |
38 | def __contains__(self, section): |
|
40 | def __contains__(self, section): | |
39 | return section in self._data |
|
41 | return section in self._data | |
40 | def update(self, src, sections=None): |
|
42 | def update(self, src, sections=None): | |
41 | if not sections: |
|
43 | if not sections: | |
42 | sections = src.sections() |
|
44 | sections = src.sections() | |
43 | for s in sections: |
|
45 | for s in sections: | |
44 | if s not in src: |
|
46 | if s not in src: | |
45 | continue |
|
47 | continue | |
46 | if s not in self: |
|
48 | if s not in self: | |
47 | self._data[s] = sortdict() |
|
49 | self._data[s] = sortdict() | |
48 | for k in src._data[s]: |
|
50 | for k in src._data[s]: | |
49 | self._data[s][k] = src._data[s][k] |
|
51 | self._data[s][k] = src._data[s][k] | |
|
52 | self._source[(s, k)] = src._source[(s, k)] | |||
50 | def get(self, section, item, default=None): |
|
53 | def get(self, section, item, default=None): | |
51 |
return self._data.get(section, {}).get(item, |
|
54 | return self._data.get(section, {}).get(item, default) | |
52 | def getsource(self, section, item): |
|
55 | def getsource(self, section, item): | |
53 |
return self._ |
|
56 | return self._source.get((section, item), "") | |
54 | def sections(self): |
|
57 | def sections(self): | |
55 | return sorted(self._data.keys()) |
|
58 | return sorted(self._data.keys()) | |
56 | def items(self, section): |
|
59 | def items(self, section): | |
57 |
return |
|
60 | return self._data.get(section, {}).items() | |
58 | def set(self, section, item, value, source=""): |
|
61 | def set(self, section, item, value, source=""): | |
59 | if section not in self: |
|
62 | if section not in self: | |
60 | self._data[section] = sortdict() |
|
63 | self._data[section] = sortdict() | |
61 |
self._data[section][item] = |
|
64 | self._data[section][item] = value | |
|
65 | self._source[(section, item)] = source | |||
62 |
|
66 | |||
63 | def read(self, path, fp=None): |
|
67 | def read(self, path, fp=None): | |
64 | sectionre = re.compile(r'\[([^\[]+)\]') |
|
68 | sectionre = re.compile(r'\[([^\[]+)\]') | |
65 | itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)') |
|
69 | itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)') | |
66 | contre = re.compile(r'\s+(\S.*)') |
|
70 | contre = re.compile(r'\s+(\S.*)') | |
67 | emptyre = re.compile(r'(;|#|\s*$)') |
|
71 | emptyre = re.compile(r'(;|#|\s*$)') | |
68 | unsetre = re.compile(r'%unset\s+(\S.*)') |
|
72 | unsetre = re.compile(r'%unset\s+(\S.*)') | |
69 | includere = re.compile(r'%include\s+(\S.*)') |
|
73 | includere = re.compile(r'%include\s+(\S.*)') | |
70 | section = "" |
|
74 | section = "" | |
71 | item = None |
|
75 | item = None | |
72 | line = 0 |
|
76 | line = 0 | |
73 | cont = 0 |
|
77 | cont = 0 | |
74 |
|
78 | |||
75 | if not fp: |
|
79 | if not fp: | |
76 | fp = open(path) |
|
80 | fp = open(path) | |
77 |
|
81 | |||
78 | for l in fp: |
|
82 | for l in fp: | |
79 | line += 1 |
|
83 | line += 1 | |
80 | if cont: |
|
84 | if cont: | |
81 | m = contre.match(l) |
|
85 | m = contre.match(l) | |
82 | if m: |
|
86 | if m: | |
83 | v = self.get(section, item) + "\n" + m.group(1) |
|
87 | v = self.get(section, item) + "\n" + m.group(1) | |
84 | self.set(section, item, v, "%s:%d" % (path, line)) |
|
88 | self.set(section, item, v, "%s:%d" % (path, line)) | |
85 | continue |
|
89 | continue | |
86 | item = None |
|
90 | item = None | |
87 | m = includere.match(l) |
|
91 | m = includere.match(l) | |
88 | if m: |
|
92 | if m: | |
89 | inc = m.group(1) |
|
93 | inc = m.group(1) | |
90 | base = os.path.dirname(path) |
|
94 | base = os.path.dirname(path) | |
91 | inc = os.path.normpath(os.path.join(base, inc)) |
|
95 | inc = os.path.normpath(os.path.join(base, inc)) | |
92 | incfp = open(inc) |
|
96 | incfp = open(inc) | |
93 | self.read(inc, incfp) |
|
97 | self.read(inc, incfp) | |
94 | continue |
|
98 | continue | |
95 | if emptyre.match(l): |
|
99 | if emptyre.match(l): | |
96 | continue |
|
100 | continue | |
97 | m = sectionre.match(l) |
|
101 | m = sectionre.match(l) | |
98 | if m: |
|
102 | if m: | |
99 | section = m.group(1) |
|
103 | section = m.group(1) | |
100 | if section not in self: |
|
104 | if section not in self: | |
101 | self._data[section] = sortdict() |
|
105 | self._data[section] = sortdict() | |
102 | continue |
|
106 | continue | |
103 | m = itemre.match(l) |
|
107 | m = itemre.match(l) | |
104 | if m: |
|
108 | if m: | |
105 | item = m.group(1) |
|
109 | item = m.group(1) | |
106 | self.set(section, item, m.group(2), "%s:%d" % (path, line)) |
|
110 | self.set(section, item, m.group(2), "%s:%d" % (path, line)) | |
107 | cont = 1 |
|
111 | cont = 1 | |
108 | continue |
|
112 | continue | |
109 | m = unsetre.match(l) |
|
113 | m = unsetre.match(l) | |
110 | if m: |
|
114 | if m: | |
111 | name = m.group(1) |
|
115 | name = m.group(1) | |
112 | if self.get(section, name) != None: |
|
116 | if self.get(section, name) != None: | |
113 | del self._data[section][name] |
|
117 | del self._data[section][name] | |
114 | continue |
|
118 | continue | |
115 |
|
119 | |||
116 | raise error.ConfigError(_('config error at %s:%d: \'%s\'') |
|
120 | raise error.ConfigError(_('config error at %s:%d: \'%s\'') | |
117 | % (path, line, l.rstrip())) |
|
121 | % (path, line, l.rstrip())) |
General Comments 0
You need to be logged in to leave comments.
Login now