##// END OF EJS Templates
ui: add configint function and tests
Sune Foldager -
r14171:fa2b596d default
parent child Browse files
Show More
@@ -164,6 +164,26 b' class ui(object):'
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
@@ -171,12 +191,47 b' class ui(object):'
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
@@ -16,6 +16,9 b' doctest.testmod(mercurial.match)'
16 import mercurial.store
16 import mercurial.store
17 doctest.testmod(mercurial.store)
17 doctest.testmod(mercurial.store)
18
18
19 import mercurial.ui
20 doctest.testmod(mercurial.ui)
21
19 import mercurial.url
22 import mercurial.url
20 doctest.testmod(mercurial.url)
23 doctest.testmod(mercurial.url)
21
24
@@ -5,6 +5,10 b' parsed = dispatch._parseconfig(testui, ['
5 'values.string=string value',
5 'values.string=string value',
6 'values.bool1=true',
6 'values.bool1=true',
7 'values.bool2=false',
7 'values.bool2=false',
8 'values.boolinvalid=foo',
9 'values.int1=42',
10 'values.int2=-42',
11 'values.intinvalid=foo',
8 'lists.list1=foo',
12 'lists.list1=foo',
9 'lists.list2=foo bar baz',
13 'lists.list2=foo bar baz',
10 'lists.list3=alice, bob',
14 'lists.list3=alice, bob',
@@ -23,7 +27,7 b' parsed = dispatch._parseconfig(testui, ['
23 'lists.list16="longer quotation" with "no ending quotation',
27 'lists.list16="longer quotation" with "no ending quotation',
24 'lists.list17=this is \\" "not a quotation mark"',
28 'lists.list17=this is \\" "not a quotation mark"',
25 'lists.list18=\n \n\nding\ndong',
29 'lists.list18=\n \n\nding\ndong',
26 ])
30 ])
27
31
28 print repr(testui.configitems('values'))
32 print repr(testui.configitems('values'))
29 print repr(testui.configitems('lists'))
33 print repr(testui.configitems('lists'))
@@ -43,6 +47,9 b" print repr(testui.configbool('values', '"
43 print repr(testui.configbool('values', 'unknown'))
47 print repr(testui.configbool('values', 'unknown'))
44 print repr(testui.configbool('values', 'unknown', True))
48 print repr(testui.configbool('values', 'unknown', True))
45 print "---"
49 print "---"
50 print repr(testui.configint('values', 'int1'))
51 print repr(testui.configint('values', 'int2'))
52 print "---"
46 print repr(testui.configlist('lists', 'list1'))
53 print repr(testui.configlist('lists', 'list1'))
47 print repr(testui.configlist('lists', 'list2'))
54 print repr(testui.configlist('lists', 'list2'))
48 print repr(testui.configlist('lists', 'list3'))
55 print repr(testui.configlist('lists', 'list3'))
@@ -79,3 +86,13 b' def function():'
79 # values that aren't strings should work
86 # values that aren't strings should work
80 testui.setconfig('hook', 'commit', function)
87 testui.setconfig('hook', 'commit', function)
81 print function == testui.config('hook', 'commit')
88 print function == testui.config('hook', 'commit')
89
90 # invalid values
91 try:
92 testui.configbool('values', 'boolinvalid')
93 except error.ConfigError:
94 print 'boolinvalid'
95 try:
96 testui.configint('values', 'intinvalid')
97 except error.ConfigError:
98 print 'intinvalid'
@@ -1,4 +1,4 b''
1 [('string', 'string value'), ('bool1', 'true'), ('bool2', 'false')]
1 [('string', 'string value'), ('bool1', 'true'), ('bool2', 'false'), ('boolinvalid', 'foo'), ('int1', '42'), ('int2', '-42'), ('intinvalid', 'foo')]
2 [('list1', 'foo'), ('list2', 'foo bar baz'), ('list3', 'alice, bob'), ('list4', 'foo bar baz alice, bob'), ('list5', 'abc d"ef"g "hij def"'), ('list6', '"hello world", "how are you?"'), ('list7', 'Do"Not"Separate'), ('list8', '"Do"Separate'), ('list9', '"Do\\"NotSeparate"'), ('list10', 'string "with extraneous" quotation mark"'), ('list11', 'x, y'), ('list12', '"x", "y"'), ('list13', '""" key = "x", "y" """'), ('list14', ',,,, '), ('list15', '" just with starting quotation'), ('list16', '"longer quotation" with "no ending quotation'), ('list17', 'this is \\" "not a quotation mark"'), ('list18', '\n \n\nding\ndong')]
2 [('list1', 'foo'), ('list2', 'foo bar baz'), ('list3', 'alice, bob'), ('list4', 'foo bar baz alice, bob'), ('list5', 'abc d"ef"g "hij def"'), ('list6', '"hello world", "how are you?"'), ('list7', 'Do"Not"Separate'), ('list8', '"Do"Separate'), ('list9', '"Do\\"NotSeparate"'), ('list10', 'string "with extraneous" quotation mark"'), ('list11', 'x, y'), ('list12', '"x", "y"'), ('list13', '""" key = "x", "y" """'), ('list14', ',,,, '), ('list15', '" just with starting quotation'), ('list16', '"longer quotation" with "no ending quotation'), ('list17', 'this is \\" "not a quotation mark"'), ('list18', '\n \n\nding\ndong')]
3 ---
3 ---
4 'string value'
4 'string value'
@@ -6,13 +6,16 b''
6 'false'
6 'false'
7 None
7 None
8 ---
8 ---
9 values.string not a boolean ('string value')
9 values.string is not a boolean ('string value')
10 True
10 True
11 False
11 False
12 False
12 False
13 False
13 False
14 True
14 True
15 ---
15 ---
16 42
17 -42
18 ---
16 ['foo']
19 ['foo']
17 ['foo', 'bar', 'baz']
20 ['foo', 'bar', 'baz']
18 ['alice', 'bob']
21 ['alice', 'bob']
@@ -42,3 +45,5 b' True'
42 ['foo', 'bar']
45 ['foo', 'bar']
43 None
46 None
44 True
47 True
48 boolinvalid
49 intinvalid
General Comments 0
You need to be logged in to leave comments. Login now