Edgewall Software

Changes between Version 1 and Version 2 of TracDev/ConfigApi


Ignore:
Timestamp:
Jan 7, 2011, 5:08:26 PM (13 years ago)
Author:
Sebastian Krysmanski <sebastian@…>
Comment:

Reading and setting option values

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/ConfigApi

    v1 v2  
    1111}}}
    1212
    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.
     13The 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.
    1416
    1517== Retrieving arbitrary option values ==
     18The easiest way to retrieve the value of a certain option is to use:
     19
     20{{{
     21#!python
     22value = env.config.get('wiki', 'ignore_missing_pages')
     23}}}
     24
     25The 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
     29env.config.get()      # as string
     30env.config.getbool()  # as bool
     31env.config.getint()   # as integer
     32env.config.getfloat() # as float; since Trac 0.12
     33env.config.getlist()  # as list
     34env.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.
    1638
    1739== Setting arbitrary option values ==
     40Setting an option value is almost as easy as retrieving one. For this purpose, you need to use the method `set()`:
    1841
    19 == Defining new options ==
     42{{{
     43#!python
     44# the last parameter is the new option value ("False" in this case)
     45env.config.set('wiki', 'ignore_missing_pages', False)
     46env.config.save()
     47}}}
     48
     49You also need to call `save()` to store the changes you've made to `trac.ini`.
     50
     51There's just one thing you need to be aware of:
     52
     53  ''The option value must be a string! ''
     54
     55This 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
     59my_list = [ 'test1', 'test2' ]
     60env.config.set('my_section', 'my_option', ', '.join(my_list))
     61env.config.save()
     62}}}
     63
     64== Defining options ==
    2065
    2166== Retrieving the value of previously defined options ==
     67
     68== Listing options == #listing_options