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