Changes between Version 8 and Version 9 of TracDev/ConfigApi
- Timestamp:
- Feb 3, 2015, 7:31:29 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/ConfigApi
v8 v9 1 = Configuration (trac.ini) API = 2 Most of Trac's configuration is stored in the [TracIni trac.ini] file. Trac provides an API to retrieve and set the settings in this configuration file. 1 = Configuration (trac.ini) API 3 2 4 For the sake of this article, here's a quick reminder of the structure of `trac.ini`:3 Most of Trac's configuration is stored in the [TracIni trac.ini] file. Trac provides an API to retrieve and set the settings in this configuration file. The `trac.ini` file has the following structure: 5 4 6 5 {{{ … … 11 10 }}} 12 11 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.12 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 13 15 14 '''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. 16 15 17 == Retrieving arbitrary option values == 16 == Retrieving arbitrary option values 17 18 18 The easiest way to retrieve the value of a certain option is to use: 19 19 … … 23 23 }}} 24 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 severalother data formats: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 other data formats: 26 26 27 27 {{{ … … 37 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. 38 38 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()`: 39 == Setting arbitrary option values 40 41 Setting an option value is almost as easy as retrieving one. Use the method `set()`: 41 42 42 43 {{{ … … 63 64 64 65 == Defining options == #defining_options 66 65 67 While you can use `config.set()` to store values for arbitrary options, there's also a way to tell Trac which options are available. To do this, create a component and specify the option like this: 66 68 … … 92 94 }}} 93 95 94 Secondly, you can define a default value for the option. If no value has been defined in `trac.ini` for this specific option, the default value will used as value (no matter which of the two previously mentioned method is used).96 Secondly, you can define a default value for the option. If no value has been defined in `trac.ini` for this specific option, the default value will used as value, regardless which of the two previously mentioned method is used. 95 97 96 98 Last, this allows plugins like [th:IniAdminPlugin] or [th:TracIniAdminPanelPlugin] to work. These plugins allow the user to edit `trac.ini` via the webadmin interface. For this purpose they need to know which options exist which is done like described in this section. 97 99 98 100 == Retrieving the value of previously defined options == #retrieving_defined_options 99 As describes in the previous section, defining an option (in a component) allows you to retrieve its value more easily. Furthermore, this definition also allows for automatic type conversion. For this you need to use one of the child classes of `Option` (instead of `Option` itself). 101 102 As describes in the previous section, defining an option (in a component) allows you to retrieve its value more easily. Furthermore, this definition also allows for automatic type conversion. For this you need to use one of the child classes of `Option` instead of `Option` itself. 100 103 101 104 For example, let's assume the option `my_option` defined in the next example has the value `1.234`. Now consider calling `my_method` in the following code: … … 113 116 }}} 114 117 115 The option value has automatically been converted to `float`. If you had simply used `Option` (instead of `FloatOption`), the value would have been a string (instead of a `float`).118 The option value has automatically been converted to `float`. If you had simply used `Option` instead of `FloatOption`, the value would have been a string. 116 119 117 Beside these "simple" types (`BoolOption`, `IntOption`, `FloatOption`) there are also options with a little bit more "magic":120 Beside these simple types (`BoolOption`, `IntOption`, `FloatOption`), there are also options with a little bit more complexity: 118 121 119 122 * `PathOption` simply describes a path that can be absolute or relative. The option always returns an absolute path. Relative paths are resolved relative to the `conf` directory of the environment. … … 135 138 }}} 136 139 137 ''Note:'' Currently (as with Trac ''0.12.1'') you can't ''set'' an option's value this way (e.g. with `self.my_option = new_option_value`). This will raise an !AttributeError, but there's ticket #9967 that aims to fix this problem.140 ''Note:'' Currently (as with Trac ''0.12.1'') you can't ''set'' an option's value this way, eg with `self.my_option = new_option_value`. This will raise an !AttributeError, but ticket #9967 aims to fix this. 138 141 139 === Why does this work? === 142 === Why does this work? 143 140 144 So, how can one define an option as `ListOption` but end up with a list? This works because there are two ways to access the option: as class variable or as instance variable. 141 145 … … 155 159 }}} 156 160 157 The first way uses the ''instance'' attribute which is the actual list, while the second way uses the ''class'' attribute which is the actual `ListOption` object.161 The first way uses the ''instance'' attribute, which is the actual list, while the second way uses the ''class'' attribute which is the actual `ListOption` object. 158 162 159 163 When you try to access another component's defined options the difference between these two ways is less obvious, so be careful: … … 170 174 }}} 171 175 172 For a more deeper understanding of how this implementation works, see the official [http://docs.python.org/howto/descriptor.html Descriptor HowTo Guide] -since the `Option` class is a data descriptor.176 For a deeper understanding of how this implementation works, see the official [http://docs.python.org/howto/descriptor.html Descriptor HowTo Guide], since the `Option` class is a data descriptor. 173 177 174 178 == Listing known options == #listing_options 175 Beside retrieving the value of a certain option, there are also methods for listing all known options. 179 180 Beside retrieving the value of a certain option, there are also methods for listing all known options: 176 181 177 182 * `config.sections()`: Returns a list of the names of all known sections. … … 179 184 * `config.defaults()`: Returns a dict with the default values of all known options (that have one) in all known sections. 180 185 181 A "known option" in this context is an option that 186 A "known option" in this context is an option that: 182 187 183 188 * is defined like described in the section [#defining_options Defining Options] above 184 189 * and/or has value assigned to it. 185 190 186 Furthermore, there is a way to list all ''defined'' options. This is done by using `Option.registry` which is a dict with `(section_name, option_name)` keys. The value for each key is the `Option` object that defines the option (not its current value). The following example will listthe descriptions of all defined options:191 Furthermore, there is a way to list all ''defined'' options. This is done by using `Option.registry` which is a dict with `(section_name, option_name)` keys. The value for each key is the `Option` object that defines the option, not its current value. The following example lists the descriptions of all defined options: 187 192 188 193 {{{