@@ -60,38 +60,47 @@ def __getitem__(self, name):
60
60
def __repr__ (self ):
61
61
return '<%s %r>' % (self .__class__ .__name__ , self .filename )
62
62
63
- def get (self , section , name , default = None ):
64
- """Return the value of the specified option."""
63
+ def get (self , section , name , default = '' ):
64
+ """Return the value of the specified option.
65
+
66
+ Valid default input is a string. Returns a string.
67
+ """
65
68
return self [section ].get (name , default )
66
69
67
- def getbool (self , section , name , default = None ):
70
+ def getbool (self , section , name , default = '' ):
68
71
"""Return the specified option as boolean value.
69
72
70
73
If the value of the option is one of "yes", "true", "enabled", "on",
71
74
or "1", this method wll return `True`, otherwise `False`.
72
75
76
+ Valid default input is a string or a bool. Returns a bool.
77
+
73
78
(since Trac 0.9.3, "enabled" added in 0.11)
74
79
"""
75
80
return self [section ].getbool (name , default )
76
81
77
- def getint (self , section , name , default = None ):
82
+ def getint (self , section , name , default = '' ):
78
83
"""Return the value of the specified option as integer.
79
84
80
85
If the specified option can not be converted to an integer, a
81
86
`ConfigurationError` exception is raised.
82
87
88
+ Valid default input is a string or an int. Returns an int.
89
+
83
90
(since Trac 0.10)
84
91
"""
85
92
return self [section ].getint (name , default )
86
93
87
- def getlist (self , section , name , default = None , sep = ',' , keep_empty = False ):
94
+ def getlist (self , section , name , default = '' , sep = ',' , keep_empty = False ):
88
95
"""Return a list of values that have been specified as a single
89
96
comma-separated option.
90
97
91
98
A different separator can be specified using the `sep` parameter. If
92
99
the `keep_empty` parameter is set to `True`, empty elements are
93
100
included in the list.
94
101
102
+ Valid default input is a string or a list. Returns a string.
103
+
95
104
(since Trac 0.10)
96
105
"""
97
106
return self [section ].getlist (name , default , sep , keep_empty )
@@ -136,6 +145,27 @@ def sections(self):
136
145
parent = parent .parent
137
146
return sorted (sections )
138
147
148
+ def has_option (self , section , option ):
149
+ """Returns True if option exists in section in either project or
150
+ parent trac.ini, or available through the Option registry.
151
+
152
+ (since Trac 0.11)
153
+ """
154
+ # Check project trac.ini
155
+ for file_option , val in self .options (section ):
156
+ if file_option == option :
157
+ return True
158
+ # Check parent trac.ini
159
+ if self .parent :
160
+ for parent_option , val in self .parent .options (section ):
161
+ if parent_option == option :
162
+ return True
163
+ # Check the registry
164
+ if (section , option ) in Option .registry :
165
+ return True
166
+ # Not found
167
+ return False
168
+
139
169
def save (self ):
140
170
"""Write the configuration options to the primary file."""
141
171
if not self .filename :
@@ -238,8 +268,11 @@ def __iter__(self):
238
268
def __repr__ (self ):
239
269
return '<Section [%s]>' % (self .name )
240
270
241
- def get (self , name , default = None ):
242
- """Return the value of the specified option."""
271
+ def get (self , name , default = '' ):
272
+ """Return the value of the specified option.
273
+
274
+ Valid default input is a string. Returns a string.
275
+ """
243
276
if self .config .parser .has_option (self .name , name ):
244
277
value = self .config .parser .get (self .name , name )
245
278
elif self .config .parent :
@@ -250,44 +283,55 @@ def get(self, name, default=None):
250
283
value = option .default or default
251
284
else :
252
285
value = default
253
- if value is None :
254
- return ''
255
- return to_unicode (value )
286
+ if not value :
287
+ return u''
288
+ elif isinstance (value , basestring ):
289
+ return to_unicode (value )
290
+ else :
291
+ return value
256
292
257
- def getbool (self , name , default = None ):
293
+ def getbool (self , name , default = '' ):
258
294
"""Return the value of the specified option as boolean.
259
295
260
296
This method returns `True` if the option value is one of "yes", "true",
261
297
"enabled", "on", or "1", ignoring case. Otherwise `False` is returned.
298
+
299
+ Valid default input is a string or a bool. Returns a bool.
262
300
"""
263
301
value = self .get (name , default )
264
302
if isinstance (value , basestring ):
265
303
value = value .lower () in _TRUE_VALUES
266
304
return bool (value )
267
305
268
- def getint (self , name , default = None ):
306
+ def getint (self , name , default = '' ):
269
307
"""Return the value of the specified option as integer.
270
308
271
309
If the specified option can not be converted to an integer, a
272
310
`ConfigurationError` exception is raised.
311
+
312
+ Valid default input is a string or an int. Returns an int.
273
313
"""
274
314
value = self .get (name , default )
275
- if value == '' :
276
- return default
315
+ if not value :
316
+ return 0
277
317
try :
278
318
return int (value )
279
319
except ValueError :
280
320
raise ConfigurationError ('expected integer, got %s' % repr (value ))
281
321
282
- def getlist (self , name , default = None , sep = ',' , keep_empty = True ):
322
+ def getlist (self , name , default = '' , sep = ',' , keep_empty = True ):
283
323
"""Return a list of values that have been specified as a single
284
324
comma-separated option.
285
325
286
326
A different separator can be specified using the `sep` parameter. If
287
- the `skip_empty ` parameter is set to `True `, empty elements are omitted
327
+ the `keep_empty ` parameter is set to `False `, empty elements are omitted
288
328
from the list.
329
+
330
+ Valid default input is a string or a list. Returns a list.
289
331
"""
290
332
value = self .get (name , default )
333
+ if not value :
334
+ return []
291
335
if isinstance (value , basestring ):
292
336
items = [item .strip () for item in value .split (sep )]
293
337
else :
@@ -296,9 +340,11 @@ def getlist(self, name, default=None, sep=',', keep_empty=True):
296
340
items = filter (None , items )
297
341
return items
298
342
299
- def getpath (self , name , default = None ):
343
+ def getpath (self , name , default = '' ):
300
344
"""Return the value of the specified option as a path name, relative to
301
345
the location of the configuration file the option is defined in.
346
+
347
+ Valid default input is a string. Returns a string with normalised path.
302
348
"""
303
349
if self .config .parser .has_option (self .name , name ):
304
350
path = self .config .parser .get (self .name , name )
0 commit comments