Mercurial > hg
comparison mercurial/ui.py @ 14171:fa2b596db182
ui: add configint function and tests
author | Sune Foldager <cryo@cyanite.org> |
---|---|
date | Tue, 03 May 2011 22:28:08 +0200 |
parents | 924c82157d46 |
children | be0daa0eeb3e |
comparison
equal
deleted
inserted
replaced
14158:d8ba6fb2ce15 | 14171:fa2b596db182 |
---|---|
162 base = os.path.dirname(src.rsplit(':')) | 162 base = os.path.dirname(src.rsplit(':')) |
163 v = os.path.join(base, os.path.expanduser(v)) | 163 v = os.path.join(base, os.path.expanduser(v)) |
164 return v | 164 return v |
165 | 165 |
166 def configbool(self, section, name, default=False, untrusted=False): | 166 def configbool(self, section, name, default=False, untrusted=False): |
167 """parse a configuration element as a boolean | |
168 | |
169 >>> u = ui(); s = 'foo' | |
170 >>> u.setconfig(s, 'true', 'yes') | |
171 >>> u.configbool(s, 'true') | |
172 True | |
173 >>> u.setconfig(s, 'false', 'no') | |
174 >>> u.configbool(s, 'false') | |
175 False | |
176 >>> u.configbool(s, 'unknown') | |
177 False | |
178 >>> u.configbool(s, 'unknown', True) | |
179 True | |
180 >>> u.setconfig(s, 'invalid', 'somevalue') | |
181 >>> u.configbool(s, 'invalid') | |
182 Traceback (most recent call last): | |
183 ... | |
184 ConfigError: foo.invalid is not a boolean ('somevalue') | |
185 """ | |
186 | |
167 v = self.config(section, name, None, untrusted) | 187 v = self.config(section, name, None, untrusted) |
168 if v is None: | 188 if v is None: |
169 return default | 189 return default |
170 if isinstance(v, bool): | 190 if isinstance(v, bool): |
171 return v | 191 return v |
172 b = util.parsebool(v) | 192 b = util.parsebool(v) |
173 if b is None: | 193 if b is None: |
174 raise error.ConfigError(_("%s.%s not a boolean ('%s')") | 194 raise error.ConfigError(_("%s.%s is not a boolean ('%s')") |
175 % (section, name, v)) | 195 % (section, name, v)) |
176 return b | 196 return b |
177 | 197 |
198 def configint(self, section, name, default=None, untrusted=False): | |
199 """parse a configuration element as an integer | |
200 | |
201 >>> u = ui(); s = 'foo' | |
202 >>> u.setconfig(s, 'int1', '42') | |
203 >>> u.configint(s, 'int1') | |
204 42 | |
205 >>> u.setconfig(s, 'int2', '-42') | |
206 >>> u.configint(s, 'int2') | |
207 -42 | |
208 >>> u.configint(s, 'unknown', 7) | |
209 7 | |
210 >>> u.setconfig(s, 'invalid', 'somevalue') | |
211 >>> u.configint(s, 'invalid') | |
212 Traceback (most recent call last): | |
213 ... | |
214 ConfigError: foo.invalid is not an integer ('somevalue') | |
215 """ | |
216 | |
217 v = self.config(section, name, None, untrusted) | |
218 if v is None: | |
219 return default | |
220 try: | |
221 return int(v) | |
222 except ValueError: | |
223 raise error.ConfigError(_("%s.%s is not an integer ('%s')") | |
224 % (section, name, v)) | |
225 | |
178 def configlist(self, section, name, default=None, untrusted=False): | 226 def configlist(self, section, name, default=None, untrusted=False): |
179 """Return a list of comma/space separated strings""" | 227 """parse a configuration element as a list of comma/space separated |
228 strings | |
229 | |
230 >>> u = ui(); s = 'foo' | |
231 >>> u.setconfig(s, 'list1', 'this,is "a small" ,test') | |
232 >>> u.configlist(s, 'list1') | |
233 ['this', 'is', 'a small', 'test'] | |
234 """ | |
180 | 235 |
181 def _parse_plain(parts, s, offset): | 236 def _parse_plain(parts, s, offset): |
182 whitespace = False | 237 whitespace = False |
183 while offset < len(s) and (s[offset].isspace() or s[offset] == ','): | 238 while offset < len(s) and (s[offset].isspace() or s[offset] == ','): |
184 whitespace = True | 239 whitespace = True |