8144
|
1 |
from i18n import _
|
|
2 |
import re, error
|
|
3 |
|
|
4 |
class sortdict(dict):
|
|
5 |
'a simple append-only sorted dictionary'
|
|
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]
|
|
26 |
|
|
27 |
class config:
|
|
28 |
def __init__(self, data=None):
|
|
29 |
self._data = {}
|
|
30 |
if data:
|
|
31 |
for k in data._data:
|
|
32 |
self._data[k] = data[k].copy()
|
|
33 |
def copy(self):
|
|
34 |
return config(self)
|
|
35 |
def __contains__(self, section):
|
|
36 |
return section in self._data
|
|
37 |
def update(self, src, sections=None):
|
|
38 |
if not sections:
|
|
39 |
sections = src.sections()
|
|
40 |
for s in sections:
|
|
41 |
if s not in src:
|
|
42 |
continue
|
|
43 |
if s not in self:
|
|
44 |
self._data[s] = sortdict()
|
|
45 |
for k in src._data[s]:
|
|
46 |
self._data[s][k] = src._data[s][k]
|
|
47 |
def get(self, section, item, default=None):
|
|
48 |
return self._data.get(section, {}).get(item, (default, ""))[0]
|
|
49 |
def getsource(self, section, item):
|
|
50 |
return self._data.get(section, {}).get(item, (None, ""))[1]
|
|
51 |
def sections(self):
|
|
52 |
return sorted(self._data.keys())
|
|
53 |
def items(self, section):
|
|
54 |
return [(k, v[0]) for k,v in self._data.get(section, {}).items()]
|
|
55 |
def set(self, section, item, value, source=""):
|
|
56 |
if section not in self:
|
|
57 |
self._data[section] = sortdict()
|
|
58 |
self._data[section][item] = (value, source)
|
|
59 |
|
|
60 |
def read(self, path, fp):
|
|
61 |
sectionre = re.compile(r'\[([^\[]+)\]')
|
|
62 |
itemre = re.compile(r'([^=\s]+)\s*=\s*(.*)')
|
|
63 |
contre = re.compile(r'\s+(\S.*)')
|
|
64 |
emptyre = re.compile(r'(;|#|\s*$)')
|
|
65 |
section = ""
|
|
66 |
item = None
|
|
67 |
line = 0
|
|
68 |
cont = 0
|
|
69 |
for l in fp:
|
|
70 |
line += 1
|
|
71 |
if cont:
|
|
72 |
m = contre.match(l)
|
|
73 |
if m:
|
|
74 |
v = self.get(section, item) + "\n" + m.group(1)
|
|
75 |
self.set(section, item, v, "%s:%d" % (path, line))
|
|
76 |
continue
|
|
77 |
item = None
|
|
78 |
if emptyre.match(l):
|
|
79 |
continue
|
|
80 |
m = sectionre.match(l)
|
|
81 |
if m:
|
|
82 |
section = m.group(1)
|
|
83 |
if section not in self:
|
|
84 |
self._data[section] = sortdict()
|
|
85 |
continue
|
|
86 |
m = itemre.match(l)
|
|
87 |
if m:
|
|
88 |
item = m.group(1)
|
|
89 |
self.set(section, item, m.group(2), "%s:%d" % (path, line))
|
|
90 |
cont = 1
|
|
91 |
continue
|
|
92 |
raise error.ConfigError(_('config error at %s:%d: \'%s\'')
|
|
93 |
% (path, line, l.rstrip()))
|