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