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