Changes between Version 1 and Version 2 of TracDev/ConfigApi
- Timestamp:
- Jan 7, 2011, 5:08:26 PM (13 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/ConfigApi
v1 v2 11 11 }}} 12 12 13 The file consists of multiple ''sections'' (written as `[sectionname]`). Each section consists of multiple ''options'' with their ''option values'' (like `ignore_missing_pages = false` in the example above). All options that come after the beginning of a section belong to this section - until a new section begins. 13 The file consists of multiple ''sections'' (written as `[sectionname]`). Each section consists of multiple ''options'' with their ''option values'' (like `ignore_missing_pages = false` in the example above). All options that come after the beginning of a section belong to this section - until a new section begins. 14 15 '''Note:''' The following examples will use `env.config` to access the configuration API. From within a [TracDev/ComponentArchitecture component] method you can use `self.config` to access the configuration API as well. 14 16 15 17 == Retrieving arbitrary option values == 18 The easiest way to retrieve the value of a certain option is to use: 19 20 {{{ 21 #!python 22 value = env.config.get('wiki', 'ignore_missing_pages') 23 }}} 24 25 The method `get()` will return the option value as string (type `unicode`). Of course, there are also methods to retrieve the option value in several other data formats: 26 27 {{{ 28 #!python 29 env.config.get() # as string 30 env.config.getbool() # as bool 31 env.config.getint() # as integer 32 env.config.getfloat() # as float; since Trac 0.12 33 env.config.getlist() # as list 34 env.config.getpath() # as absolute path 35 }}} 36 37 ''Note:'' Most options have some meta data (data type, description) associated with them. For getting those meta data, see [#listing_options Listing Options] below. 16 38 17 39 == Setting arbitrary option values == 40 Setting an option value is almost as easy as retrieving one. For this purpose, you need to use the method `set()`: 18 41 19 == Defining new options == 42 {{{ 43 #!python 44 # the last parameter is the new option value ("False" in this case) 45 env.config.set('wiki', 'ignore_missing_pages', False) 46 env.config.save() 47 }}} 48 49 You also need to call `save()` to store the changes you've made to `trac.ini`. 50 51 There's just one thing you need to be aware of: 52 53 ''The option value must be a string! '' 54 55 This is not a problem for most data types - except for lists. When you want to save a list, write your code like this: 56 57 {{{ 58 #!python 59 my_list = [ 'test1', 'test2' ] 60 env.config.set('my_section', 'my_option', ', '.join(my_list)) 61 env.config.save() 62 }}} 63 64 == Defining options == 20 65 21 66 == Retrieving the value of previously defined options == 67 68 == Listing options == #listing_options