1 .. default-role:: literal
3 =======================
5 =======================
7 :module: :api:`config.py`
8 :tests: - :api:`test_config.py`
9 - :api:`test_option_setting.py`
14 Configuration option objects :api:`config.Config()` are produced at the
15 entry points and handed down to where they are actually used. This keeps
16 configuration local but available everywhere and consistent.
18 `Config` and `Option` objects
19 ==============================
21 Configuration option objects can be created in different ways. Let's perform
22 very basic `Config` object manipulations:
26 >>> from tiramisu.config import Config
27 >>> from tiramisu.option import OptionDescription, BoolOption
28 >>> descr = OptionDescription("optgroup", "", [
29 ... BoolOption("bool", "", default=False)])
31 >>> config = Config(descr)
34 >>> config.bool = True
38 Take a look at :api:`test_config.test_base_config()` or
39 :api:`test_config.test_base_config_and_groups()`.
42 Accessing the configuration `Option`'s
43 -----------------------------------------
45 The `Config` object attribute access notation stands for the value of the
46 configuration's `Option`. That is, the `Config`'s object attribute is the name
47 of the `Option`, and the value is the value accessed by the `__getattr__`
48 attribute access mechanism.
50 If the attribute of the `Config` called by `__getattr__` has not been set before
51 (by the classic `__setattr__` mechanism), the default value of the `Option`
52 object is returned, and if no `Option` has been declared in the
53 `OptionDescription` (that is the schema of the configuration), an
54 `AttributeError` is raised.
58 >>> gcdummy = BoolOption('dummy', 'dummy', default=False)
61 >>> gcdummy.getdefault()
63 >>> descr = OptionDescription('tiramisu', '', [gcdummy])
64 >>> cfg = Config(descr)
71 AttributeError: 'OptionDescription' object has no attribute 'idontexist'
73 The configuration `Option` objects (in this case the `BoolOption`), are
74 organized into a tree into nested `OptionDescription` objects. Every
75 option has a name, as does every option group. The parts of the full
76 name of the option are separated by dots: e.g.
77 ``config.optgroup.optname``.
79 **Can you repeat it, what is the protocol of accessing a config's attribute ?**
81 1. If the option has not been declared, an `AttributeError` is raised,
83 2. If an option is declared, but neither a value nor a default value has
84 been set, the returned value is `None`,
86 3. If an option is declared and a default value has been set, but no value
87 has been set, the returned value is the default value of the option,
89 4. If an option is declared, and a value has been set, the returned value is
90 the value of the option.
92 If you do not want to use the pythonic way, that is the attribute access
93 way to obtain the value of the configuration option, you can also search
94 for it recursively in the whole config namespaces with the ``get()``
99 >>> config.get('bool')
103 To find the right option, `get()` searches recursively into the whole
104 tree. For example, to find an option which is in the `gc` namespace
105 there are two possibilites.
107 If you know the path:
114 If you don't remember the path:
118 >>> config.get('dummy')
121 Setting the values of the options
122 ----------------------------------------
124 An important part of the setting of the configuration consists of setting the
125 values of the configuration options. There are different ways of setting values,
126 the first one is of course the `__setattr__` method
132 wich has the same effect that the "global" `set()` method : it expects that
133 the value owner is the default :ref:`glossary#valueowner`
139 The global `setoption()` method of the config objects can set a value with a specific owner
143 cfg.setoption('name', value, 'owner')
146 Finally, the local `setoption()` method directly in the `Option` object can be
147 used. While the `Option` object refers to his parent, the config knows that the
148 value has been changed and no bad side effect won't occur
152 >>> booloption = BoolOption('bool', 'Test boolean option', default=True)
153 >>> descr = OptionDescription('descr', '', [booloption])
154 >>> cfg = Config(descr)
155 >>> booloption.setoption(cfg, False, 'owner')